原始套接字原码
来源:互联网 发布:淘宝提高动态评分 编辑:程序博客网 时间:2024/04/29 12:48
原始套接字类,参数类
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ReceiveAll
...{
[StructLayout(LayoutKind.Explicit)]
public struct IPHeader
...{
[FieldOffset(0)] public byte ip_verlen; //I4位首部长度+4位IP版本号
[FieldOffset(1)] public byte ip_tos; //8位服务类型TOS
[FieldOffset(2)] public ushort ip_totallength; //16位数据包总长度(字节)
[FieldOffset(4)] public ushort ip_id; //16位标识
[FieldOffset(6)] public ushort ip_offset; //3位标志位
[FieldOffset(8)] public byte ip_ttl; //8位生存时间 TTL
[FieldOffset(9)] public byte ip_protocol; //8位协议(TCP, UDP, ICMP, Etc.)
[FieldOffset(10)] public ushort ip_checksum; //16位IP首部校验和
[FieldOffset(12)] public uint ip_srcaddr; //32位源IP地址
[FieldOffset(16)] public uint ip_destaddr; //32位目的IP地址
}
public class RawSocket
...{
private bool error_occurred;//是否产生错误
public bool KeepRunning;//是否继续进行
private static int len_receive_buf;//得到的数据流的长度
byte [] receive_buf_bytes;//收到的字节
private Socket socket = null; //声明套接字
const int SIO_R = unchecked((int)0x98000001);//监听所有的数据包
const int SIO_1=unchecked((int)0x98000002);
const int SIO_2=unchecked((int)0x98000003);
public RawSocket()//构造函数
...{
error_occurred=false;
len_receive_buf = 4096;//4k
receive_buf_bytes = new byte[len_receive_buf];
}
public void CreateAndBindSocket(string IP)//建立并绑定套接字
...{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Blocking = false;//置socket非阻塞状态
socket.Bind(new IPEndPoint(IPAddress.Parse(IP), 0));
if (SetSocketOption()==false) error_occurred=true;
}
public void Shutdown()
...{
if(socket != null)
...{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
private bool SetSocketOption()//
...{
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_R, IN, OUT);//低级别操作模式
ret_code = OUT[0] + OUT[1] + OUT[2] + OUT[3];//把4个8位字节合成一个32位整数
System.Windows.Forms.MessageBox.Show(ret_code.ToString());
if(ret_code != 0) ret_value = false;
}
catch(SocketException)
...{
ret_value = false;
}
return ret_value;
}
---SetSocketOption简介---#region---SetSocketOption简介---
// 其中,设置套接字选项时必须使套接字包含IP包头,否则无法填充IPHeader结构,也无法获得数据包信息。 int ret_code = socket.IOControl(SIO_RCVALL, IN, OUT);是函数中最关键的一步了,因为,在windows 中我们不能用Receive函数来接收raw socket上的数据,这是因为,所有的IP包都是先递交给系统核心,然后再传输到用户程序,当发送一个raws socket包的时候(比如syn),核心并不知道,也没有这个数据被发送或者连接建立的记录,因此,当远端主机回应的时候,系统核心就把这些包都全部丢掉,从而到不了应用程序上。所以,就不能简单地使用接收函数来接收这些数据报。要达到接收数据的目的,就必须采用嗅探,接收所有通过的数据包,然后进行筛选,留下符合我们需要的。可以通过设置SIO_RCVALL,表示接收所有网络上的数据包。接下来介绍一下IOControl函数。MSDN解释它说是设置套接字为低级别操作模式,怎么低级别操作法?其实这个函数与API中的WSAIoctl函数很相似。WSAIoctl函数定义如下:
// int WSAIoctl(
// SOCKET s, //一个指定的套接字
// DWORD dwIoControlCode, //控制操作码
// LPVOID lpvInBuffer, //指向输入数据流的指针
// DWORD cbInBuffer, //输入数据流的大小(字节数)
// LPVOID lpvOutBuffer, // 指向输出数据流的指针
// DWORD cbOutBuffer, //输出数据流的大小(字节数)
// LPDWORD lpcbBytesReturned, //指向输出字节流数目的实数值
// LPWSAOVERLAPPED lpOverlapped, //指向一个WSAOVERLAPPED结构
// LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine//指向操作完成时执行的例程
// );
// C#的IOControl函数不像WSAIoctl函数那么复杂,其中只包括其中的控制操作码、输入字节流、输出字节流三个参数,不过这三个参数已经足够了。我们看到函数中定义了一个字节数组:byte []IN = new byte[4]{1, 0, 0, 0}实际上它是一个值为1的DWORD或是Int32,同样byte []OUT = new byte[4];也是,它整和了一个int,作为WSAIoctl函数中参数lpcbBytesReturned指向的值。因为设置套接字选项时可能会发生错误,需要用一个值传递错误标志:
#endregion
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;
//return;
PacketArrivedEventArgs e=new PacketArrivedEventArgs();
fixed(byte *fixed_buf = buf)
...{
IPHeader * head = (IPHeader *) fixed_buf;
e.HeaderLength=(uint)(head->ip_verlen & 0x0F) << 2;//4位首部长度
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;//4位IP版本号
e.IPVersion = temp_version.ToString();
temp_ip_srcaddr = head->ip_srcaddr;
temp_ip_destaddr = head->ip_destaddr;
temp_ip = new IPAddress(temp_ip_srcaddr);
e.OriginationAddress =temp_ip.ToString();//IP
temp_ip = new IPAddress(temp_ip_destaddr);
e.DestinationAddress = temp_ip.ToString();//IP
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();
// if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000)
// {
// return;
// }
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);//received_bytes接收字节数
Receive(receive_buf_bytes, received_bytes);//接收字节
if (KeepRunning) Run();
}
//事件参数
public class PacketArrivedEventArgs : EventArgs
...{
private string protocol;
private string destination_port;
private string origination_port;
private string destination_address;
private string origination_address;
private string ip_version;
private uint total_packet_length;
private uint message_length;
private uint header_length;
private byte []receive_buf_bytes = null;
private byte []ip_header_bytes = null;
private byte []message_bytes = null;
public PacketArrivedEventArgs()
...{
this.protocol = "";
this.destination_port = "";
this.origination_port = "";
this.destination_address = "";
this.origination_address = "";
this.ip_version = "";
this.total_packet_length =0;
this.message_length =0;
this.header_length =0;
this.receive_buf_bytes=new byte[len_receive_buf];
this.ip_header_bytes=new byte[len_receive_buf];
this.message_bytes=new byte[len_receive_buf];
}
public string Protocol
...{
get ...{return protocol;}
set ...{protocol=value;}
}
public string DestinationPort
...{
get ...{return destination_port;}
set ...{destination_port=value;}
}
public string OriginationPort
...{
get ...{return origination_port;}
set ...{origination_port=value;}
}
public string DestinationAddress
...{
get ...{return destination_address;}
set ...{destination_address=value;}
}
public string OriginationAddress
...{
get ...{return origination_address;}
set ...{origination_address=value;}
}
public string IPVersion
...{
get ...{return ip_version;}
set ...{ip_version=value;}
}
public uint PacketLength
...{
get ...{return total_packet_length;}
set ...{total_packet_length=value;}
}
public uint MessageLength
...{
get ...{return message_length;}
set ...{message_length=value;}
}
public uint HeaderLength
...{
get ...{return header_length;}
set ...{header_length=value;}
}
public byte [] ReceiveBuffer
...{
get ...{return receive_buf_bytes;}
set ...{receive_buf_bytes=value;}
}
public byte [] IPHeaderBuffer
...{
get ...{return ip_header_bytes;}
set ...{ip_header_bytes=value;}
}
public byte [] MessageBuffer
...{
get ...{return message_bytes;}
set ...{message_bytes=value;}
}
}
public delegate void PacketArrivedEventHandler(Object sender, PacketArrivedEventArgs args);
public event PacketArrivedEventHandler PacketArrival;
protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
...{
if (PacketArrival != null)
...{
PacketArrival(this, e);
}
}
}
}
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ReceiveAll
...{
[StructLayout(LayoutKind.Explicit)]
public struct IPHeader
...{
[FieldOffset(0)] public byte ip_verlen; //I4位首部长度+4位IP版本号
[FieldOffset(1)] public byte ip_tos; //8位服务类型TOS
[FieldOffset(2)] public ushort ip_totallength; //16位数据包总长度(字节)
[FieldOffset(4)] public ushort ip_id; //16位标识
[FieldOffset(6)] public ushort ip_offset; //3位标志位
[FieldOffset(8)] public byte ip_ttl; //8位生存时间 TTL
[FieldOffset(9)] public byte ip_protocol; //8位协议(TCP, UDP, ICMP, Etc.)
[FieldOffset(10)] public ushort ip_checksum; //16位IP首部校验和
[FieldOffset(12)] public uint ip_srcaddr; //32位源IP地址
[FieldOffset(16)] public uint ip_destaddr; //32位目的IP地址
}
public class RawSocket
...{
private bool error_occurred;//是否产生错误
public bool KeepRunning;//是否继续进行
private static int len_receive_buf;//得到的数据流的长度
byte [] receive_buf_bytes;//收到的字节
private Socket socket = null; //声明套接字
const int SIO_R = unchecked((int)0x98000001);//监听所有的数据包
const int SIO_1=unchecked((int)0x98000002);
const int SIO_2=unchecked((int)0x98000003);
public RawSocket()//构造函数
...{
error_occurred=false;
len_receive_buf = 4096;//4k
receive_buf_bytes = new byte[len_receive_buf];
}
public void CreateAndBindSocket(string IP)//建立并绑定套接字
...{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Blocking = false;//置socket非阻塞状态
socket.Bind(new IPEndPoint(IPAddress.Parse(IP), 0));
if (SetSocketOption()==false) error_occurred=true;
}
public void Shutdown()
...{
if(socket != null)
...{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
private bool SetSocketOption()//
...{
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_R, IN, OUT);//低级别操作模式
ret_code = OUT[0] + OUT[1] + OUT[2] + OUT[3];//把4个8位字节合成一个32位整数
System.Windows.Forms.MessageBox.Show(ret_code.ToString());
if(ret_code != 0) ret_value = false;
}
catch(SocketException)
...{
ret_value = false;
}
return ret_value;
}
---SetSocketOption简介---#region---SetSocketOption简介---
// 其中,设置套接字选项时必须使套接字包含IP包头,否则无法填充IPHeader结构,也无法获得数据包信息。 int ret_code = socket.IOControl(SIO_RCVALL, IN, OUT);是函数中最关键的一步了,因为,在windows 中我们不能用Receive函数来接收raw socket上的数据,这是因为,所有的IP包都是先递交给系统核心,然后再传输到用户程序,当发送一个raws socket包的时候(比如syn),核心并不知道,也没有这个数据被发送或者连接建立的记录,因此,当远端主机回应的时候,系统核心就把这些包都全部丢掉,从而到不了应用程序上。所以,就不能简单地使用接收函数来接收这些数据报。要达到接收数据的目的,就必须采用嗅探,接收所有通过的数据包,然后进行筛选,留下符合我们需要的。可以通过设置SIO_RCVALL,表示接收所有网络上的数据包。接下来介绍一下IOControl函数。MSDN解释它说是设置套接字为低级别操作模式,怎么低级别操作法?其实这个函数与API中的WSAIoctl函数很相似。WSAIoctl函数定义如下:
// int WSAIoctl(
// SOCKET s, //一个指定的套接字
// DWORD dwIoControlCode, //控制操作码
// LPVOID lpvInBuffer, //指向输入数据流的指针
// DWORD cbInBuffer, //输入数据流的大小(字节数)
// LPVOID lpvOutBuffer, // 指向输出数据流的指针
// DWORD cbOutBuffer, //输出数据流的大小(字节数)
// LPDWORD lpcbBytesReturned, //指向输出字节流数目的实数值
// LPWSAOVERLAPPED lpOverlapped, //指向一个WSAOVERLAPPED结构
// LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine//指向操作完成时执行的例程
// );
// C#的IOControl函数不像WSAIoctl函数那么复杂,其中只包括其中的控制操作码、输入字节流、输出字节流三个参数,不过这三个参数已经足够了。我们看到函数中定义了一个字节数组:byte []IN = new byte[4]{1, 0, 0, 0}实际上它是一个值为1的DWORD或是Int32,同样byte []OUT = new byte[4];也是,它整和了一个int,作为WSAIoctl函数中参数lpcbBytesReturned指向的值。因为设置套接字选项时可能会发生错误,需要用一个值传递错误标志:
#endregion
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;
//return;
PacketArrivedEventArgs e=new PacketArrivedEventArgs();
fixed(byte *fixed_buf = buf)
...{
IPHeader * head = (IPHeader *) fixed_buf;
e.HeaderLength=(uint)(head->ip_verlen & 0x0F) << 2;//4位首部长度
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;//4位IP版本号
e.IPVersion = temp_version.ToString();
temp_ip_srcaddr = head->ip_srcaddr;
temp_ip_destaddr = head->ip_destaddr;
temp_ip = new IPAddress(temp_ip_srcaddr);
e.OriginationAddress =temp_ip.ToString();//IP
temp_ip = new IPAddress(temp_ip_destaddr);
e.DestinationAddress = temp_ip.ToString();//IP
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();
// if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000)
// {
// return;
// }
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);//received_bytes接收字节数
Receive(receive_buf_bytes, received_bytes);//接收字节
if (KeepRunning) Run();
}
//事件参数
public class PacketArrivedEventArgs : EventArgs
...{
private string protocol;
private string destination_port;
private string origination_port;
private string destination_address;
private string origination_address;
private string ip_version;
private uint total_packet_length;
private uint message_length;
private uint header_length;
private byte []receive_buf_bytes = null;
private byte []ip_header_bytes = null;
private byte []message_bytes = null;
public PacketArrivedEventArgs()
...{
this.protocol = "";
this.destination_port = "";
this.origination_port = "";
this.destination_address = "";
this.origination_address = "";
this.ip_version = "";
this.total_packet_length =0;
this.message_length =0;
this.header_length =0;
this.receive_buf_bytes=new byte[len_receive_buf];
this.ip_header_bytes=new byte[len_receive_buf];
this.message_bytes=new byte[len_receive_buf];
}
public string Protocol
...{
get ...{return protocol;}
set ...{protocol=value;}
}
public string DestinationPort
...{
get ...{return destination_port;}
set ...{destination_port=value;}
}
public string OriginationPort
...{
get ...{return origination_port;}
set ...{origination_port=value;}
}
public string DestinationAddress
...{
get ...{return destination_address;}
set ...{destination_address=value;}
}
public string OriginationAddress
...{
get ...{return origination_address;}
set ...{origination_address=value;}
}
public string IPVersion
...{
get ...{return ip_version;}
set ...{ip_version=value;}
}
public uint PacketLength
...{
get ...{return total_packet_length;}
set ...{total_packet_length=value;}
}
public uint MessageLength
...{
get ...{return message_length;}
set ...{message_length=value;}
}
public uint HeaderLength
...{
get ...{return header_length;}
set ...{header_length=value;}
}
public byte [] ReceiveBuffer
...{
get ...{return receive_buf_bytes;}
set ...{receive_buf_bytes=value;}
}
public byte [] IPHeaderBuffer
...{
get ...{return ip_header_bytes;}
set ...{ip_header_bytes=value;}
}
public byte [] MessageBuffer
...{
get ...{return message_bytes;}
set ...{message_bytes=value;}
}
}
public delegate void PacketArrivedEventHandler(Object sender, PacketArrivedEventArgs args);
public event PacketArrivedEventHandler PacketArrival;
protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
...{
if (PacketArrival != null)
...{
PacketArrival(this, e);
}
}
}
}
实现代码
//Form1.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Text;
namespace ReceiveAll
...{
public class MainForm : Form
...{
RawSocket myRawSock;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
string IPs;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label17;
private System.Windows.Forms.Label label18;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label19;
private System.Windows.Forms.Label label20;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.ComponentModel.Container components = null;
public MainForm()
...{
IPAddress ipa;
InitializeComponent();
this.AutoScale=true;
String host=Dns.GetHostName();//获得主机名
IPHostEntry iph=Dns.Resolve(host);//解析主机地址
ipa=iph.AddressList[0];//解析主机ip
IPs=ipa.ToString();
textBox2.Text=IPs;
textBox3.Text=host;
}
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if(components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
...{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label17 = new System.Windows.Forms.Label();
this.label18 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label19 = new System.Windows.Forms.Label();
this.label20 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button1.Location = new System.Drawing.Point(8, 536);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 24);
this.button1.TabIndex = 1;
this.button1.Text = "&Start";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(128, 536);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 24);
this.button2.TabIndex = 2;
this.button2.Text = "s&Top";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.textBox1.Location = new System.Drawing.Point(0, 40);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(810, 208);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(0, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 16);
this.label1.TabIndex = 4;
this.label1.Text = "源IP";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.Control;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(136, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 5;
this.label2.Text = "源端口";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.Control;
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Location = new System.Drawing.Point(360, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 16);
this.label4.TabIndex = 7;
this.label4.Text = "目的端口";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label5
//
this.label5.BackColor = System.Drawing.SystemColors.Control;
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Location = new System.Drawing.Point(224, 24);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(136, 16);
this.label5.TabIndex = 8;
this.label5.Text = "目的IP";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label6
//
this.label6.BackColor = System.Drawing.SystemColors.Control;
this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label6.Location = new System.Drawing.Point(456, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(80, 16);
this.label6.TabIndex = 9;
this.label6.Text = "协议";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.Control;
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label3.Location = new System.Drawing.Point(536, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 16);
this.label3.TabIndex = 10;
this.label3.Text = "包长";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label7
//
this.label7.BackColor = System.Drawing.SystemColors.Control;
this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label7.Location = new System.Drawing.Point(624, 24);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(136, 16);
this.label7.TabIndex = 11;
this.label7.Text = "IP头长";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label8
//
this.label8.BackColor = System.Drawing.SystemColors.Control;
this.label8.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label8.Location = new System.Drawing.Point(760, 24);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(48, 16);
this.label8.TabIndex = 12;
this.label8.Text = "IP版本";
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox2
//
this.textBox2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox2.Location = new System.Drawing.Point(428, 536);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 13;
this.textBox2.Text = "";
//
// label9
//
this.label9.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label9.Location = new System.Drawing.Point(372, 536);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(48, 16);
this.label9.TabIndex = 14;
this.label9.Text = "本机IP:";
//
// label10
//
this.label10.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label10.Location = new System.Drawing.Point(564, 536);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(72, 16);
this.label10.TabIndex = 15;
this.label10.Text = "本机标识:";
//
// textBox3
//
this.textBox3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox3.Location = new System.Drawing.Point(636, 536);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(168, 21);
this.textBox3.TabIndex = 16;
this.textBox3.Text = "";
//
// label11
//
this.label11.BackColor = System.Drawing.SystemColors.Control;
this.label11.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label11.Location = new System.Drawing.Point(760, 280);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(48, 16);
this.label11.TabIndex = 25;
this.label11.Text = "IP版本";
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label12
//
this.label12.BackColor = System.Drawing.SystemColors.Control;
this.label12.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label12.Location = new System.Drawing.Point(624, 280);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(136, 16);
this.label12.TabIndex = 24;
this.label12.Text = "IP头长";
this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label13
//
this.label13.BackColor = System.Drawing.SystemColors.Control;
this.label13.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label13.Location = new System.Drawing.Point(536, 280);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(88, 16);
this.label13.TabIndex = 23;
this.label13.Text = "包长";
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label14
//
this.label14.BackColor = System.Drawing.SystemColors.Control;
this.label14.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label14.Location = new System.Drawing.Point(456, 280);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(80, 16);
this.label14.TabIndex = 22;
this.label14.Text = "协议";
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label15
//
this.label15.BackColor = System.Drawing.SystemColors.Control;
this.label15.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label15.Location = new System.Drawing.Point(224, 280);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(136, 16);
this.label15.TabIndex = 21;
this.label15.Text = "目的IP";
this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label16
//
this.label16.BackColor = System.Drawing.SystemColors.Control;
this.label16.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label16.Location = new System.Drawing.Point(360, 280);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(96, 16);
this.label16.TabIndex = 20;
this.label16.Text = "目的端口";
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label17
//
this.label17.BackColor = System.Drawing.SystemColors.Control;
this.label17.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label17.Location = new System.Drawing.Point(136, 280);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(88, 16);
this.label17.TabIndex = 19;
this.label17.Text = "源端口";
this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label18
//
this.label18.BackColor = System.Drawing.SystemColors.Control;
this.label18.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label18.Location = new System.Drawing.Point(0, 280);
this.label18.Name = "label18";
this.label18.Size = new System.Drawing.Size(136, 16);
this.label18.TabIndex = 18;
this.label18.Text = "源IP";
this.label18.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox4
//
this.textBox4.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.textBox4.Location = new System.Drawing.Point(0, 296);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox4.Size = new System.Drawing.Size(810, 208);
this.textBox4.TabIndex = 17;
this.textBox4.Text = "";
//
// label19
//
this.label19.Location = new System.Drawing.Point(0, 8);
this.label19.Name = "label19";
this.label19.Size = new System.Drawing.Size(168, 16);
this.label19.TabIndex = 26;
this.label19.Text = "收到的包:";
//
// label20
//
this.label20.Location = new System.Drawing.Point(0, 264);
this.label20.Name = "label20";
this.label20.Size = new System.Drawing.Size(168, 16);
this.label20.TabIndex = 27;
this.label20.Text = "发出的包:";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(816, 24);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox5.Size = new System.Drawing.Size(176, 224);
this.textBox5.TabIndex = 28;
this.textBox5.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(816, 280);
this.textBox6.Multiline = true;
this.textBox6.Name = "textBox6";
this.textBox6.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox6.Size = new System.Drawing.Size(176, 224);
this.textBox6.TabIndex = 29;
this.textBox6.Text = "";
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(248, 536);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 24);
this.button3.TabIndex = 30;
this.button3.Text = "&Clean";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(696, 248);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(112, 24);
this.button4.TabIndex = 31;
this.button4.Text = "c&Lean";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(994, 561);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label20);
this.Controls.Add(this.label19);
this.Controls.Add(this.label11);
this.Controls.Add(this.label12);
this.Controls.Add(this.label13);
this.Controls.Add(this.label14);
this.Controls.Add(this.label15);
this.Controls.Add(this.label16);
this.Controls.Add(this.label17);
this.Controls.Add(this.label18);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label3);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "原始套接字";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private void MainForm_Load(object sender, System.EventArgs e)
...{
string IPString="10.10.10.10";
IPHostEntry HosyEntry = Dns.Resolve(Dns.GetHostName());
if(HosyEntry.AddressList.Length > 0)
...{
foreach(IPAddress ip in HosyEntry.AddressList)
...{
IPString=ip.ToString();
}
}
//原始套接字
myRawSock=new RawSocket();
myRawSock.CreateAndBindSocket (IPString);
myRawSock.PacketArrival += new RawSocket.PacketArrivedEventHandler(DataArrival);
}
//事件
private void DataArrival(Object sender, RawSocket.PacketArrivedEventArgs e)
...{
textBox1.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// string s=null;
// byte[] sByte=new byte[e.MessageBuffer.Length];
//
// if(e.OriginationAddress.Equals(IPs))
// {
// textBox4.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// for(int i=0;i<e.MessageBuffer.Length;i++)
/**///// sByte[i]=e.MessageBuffer[i];
// s+=e.MessageBuffer[i].ToString();
/**///// s=Encoding.ASCII.GetString(e.MessageBuffer);
// textBox6.Text=s;
//
// }
// if(e.DestinationAddress.Equals(IPs))
// {
// textBox1.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// for(int i=0;i<e.MessageBuffer.Length;i++)
/**///// sByte[i]=e.MessageBuffer[i];
// s+=e.MessageBuffer[i].ToString();
/**///// s=Encoding.ASCII.GetString(e.MessageBuffer);
// textBox5.Text=s;
// while(true);
// }
}
//开始监听
private void button1_Click(object sender, System.EventArgs e)
...{
myRawSock.KeepRunning =true;
myRawSock.Run ();
}
//停止
private void button2_Click(object sender, System.EventArgs e)
...{
myRawSock.KeepRunning =false;
}
private void button3_Click(object sender, System.EventArgs e)
...{
textBox4.Text="";
}
private void button4_Click(object sender, System.EventArgs e)
...{
textBox1.Text="";
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Text;
namespace ReceiveAll
...{
public class MainForm : Form
...{
RawSocket myRawSock;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
string IPs;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label17;
private System.Windows.Forms.Label label18;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label19;
private System.Windows.Forms.Label label20;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.ComponentModel.Container components = null;
public MainForm()
...{
IPAddress ipa;
InitializeComponent();
this.AutoScale=true;
String host=Dns.GetHostName();//获得主机名
IPHostEntry iph=Dns.Resolve(host);//解析主机地址
ipa=iph.AddressList[0];//解析主机ip
IPs=ipa.ToString();
textBox2.Text=IPs;
textBox3.Text=host;
}
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if(components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
...{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label17 = new System.Windows.Forms.Label();
this.label18 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label19 = new System.Windows.Forms.Label();
this.label20 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button1.Location = new System.Drawing.Point(8, 536);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 24);
this.button1.TabIndex = 1;
this.button1.Text = "&Start";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(128, 536);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 24);
this.button2.TabIndex = 2;
this.button2.Text = "s&Top";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.textBox1.Location = new System.Drawing.Point(0, 40);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(810, 208);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(0, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 16);
this.label1.TabIndex = 4;
this.label1.Text = "源IP";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.Control;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(136, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 5;
this.label2.Text = "源端口";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.Control;
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Location = new System.Drawing.Point(360, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 16);
this.label4.TabIndex = 7;
this.label4.Text = "目的端口";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label5
//
this.label5.BackColor = System.Drawing.SystemColors.Control;
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label5.Location = new System.Drawing.Point(224, 24);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(136, 16);
this.label5.TabIndex = 8;
this.label5.Text = "目的IP";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label6
//
this.label6.BackColor = System.Drawing.SystemColors.Control;
this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label6.Location = new System.Drawing.Point(456, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(80, 16);
this.label6.TabIndex = 9;
this.label6.Text = "协议";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.Control;
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label3.Location = new System.Drawing.Point(536, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 16);
this.label3.TabIndex = 10;
this.label3.Text = "包长";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label7
//
this.label7.BackColor = System.Drawing.SystemColors.Control;
this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label7.Location = new System.Drawing.Point(624, 24);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(136, 16);
this.label7.TabIndex = 11;
this.label7.Text = "IP头长";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label8
//
this.label8.BackColor = System.Drawing.SystemColors.Control;
this.label8.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label8.Location = new System.Drawing.Point(760, 24);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(48, 16);
this.label8.TabIndex = 12;
this.label8.Text = "IP版本";
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox2
//
this.textBox2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox2.Location = new System.Drawing.Point(428, 536);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 13;
this.textBox2.Text = "";
//
// label9
//
this.label9.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label9.Location = new System.Drawing.Point(372, 536);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(48, 16);
this.label9.TabIndex = 14;
this.label9.Text = "本机IP:";
//
// label10
//
this.label10.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label10.Location = new System.Drawing.Point(564, 536);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(72, 16);
this.label10.TabIndex = 15;
this.label10.Text = "本机标识:";
//
// textBox3
//
this.textBox3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox3.Location = new System.Drawing.Point(636, 536);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(168, 21);
this.textBox3.TabIndex = 16;
this.textBox3.Text = "";
//
// label11
//
this.label11.BackColor = System.Drawing.SystemColors.Control;
this.label11.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label11.Location = new System.Drawing.Point(760, 280);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(48, 16);
this.label11.TabIndex = 25;
this.label11.Text = "IP版本";
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label12
//
this.label12.BackColor = System.Drawing.SystemColors.Control;
this.label12.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label12.Location = new System.Drawing.Point(624, 280);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(136, 16);
this.label12.TabIndex = 24;
this.label12.Text = "IP头长";
this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label13
//
this.label13.BackColor = System.Drawing.SystemColors.Control;
this.label13.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label13.Location = new System.Drawing.Point(536, 280);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(88, 16);
this.label13.TabIndex = 23;
this.label13.Text = "包长";
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label14
//
this.label14.BackColor = System.Drawing.SystemColors.Control;
this.label14.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label14.Location = new System.Drawing.Point(456, 280);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(80, 16);
this.label14.TabIndex = 22;
this.label14.Text = "协议";
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label15
//
this.label15.BackColor = System.Drawing.SystemColors.Control;
this.label15.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label15.Location = new System.Drawing.Point(224, 280);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(136, 16);
this.label15.TabIndex = 21;
this.label15.Text = "目的IP";
this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label16
//
this.label16.BackColor = System.Drawing.SystemColors.Control;
this.label16.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label16.Location = new System.Drawing.Point(360, 280);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(96, 16);
this.label16.TabIndex = 20;
this.label16.Text = "目的端口";
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label17
//
this.label17.BackColor = System.Drawing.SystemColors.Control;
this.label17.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label17.Location = new System.Drawing.Point(136, 280);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(88, 16);
this.label17.TabIndex = 19;
this.label17.Text = "源端口";
this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label18
//
this.label18.BackColor = System.Drawing.SystemColors.Control;
this.label18.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label18.Location = new System.Drawing.Point(0, 280);
this.label18.Name = "label18";
this.label18.Size = new System.Drawing.Size(136, 16);
this.label18.TabIndex = 18;
this.label18.Text = "源IP";
this.label18.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox4
//
this.textBox4.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.textBox4.Location = new System.Drawing.Point(0, 296);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox4.Size = new System.Drawing.Size(810, 208);
this.textBox4.TabIndex = 17;
this.textBox4.Text = "";
//
// label19
//
this.label19.Location = new System.Drawing.Point(0, 8);
this.label19.Name = "label19";
this.label19.Size = new System.Drawing.Size(168, 16);
this.label19.TabIndex = 26;
this.label19.Text = "收到的包:";
//
// label20
//
this.label20.Location = new System.Drawing.Point(0, 264);
this.label20.Name = "label20";
this.label20.Size = new System.Drawing.Size(168, 16);
this.label20.TabIndex = 27;
this.label20.Text = "发出的包:";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(816, 24);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox5.Size = new System.Drawing.Size(176, 224);
this.textBox5.TabIndex = 28;
this.textBox5.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(816, 280);
this.textBox6.Multiline = true;
this.textBox6.Name = "textBox6";
this.textBox6.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox6.Size = new System.Drawing.Size(176, 224);
this.textBox6.TabIndex = 29;
this.textBox6.Text = "";
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(248, 536);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 24);
this.button3.TabIndex = 30;
this.button3.Text = "&Clean";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(696, 248);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(112, 24);
this.button4.TabIndex = 31;
this.button4.Text = "c&Lean";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(994, 561);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label20);
this.Controls.Add(this.label19);
this.Controls.Add(this.label11);
this.Controls.Add(this.label12);
this.Controls.Add(this.label13);
this.Controls.Add(this.label14);
this.Controls.Add(this.label15);
this.Controls.Add(this.label16);
this.Controls.Add(this.label17);
this.Controls.Add(this.label18);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label3);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "原始套接字";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private void MainForm_Load(object sender, System.EventArgs e)
...{
string IPString="10.10.10.10";
IPHostEntry HosyEntry = Dns.Resolve(Dns.GetHostName());
if(HosyEntry.AddressList.Length > 0)
...{
foreach(IPAddress ip in HosyEntry.AddressList)
...{
IPString=ip.ToString();
}
}
//原始套接字
myRawSock=new RawSocket();
myRawSock.CreateAndBindSocket (IPString);
myRawSock.PacketArrival += new RawSocket.PacketArrivedEventHandler(DataArrival);
}
//事件
private void DataArrival(Object sender, RawSocket.PacketArrivedEventArgs e)
...{
textBox1.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// string s=null;
// byte[] sByte=new byte[e.MessageBuffer.Length];
//
// if(e.OriginationAddress.Equals(IPs))
// {
// textBox4.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// for(int i=0;i<e.MessageBuffer.Length;i++)
/**///// sByte[i]=e.MessageBuffer[i];
// s+=e.MessageBuffer[i].ToString();
/**///// s=Encoding.ASCII.GetString(e.MessageBuffer);
// textBox6.Text=s;
//
// }
// if(e.DestinationAddress.Equals(IPs))
// {
// textBox1.Text+=e.OriginationAddress+" "+e.OriginationPort+" "+e.DestinationAddress+" "+e.DestinationPort+" "+e.Protocol+" "+e.PacketLength+" "+e.HeaderLength+" "+e.IPVersion+" ";
// for(int i=0;i<e.MessageBuffer.Length;i++)
/**///// sByte[i]=e.MessageBuffer[i];
// s+=e.MessageBuffer[i].ToString();
/**///// s=Encoding.ASCII.GetString(e.MessageBuffer);
// textBox5.Text=s;
// while(true);
// }
}
//开始监听
private void button1_Click(object sender, System.EventArgs e)
...{
myRawSock.KeepRunning =true;
myRawSock.Run ();
}
//停止
private void button2_Click(object sender, System.EventArgs e)
...{
myRawSock.KeepRunning =false;
}
private void button3_Click(object sender, System.EventArgs e)
...{
textBox4.Text="";
}
private void button4_Click(object sender, System.EventArgs e)
...{
textBox1.Text="";
}
}
}
- 原始套接字原码
- 原始套接字类
- 原始套接字
- 原始套接字(服务器端)
- 学习--原始套接字
- 原始套接字
- 原始套接字发包
- 原始套接字 IP_HDRINCL
- Linux--原始套接字
- 原始套接字
- 原始套接字
- 原始套接字
- 原始套接字
- 原始套接字
- 原始套接字SOCK_RAW
- 原始套接字
- 原始套接字
- 原始套接字实现
- ListView点击列排序
- A byte order mark (BOM)
- 用xsl分类的方法
- 青春华年
- HeartBeat源代码学习
- 原始套接字原码
- 如何做Web的功能性测试
- 创建动态Web网页需要知道的10个服务器变量(翻译)
- 实用Web Service
- C#用栈来输出后缀表达式
- 修改数据问题
- 因为数据库正在使用,所以未能获得对数据库的排它访问权。RESTORE DATABASE 操作异常终止。
- HELLO WORLD!
- 中文版SPPS 2003安装手册