日期:2014-05-17  浏览次数:20895 次

C#能不能直接获取IP数据包

class Server
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Server is running ... ");
            IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
            TcpListener listener = new TcpListener(ip, 8500);

            listener.Start();           // 开始侦听
            Console.WriteLine("Start Listening ...");

            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
        }
    }
这段代码是在网上找的,我想问能不能直接在里面加上一些代码获取到服务器端收到的IP数据包?

------解决方案--------------------
当然可以,不过是知道端口的TCP包,你上面代码只有一半,只建立连接。没有收包的,看看msdn的TCP Socket通信代码
------解决方案--------------------
Winpcap库可以。
------解决方案--------------------
winpcap比较常用
http://pcapdotnet.codeplex.com/
http://www.codeproject.com/Articles/4217/Packet-Sniffing-with-Winpcap-Functions-Ported-to-a
http://www.codeproject.com/Articles/17031/A-Network-Sniffer-in-C
http://www.codeproject.com/Articles/8254/Network-Sniffer-and-Connection-Analyzer

raw socket也可以
------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/system.net.sockets.tcplistener.accepttcpclient(v=vs.100).aspx