1
votes

I am sending two byte arrays in the same buffer to be sent to a socket i don't know how to separate them when received

clientSocket.SendBufferSize = blindedVote.getBytes().Length + sBEVote.Length;
byte[] outStream = new byte[clientSocket.SendBufferSize];
blindedVote.getBytes().CopyTo(outStream, 0);
sBEVote.CopyTo(outStream, blindedVote.getBytes().Length);
serverStream.Write(outStream, 0, outStream.Length);
// int size = clientSocket.SendBufferSize;
serverStream.Flush();

when received how can i separate each array of bytes?

thanks

1
Send the length, which is a fixed length (the length of a long for instance), then read that many bytes from the stream. That's the general idea. - default

1 Answers

0
votes

It generally depends on your protocol.

You might try creating a specific data structure and send it whole - with the lengths of each array, the actual data, some CRC (it's always good to check data from the socket in one way or another), anything else.

Other option would be to send the length of an array, the first array, wait for some acknowledgement from the receiver, repeat with the second array, etc.

You can't do that without any knowledge of the data on the receiver side.