vb.net - TCP Client to Server communication -
all i'm looking simple tcpclient/listner example on windows form vb.net. i'm newbie , microsoft tcpclient/listner class examples not looking for. looking tcpclient send message , tcplistener message , send message "i got message" ?
a little great. have codes, send message server , not server client.. appreciated..
tcp communication what's called stream-based, means doesn't handle packets. due messages received can partial or lumped together.
you example send:
hello!
how you?
but might receive:
hello!how you?
or:
hello!how ar
e you?
(or similar)
to fix must apply called "length-prefixing". length-prefixing (or length prefixing) means before send message, put it's length (amount of characters) in beginning of it. can read endpoint read each message according it's length, there no problems partial messages or messages lumped together.
this not simple thing do, i've created 2 classes you. see examples below on how use them simple message-based communication.
link source: http://www.mydoomsite.com/sourcecodes/extendedtcpclient.zip
link c# source : http://www.mydoomsite.com/sourcecodes/extendedtcpclient%20csharp.zip
example usage
note in examples client
not refer client side, tcpclient.
server side
first declare new variable
extendedtcpclient
, , sure includewithevents
in declaration.dim withevents client extendedtcpclient
then need
tcplistener
,timer
check incoming connections.dim listener new tcplistener("0.0.0.0", 5555) 'listen connection on port 5555. dim withevents tmr new system.windows.forms.timer
then need subscribe timer's
tick
event.private sub tmr_tick(sender system.object, e system.eventargs) handles tmr.tick end sub
in there check incoming connections via
listener.pending()
method. when accept connection first declare new instance ofextendedtcpclient
. class requires have form it's owner, in applicationme
current form.
useextendedtcpclient.setnewclient()
methodlistener.accepttcpclient()
it's argument applytcpclient
listener. put code intmr_tick
sub:if listener.pending() = true client = new extendedtcpclient(me) client.setnewclient(listener.accepttcpclient()) end if
now client , server connected each other.
now need subscribe
packetreceived
event of client. create sub so:private sub client_packetreceived(sender object, e extendedtcpclient.packetreceivedeventargs) handles client.packetreceived end sub
all received data presented in array of bytes. in
packetreceived
sub can output received packet texttextbox
. check if packet headerplaintext
, can convert received packets contents (which array of bytes, accessed viae.packet.contents
) string , put intextbox
.if e.packet.header = tcpmessagepacket.packetheader.plaintext textbox1.appendtext("message recieved: " & system.text.encoding.default.getstring(e.packet.contents) & environment.newline) end if
system.text.encoding.default.getstring()
convert byte array normal text.in
packetreceived
sub can make send "message received" client.dim responsepacket new tcpmessagepacket(system.text.encoding.default.getbytes("message received."), tcpmessagepacket.packetheader.plaintext) responsepacket.send(client.client) 'get extendedtcpclient's underlying tcpclient.
lastly, when closing form need disconnect client.
private sub serverwindow_formclosing(sender object, e system.windows.forms.formclosingeventargs) handles me.formclosing if client isnot nothing client.disconnect() end sub
and that's server side.
client side
for client side pretty same server side, though won't needing
tcplistener
nortimer
.dim client new extendedtcpclient(me) 'the current form it's owner.
connect server via ip , port you've given listener.
client.connect("127.0.0.1", 5555) 'connects localhost (your computer) @ port 5555.
now if want send text server you'd (in example button):
dim messagepacket new tcpmessagepacket(system.text.encoding.default.getbytes(textbox2.text), tcpmessagepacket.packetheader.plaintext) messagepacket.send(client.client)
textbox2
includes text want send.lastly, need subscribe
packetreceived
event here check responses server. in there receive text server does.private sub client_packetreceived(sender object, e extendedtcpclient.packetreceivedeventargs) handles client.packetreceived if e.packet.header = tcpmessagepacket.packetheader.plaintext textbox1.appendtext(system.text.encoding.default.getstring(e.packet.contents) & environment.newline) 'prints example "message received." server. end if end sub
and should working!
link complete example project (only client-to-server): http://www.mydoomsite.com/sourcecodes/tcp%20messaging%20system.zip
link c# example: http://www.mydoomsite.com/sourcecodes/csharp%20tcp%20messaging%20system.zip
if want add more headers class (the headers indicates type of packet is), open tcpmessagepacket.vb
, add more values in packetheader
enum (located in region called constants
).
hope helps!
screenshot example project
(click image larger resolution)
Comments
Post a Comment