BenVlodgi

Send and Receive Byte Data extension methods

Feb 7th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1.         /// <summary>Receives the byte data over the socket.</summary>
  2.         /// <param name="s">The socket to receive data over.</param>
  3.         /// <returns>The received data</returns>
  4.         public static byte[] ReceiveByteData(this Socket s)
  5.         {
  6.             int total = 0;
  7.             int recv;
  8.             byte[] datasize = new byte[4];
  9.  
  10.             recv = s.Receive(datasize, 0, 4, 0);
  11.             int size = BitConverter.ToInt32(datasize, 0);
  12.             int dataleft = size;
  13.             byte[] data = new byte[size];
  14.  
  15.             while (total < size)
  16.             {
  17.                 recv = s.Receive(data, total, dataleft, 0);
  18.                 if (recv == 0)
  19.                 {
  20.                     break;
  21.                 }
  22.                 total += recv;
  23.                 dataleft -= recv;
  24.             }
  25.             return data;
  26.         }
Advertisement
Add Comment
Please, Sign In to add comment