i learning how send message on tcp/ip connection in visual basic between 2 computers linked ethernet cable. when send message, console screen scrolls far down , received message no longer shows on host computer's console window. when create loop outputs message several hundred times can see message scrolling through console window, @ end window left black, i'm thinking means window continued scroll.
i inputing message client console , having listener console output message.
here host/listener code:
imports system.net.sockets imports system imports system.io imports system.net imports system.text imports microsoft.visualbasic module module1 sub main() 'open listener @ port 8 dim myhost new tcplistener(8) myhost.start() console.writeline("waiting connection") dim myclient tcpclient = myhost.accepttcpclient console.writeline("connected") dim mystream networkstream = myclient.getstream dim bytes(myclient.receivebuffersize) byte dim receivedmessage string mystream.read(bytes, 0, cint(myclient.receivebuffersize)) receivedmessage = encoding.ascii.getstring(bytes) console.writeline("message was: " & receivedmessage) system.threading.thread.sleep(2000) console.readline() myclient.close() myhost.stop() end sub end module here code client, imports same above:
module module1 sub main() dim myclient new tcpclient myclient.connect("my ip", 8) 'connects laptop ip on port 8 dim mystream networkstream = myclient.getstream() dim message string message = console.readline console.writeline("we sending read line") sendoverip(message, mystream) console.readline() end sub public sub sendoverip(byval message string, byval mystream networkstream) dim sendbytes [byte]() = encoding.ascii.getbytes(message) 'turns message ascii bytes mystream.write(sendbytes, 0, sendbytes.length) console.writeline("we sent: " & message) end sub end module i have breakpoint @ point in listener
console.writeline("message was: " & receivedmessage) and tell continue, console window becomes black. i'm assuming writes line continues scroll. how can make received message stays on console output listener?
i think it's because converting entire bytes array string in "host/listener". need convert actual bytes received, not whole buffer:
dim actualbytes = mystream.read(bytes, 0, bytes.length) receivedmessage = encoding.ascii.getstring(bytes, 0, actualbytes)
Comments
Post a Comment