Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Winsock length prefix

$
0
0
Hello.

I am building a chat client-server application and it works mostly.
But now I'm faced with the problem of transferring files, as they are binary data.
I was previously using a message delimiter to delimit the end of each message. Unfortunately it doesn't work the same for binary data, so I'm now using a length prefix type of data parsing.
It works, but when I try to read simultaneous sent data, they end up becoming together, and I can't split them because before I could split them by their delimiter, but now I have nothing to split them with.

The way I'm sending the data is:
"8/aTest"

8 is the data's length, "/a" is the character I use to know when to stop looking for the data's length (8), and Test is the actual data in Unicode.

This is the receiver part:

int bytes_to_read = 0;
string data = null;
while (true)
{
	byte[] bytes = new byte[1024];
	int read = tcpclient.GetStream().Read(bytes, 0, bytes.Length);
	if (read == 0)
	{
		break;
	}
	if (bytes_to_read == 0)
	{
		bytes_to_read = System.Convert.ToInt32(System.Text.Encoding.Unicode.GetString(bytes, 0, read).Substring(0, System.Text.Encoding.Unicode.GetString(bytes, 0, read).IndexOf('\a')));
	}
	data += System.Text.Encoding.Unicode.GetString(bytes, 0, read).Substring(System.Text.Encoding.Unicode.GetString(bytes, 0, read).IndexOf('\a') + 1);



Any hints?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>