简体   繁体   中英

C# multiThreaded client-server

I am doing a multi-threaded server-client project. In my project server is receiving the connection request from client and provides a thread to handle read and send data. But while Reading data it's gives an exception at line 61.

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

Exception is:

 >> System.ArgumentOutOfRangeException: Specified argument was out of the range
of valid values.
Parameter name: size
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
ize)
   at Server.handleClinet.doChat() in C:\Users\Mamun\documents\visual studio 201
0\Projects\Server\Server\Program.cs:line 62

I've uploaded my Project here : http://www.mediafire.com/?z2z5moqn8zdl6pt

I'am using these code-references: http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm

http://csharp.net-informations.com/communications/csharp-multi-threaded-client-socket.htm

Please see my project and tell me what's wrong. Thanks in advance.

According to MSDN that error can happen if:

The size parameter is less than 0.

-or-

The size parameter is greater than the length of buffer minus the value of the offset parameter.

I haven't seen your code, but would guess it's about the 2nd case . In the example from MSDN they simply use the length of the buffer itself as the size value:

byte[] myReadBuffer = new byte[1024];
int numberOfBytesRead = 0;

do
{
  numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

  //..
}
while(myNetworkStream.DataAvailable);

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