2016-10-21 16 views
0

私はまだTcpソケットで立ち往生しています。私は現在サーバー/クライアントプロジェクトで作業中です クライアントから送信された完全なデータを受信しようとしていますが、すべてを手に入れよう。
いくつかの調査の後、私はone Send doesnt surely mean one receiveを知っているので、バッファサイズが送信されたデータのサイズに達するまで読書を続ける必要があります。C#ソケット完全なデータを受信

public void Connect() 
    { 

     try 
     { 
      _ThisSocket.Connect(this._HOST); 
     }catch {} 

     new Thread(() => 
     { 
      while (_ThisSocket.Connected) 
      { 
       _ThisSocket.Receive(BufferSize, 0, 4, SocketFlags.None); 
       int Size = BitConverter.ToInt32(BufferSize, 0); 
       while (Size > 0) 
       { 
        if (Size < _ThisSocket.ReceiveBufferSize) 
        { 
         _Buffer = new byte[Size]; 
        } 
        else 
        { 
         _Buffer = new byte[_ThisSocket.ReceiveBufferSize]; 
        } 
        _ThisSocket.BeginReceive(_Buffer, 0, _Buffer.Length, SocketFlags.None, Receive, _Buffer.Length); 

       } 
      } 
     } 
     ).Start(); 


    } 

private void Receive(IAsyncResult AR) 
{ 
    int Size = (int)AR.AsyncState; 
    Byte[] buff = new Byte[Size]; 
    Array.Copy(_Buffer, buff, Size); 
    String Data = Encoding.Unicode.GetString(buff); 
    String Cmd = Crypter.Decrypt(Data); 
    Switch(Cmd); 

} 

私はまだC#プログラミングに新しいですが、私は何の言い訳もありません!

+0

はこの質問を見てみましょう私は数年前に尋ねましたが、答えはいくつかの有用な洞察を与えます。 http://stackoverflow.com/questions/7126985/will-this-code-make-sure-i-read-all-i-want-from-a-socket – cost

答えて

0

これはSocketクラスを使用した例です。ソケット経由で大きな ファイルおよびすべてのデータを受信する例:

 private byte[] ReceiveLargeFile(Socket socket, int lenght) 
    { 
     // send first the length of total bytes of the data to server 
     // create byte array with the length that you've send to the server. 
     byte[] data = new byte[lenght]; 


     int size = lenght; // lenght to reveive 
     var total = 0; // total bytes to received 
     var dataleft = size; // bytes that havend been received 

     // 1. check if the total bytes that are received < than the size you've send before to the server. 
     // 2. if true read the bytes that have not been receive jet 
     while (total < size) 
     { 
      // receive bytes in byte array data[] 
      // from position of total received and if the case data that havend been received. 
      var recv = socket.Receive(data, total, dataleft, SocketFlags.None); 
      if (recv == 0) // if received data = 0 than stop reseaving 
      { 
       data = null; 
       break; 
      } 
      total += recv; // total bytes read + bytes that are received 
      dataleft -= recv; // bytes that havend been received 
     } 
     return data; // return byte array and do what you have to do whith the bytes. 
    } 
+0

私は完全に理解するためにいくつかの説明を必要としています –

+0

これは明らかに明白です? –

0

バッファサイズを設定する必要はありません、この方法で怒鳴るは応答サイズに収まる:

public static byte[] ReceiveAll(this Socket socket) 
{ 
    var buffer = new List<byte>(); 

    while (socket.Available > 0) 
    { 
     var currByte = new Byte[1]; 
     var byteCounter = socket.Receive(currByte, currByte.Length, SocketFlags.None); 

     if (byteCounter.Equals(1)) 
     { 
      buffer.Add(currByte[0]); 
     } 
    } 

    return buffer.ToArray(); 
} 
関連する問題