本文介绍了从一个网卡获取所有网络数据包并将其发送到第二个网卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的系统上有两张网卡。

我想写C#中的一个程序。

第一个网卡接收数据包并将它们发送到第二个网卡。

我写了一个接收第一个数据包网卡的程序。我写了一个程序代码:

Hi
I have two network cards on your system.
I want to write a program in C#.
The first network card receive the packets and sends them to a second network card.
I have written a program that receives the first packet network card.I have written a program code:

private Socket mainSocket;
        private byte[] byteData = new byte[4096];
        private bool bContinueCapturing = false;
 
        private delegate void AddTreeNode(TreeNode node);
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string strIP = null;
 
            IPHostEntry HosyEntry = Dns.GetHostEntry((Dns.GetHostName()));
            if (HosyEntry.AddressList.Length > 0)
            {
                foreach (IPAddress ip in HosyEntry.AddressList)
                {
                    strIP = ip.ToString();
                    cmbInterfaces.Items.Add(strIP);
                }
            }
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bContinueCapturing)
            {
                mainSocket.Close();
            }
        }
 
        private void btnStart_Click(object sender, EventArgs e)
        {
             
            if (cmbInterfaces.Text == "")
            {
                MessageBox.Show("Select an Interface to capture the packets.", "sniffer",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (!bContinueCapturing)
                {
 
                    btnStart.Text = "&Stop";
 
                    bContinueCapturing = true;
 
                    mainSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Raw, ProtocolType.IP);
 
                    mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));
 
                    mainSocket.SetSocketOption(SocketOptionLevel.IP,
                                               SocketOptionName.HeaderIncluded,
                                               true);
 
                    byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                    byte[] byOut = new byte[4] { 1, 0, 0, 0 };
 
                    mainSocket.IOControl(IOControlCode.ReceiveAll,
                                         byTrue,
                                         byOut);
                     
                    mainSocket.Receive(byteData);
                     
                    mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                else
                {
                    btnStart.Text = "&Start";
                    bContinueCapturing = false;
                    mainSocket.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "sniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
        private void OnReceive(IAsyncResult ar)
        {
             
            try
            {
                 
                int nReceived = mainSocket.EndReceive(ar);
 
                if (bContinueCapturing)
                {
                    byteData = new byte[4096];
                    mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), null);
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "sniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 
    }



我想编写一个程序来发送收到第二张网卡的数据包。

请帮帮我。


I want to write a program that sends packets received a second network card.
Please help me.

推荐答案

var localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.49"), port: 0);
var tcpSendClient = new TcpClient(localEndPoint);
tcpSendClient.Connect("192.168.2.100", 12345);





在端点上使用'端口0'表示192.168.2.49表示输出/出站端口是动态的 - 通过指定端点192.168.2.49绕过自动路由



by using 'port 0' in the Endpoint for the 192.168.2.49 means the output/outbound port is dynamic - by specifying the endpoint 192.168.2.49 you circumvent automatic routing


这篇关于从一个网卡获取所有网络数据包并将其发送到第二个网卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:35