Socket Send Problem

When i'am sending something like this:

Dim bye() As Byte

For i As Integer = 1 To Clients.Count

bye = stringTobyte(str)

stream.Write(bye, 0, bye.Length)

Next

It will always be send in one string "string1string2string3" but i want it to send all strings alone so..

"string1"

"string2" ....

when i'am using stream.flush its dosn't working too.

What can i do that this works



Answer this question

Socket Send Problem

  • LibertadV

    As mike points out, you need to add framing logic to your network traffic.  There is no guarantee that the data will arrive on the other side of the stream in the same size of chunks that you write to the stream. 

    An example solution borrowed from "Chunking" in HTTP is that you put the size of the data (http uses hex numbers in string format) followed by a new line and then the data follows that. 

    Using this format, if I wanted to write "hello world" on the wire, I would actually end up writing this to the stream:

    b\r\nhello world

    The other end of the stream would read data until it gets the \r\n sequence.  It would take that portion of the data and convert it to a digit (remembering that it is hex) and then read 11 bytes from the stream.  It is like reading a binary file from the disk, you have to know the format of the file or the file has to include information on how much data to read.



  • Nikas1

    You need to add some sort o separator character between strings like a new line for example.

    For i As Integer = 1 To Clients.Count

    bye = stringTobyte(str)

    stream.Write(bye, 0, bye.Length)

    bye = stringToByte(Environment.NewLine)

    stream.Write(bye, 0, bye.Length)

    Next

    In TCP protocol there is no guarantee that when you Write something at one end the Read at the other end will return exactly the same thing. TCP can concatenate multiple buffers and send them at once or it can split a large buffer into smaller ones and send them one by one.


  • Michael Zhou

    mh in my str i have used a new line

    str = teststring & vbcrlf

    but it dosn't work.... :\


  • Socket Send Problem