110,566
社区成员
发帖
与我相关
我的任务
分享
socket.BeginReceive(buf, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this);
/// <summary>
/// 根据数据包内容设置各字段
/// </summary>
/// <param name="buf"></param>
public void SetArgs(byte[] buf)
{
if( buf == null )
{
return;
}
string tempReceiveString = BitConverter.ToString( buf );
m_PackageLength = Convert.ToInt32( tempReceiveString.Substring(6, 2), 16 ) * 16 * 16
+ Convert.ToInt32( tempReceiveString.Substring(9, 2), 16 );
string receiveString = tempReceiveString.Substring(0, m_PackageLength * 3 - 1);
m_IPHeaderLength = Convert.ToInt32( receiveString.Substring(1, 1), 16 ) * 4;
m_IPHeaderContent = receiveString.Substring( 0, m_IPHeaderLength * 3 - 1 );
m_IPVersion = m_IPHeaderContent.Substring( 0, 1 );
string protocol = m_IPHeaderContent.Substring(27, 2);
switch(protocol)
{
case "01":
m_Protocol = "ICMP";
break;
case "02":
m_Protocol = "IGMP";
break;
case "06":
m_Protocol = "TCP";
break;
case "11":
m_Protocol = "UDP";
break;
default:
m_Protocol = "OTHER";
break;
}
string originationIPAddress = m_IPHeaderContent.Substring( 36, 11 );
m_OriginationIPAddress = getIPAddress( originationIPAddress );
string destinationIPAddress = m_IPHeaderContent.Substring( 48, 11 );
m_DestinationIPAddress = getIPAddress( destinationIPAddress );
m_MessageLength = m_PackageLength - m_IPHeaderLength;
m_MessageContent = receiveString.Substring( m_IPHeaderLength * 3 );
try // 这两个字段没用到,但读出来的TCPHeaderLength有可能为空,之后读内容就会因为长度为-1而报错。具体原因待查
{
m_TCPHeaderLength = Convert.ToInt32( m_MessageContent.Substring(36, 1), 16 ) * 4;
m_TCPHeaderContent = m_MessageContent.Substring( 0, m_TCPHeaderLength * 3 - 1 );
}
catch
{
}
m_OriginationPort = ( Convert.ToUInt32( m_MessageContent.Substring(0, 2), 16 ) * 16 * 16
+ Convert.ToUInt32( m_MessageContent.Substring(3, 2), 16 ) ).ToString();
m_DestinationPort = ( Convert.ToUInt32( m_MessageContent.Substring(6, 2), 16 ) * 16 * 16
+ Convert.ToUInt32( m_MessageContent.Substring(9, 2), 16 ) ).ToString();
}