扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:佚名 来源:CSDN 2007年11月13日
关键字:
private bool SetSocketOption() //设置raw socket { bool ret_value = true; try { socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1); byte []IN = new byte[4]{1, 0, 0, 0}; byte []OUT = new byte[4]; //低级别操作模式,接受所有的数据包,这一步是关键,必须把socket设成raw和IP Level才可用SIO_RCVALL int ret_code = socket.IOControl(SIO_RCVALL, IN, OUT); ret_code = OUT[0] + OUT[1] + OUT[2] + OUT[3];//把4个8位字节合成一个32位整数 if(ret_code != 0) ret_value = false; } catch(SocketException) { ret_value = false; } return ret_value; } |
int WSAIoctl( SOCKET s, //一个指定的套接字 DWORD dwIoControlCode, //控制操作码 LPVOID lpvInBuffer, //指向输入数据流的指针 DWORD cbInBuffer, //输入数据流的大小(字节数) LPVOID lpvOutBuffer, // 指向输出数据流的指针 DWORD cbOutBuffer, //输出数据流的大小(字节数) LPDWORD lpcbBytesReturned, //指向输出字节流数目的实数值 LPWSAOVERLAPPED lpOverlapped, //指向一个WSAOVERLAPPED结构 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine//指向操作完成时执行的例程 ); |
public bool ErrorOccurred { get { return error_occurred; } } |
//解析接收的数据包,形成PacketArrivedEventArgs事件数据类对象,并引发PacketArrival事件 unsafe private void Receive(byte [] buf, int len) { byte temp_protocol=0; uint temp_version=0; uint temp_ip_srcaddr=0; uint temp_ip_destaddr=0; short temp_srcport=0; short temp_dstport=0; IPAddress temp_ip; PacketArrivedEventArgs e=new PacketArrivedEventArgs();//新网络数据包信息事件 fixed(byte *fixed_buf = buf) { IPHeader * head = (IPHeader *) fixed_buf;//把数据流整和为IPHeader结构 e.HeaderLength=(uint)(head->ip_verlen & 0x0F) << 2; temp_protocol = head->ip_protocol; switch(temp_protocol)//提取协议类型 { case 1: e.Protocol="ICMP"; break; case 2: e.Protocol="IGMP"; break; case 6: e.Protocol="TCP"; break; case 17: e.Protocol="UDP"; break; default: e.Protocol= "UNKNOWN"; break; } temp_version =(uint)(head->ip_verlen & 0xF0) >> 4;//提取IP协议版本 e.IPVersion = temp_version.ToString(); //以下语句提取出了PacketArrivedEventArgs对象中的其他参数 temp_ip_srcaddr = head->ip_srcaddr; temp_ip_destaddr = head->ip_destaddr; temp_ip = new IPAddress(temp_ip_srcaddr); e.OriginationAddress =temp_ip.ToString(); temp_ip = new IPAddress(temp_ip_destaddr); e.DestinationAddress = temp_ip.ToString(); temp_srcport = *(short *)&fixed_buf[e.HeaderLength]; temp_dstport = *(short *)&fixed_buf[e.HeaderLength+2]; e.OriginationPort=IPAddress.NetworkToHostOrder(temp_srcport).ToString(); e.DestinationPort=IPAddress.NetworkToHostOrder(temp_dstport).ToString(); e.PacketLength =(uint)len; e.MessageLength =(uint)len - e.HeaderLength; e.ReceiveBuffer=buf; //把buf中的IP头赋给PacketArrivedEventArgs中的IPHeaderBuffer Array.Copy(buf,0,e.IPHeaderBuffer,0,(int)e.HeaderLength); //把buf中的包中内容赋给PacketArrivedEventArgs中的MessageBuffer Array.Copy(buf,(int)e.HeaderLength,e.MessageBuffer,0,(int)e.MessageLength); } //引发PacketArrival事件 OnPacketArrival(e); } |
public void Run() //开始监听 { IAsyncResult ar = socket.BeginReceive(receive_buf_bytes, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this); } |
private void CallReceive(IAsyncResult ar)//异步回调 { int received_bytes; received_bytes = socket.EndReceive(ar); Receive(receive_buf_bytes, received_bytes); if (KeepRunning) Run(); } |
public delegate void PacketArrivedEventHandler(Object sender, PacketArrivedEventArgs args); //事件句柄:包到达时引发事件 public event PacketArrivedEventHandler PacketArrival;//声明时间句柄函数 |
public void Shutdown() //关闭raw socket { if(socket != null) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者