《Microsoft Visual C# .NET 2003开发技巧大全》源代码
源代码在线查看: 15.3.txt
Listing 15.3 Receiving Client Data
public static string ReceiveData( Socket client )
{
string data = “”;
int bytesRecvd = 0;
int totalBytes = 0;
byte[] recvData = new byte[1024]; // 1k packet size
do
{
bytesRecvd = client.Receive( recvData, 0,
1024, SocketFlags.None );
if( bytesRecvd > 0 )
{
data += Encoding.ASCII.GetString( recvData );
totalBytes += bytesRecvd;
}
} while( bytesRecvd == 1024 );
return data;
}
// methods to break apart protocol messages
// extracts the numerical identifier at beginning of message
public static int GetResponseCode( string response )
{
string code = response.Substring( 0, response.IndexOf( ‘\r’ ));
return Int32.Parse( code );
}
// extracts extra message data
public static string GetResponseData( string response )
{
return response.Substring( response.IndexOf(‘\n’)+1,
response.LastIndexOf(“\r\n\r\n”)-response.IndexOf(‘\n’)+1 );
}