0

I am needing to translate this code to add a big endian 2 byte header. It works now without header but I am a little lost on how to add it.

TcpClient client = new TcpClient(ipNum, portNum);
NetworkStream nw = client.GetStream();
byte[] send = Encoding.UTF8.GetBytes(userInput);

Console.WriteLine("Sending : " + userInput);
nw.Write(send, 0, send.Length);

byte[] readBytes = new byte[client.ReceiveBufferSize];
int bytesRead = nw.Read(readBytes, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.UTF8.GetString(readBytes 0, readBytes);
Nejc Galof
  • 2,538
  • 3
  • 31
  • 70
user3772253
  • 53
  • 1
  • 6
  • do a google search on `Adding header to tcp client` in the meantime check this out - http://stackoverflow.com/questions/19523088/create-http-request-using-tcpclient – MethodMan Mar 03 '16 at 21:09

1 Answers1

1

UTF8Encoding supports adding that header. Instantiate that class manually and use the constructor that allows you to specify that you want a BOM header.

Note, that your read code is broken because you might receive less than one "message" from the remote side. You might receive just one byte at a time. client.ReceiveBufferSize also makes no sense. Just choose a static buffer size such as 4096.

usr
  • 168,620
  • 35
  • 240
  • 369