简体   繁体   中英

Socket programming network stream functionality c#

I'm new to socket programming. I found some help on internet. when sending some data it uses network stream function. I'm confused why 60000 is subtracted from it. i tried to remove it or even change it but it throws an exception "Index out of bound".
my question is why 60000 is subtracted from receivedBufferSize? please help

NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize-60000);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  1. You should pass the length of your buffer in the third argument, for example bytesFrom.Length . The value Socket.ReceiveBufferSize is internal socket buffer size in the operating system. There is usually no need to read or change this value.

  2. You should use return value of Read function - the number of actually read bytes, for example: System.Text.Encoding.ASCII.GetString(bytesFrom, 0, numRead); . Note that the receive operation may be fragmented and you will need several read operations to read the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM