As part of a ‘protocol’ I have an Enum, which is shared across the client and server, which dictates what to do with the rest of the data
Client:
byte[] outStream = Encoding.UTF8.GetBytes((int)Shared.CommandType.ClientJoin + "sometext");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
And at the server end:
string dataFromClient = null;
byte[] bytesFrom = new byte[1024];
NetworkStream networkStream = tcpclient.GetStream();
int bytesread = networkStream.Read(bytesFrom, 0, bytesFrom.Length);
Byte[] commandBytes = new Byte[4];
Array.Copy(bytesFrom, commandBytes, 4);
dataFromClient = Encoding.UTF8.GetString(bytesFrom, 4, bytesread - 4);
//test values
int receivedcommand = BitConverter.ToInt32(commandBytes, 0);
int actualcommand = (int)Shared.CommandType.ClientJoin;
However, the received ‘value’ (1634222896 in tests) is wildly different to the actual value (0)
I’m sure I’m doing something wildly wrong, something to do with the byte array splitting, but i can’t for the life of me see what
Any ideas?
(as always, if there is a better method of implementation than the one above, i am open to suggestions 🙂 )
becomes
After receiving
commandByteswill have bytes representation of{'0','s','o','m'}and
BitConverter.ToInt32(commandBytes, 0);will just make an integer representation of 4 byte array,all of whose members having a non zero value (Since all are ascii text).