TCP Client/Server Image Transfer

后端 未结 5 1186
生来不讨喜
生来不讨喜 2021-01-07 04:04

I\'m trying to send an image using a TCP socket. The client connects to the server without any problems and start to receive the data. The problem is when I try to convert t

5条回答
  •  别那么骄傲
    2021-01-07 04:29

    This part looks funky to me:

      byte[] bytes = new byte[client.ReceiveBufferSize]; 
      int i; 
      if (nNetStream.CanRead) 
      { 
        nNetStream.Read(bytes, 0, bytes.Length);   
    
        Image returnImage = Image.FromStream(nNetStream); //exception occurs here 
    

    First you read client.ReceiveBufferSize bytes into the "bytes" array, and then you proceed to construct the image from what's left on the stream. What about the bytes you just read into "bytes"?

提交回复
热议问题