i want send data (text) specific ip/port within same network. know code enough, heard "but adviced use length-prefixed messages" - of tell me true, , if should change within code what?
''' <summary> ''' send data on tcp/ip network ''' </summary> ''' <param name="data">data string write</param> ''' <param name="ip">the connected destination tcpclient</param> public sub writedata(byval data string, byref ip string) console.writeline("sending message """ & data & """ " & ip) dim client tcpclient = new tcpclient() client.connect(new ipendpoint(ipaddress.parse(ip), my.settings.commport)) dim stream networkstream = client.getstream() dim sendbytes byte() = encoding.ascii.getbytes(data) stream.write(sendbytes, 0, sendbytes.length) end sub
here opinions:
in general, in data communication context, message
header
have (includinglength
of message - put part ofheader
), when data of various types , of various length.this because in receiver site, can choose how handle incoming data before data received completely.
for instance, if have 2 data types: long text , highly-compressed images. header (which first received in receiver side) tell kind of data receiver receiving.
if long text, can process (like displaying partial content) before stream finished, in highly compressed image cannot done.
think in case may receive multiple (different) data sender , same ports. in such case, having header per message sending receiver know, message belong data , not concat them wrongly.
thus, in context, prefixed-length can put part of
header
- assuming receiver may receive various data variable length. this, knows when consider data communication "completed" form complete packet.
this not finished though... may want check integrity of packet received (by having suffix/appended message crc) or kind of encryption/decryption.
but general idea is have mentioned reasons.
Comments
Post a Comment