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

  1. first declare new variable extendedtcpclient, , sure include withevents in declaration.

    dim withevents client extendedtcpclient 
  2. 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 
  3. 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 of extendedtcpclient. class requires have form it's owner, in application me current form.
    use extendedtcpclient.setnewclient() method listener.accepttcpclient() it's argument apply tcpclient listener. put code in tmr_tick sub:

    if listener.pending() = true     client = new extendedtcpclient(me)     client.setnewclient(listener.accepttcpclient()) end if 

    now client , server connected each other.

  4. 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 text textbox. check if packet header plaintext , can convert received packets contents (which array of bytes, accessed via e.packet.contents) string , put in textbox.

    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.

  5. 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. 
  6. 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

  1. for client side pretty same server side, though won't needing tcplistener nor timer.

    dim client new extendedtcpclient(me) 'the current form it's owner. 
  2. connect server via ip , port you've given listener.

    client.connect("127.0.0.1", 5555) 'connects localhost (your computer) @ port 5555. 
  3. 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.

  4. 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)

extendedtcpclient example project output


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -