写端口监听器

时间:2012-03-03 11:27:57

标签: c# sockets

我写了一个类似嗅探器的应用程序,它可以在端口上侦听并向用户显示在您的计算机和互联网之间传输的信息,如源和目标IP。

我怎样才能将其更改为检索url地址而不是ip地址是否可能? 我使用套接字。

private byte[] byUDPData = new byte[4096];  //Data carried by the UDP packet

    public UDPHeader(byte [] byBuffer, int nReceived)
    {
        MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived);
        BinaryReader binaryReader = new BinaryReader(memoryStream);

        //The first sixteen bits contain the source port
        usSourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the destination port
        usDestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the length of the UDP packet
        usLength = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the checksum
        sChecksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());            

        //Copy the data carried by the UDP packet into the data buffer
        Array.Copy(byBuffer, 
                   8,              
                   byUDPData, 
                   0, 
                   nReceived - 8);
    }

2 个答案:

答案 0 :(得分:1)

通常没有必要这样做。看看Wire Shark。如果找不到您需要的内容,您可以编写自己的协议分析程序和其他加载项。

另外,请查看WinPCap,它可让您的应用程序访问Wire Shark的相同嗅探功能。

答案 1 :(得分:0)

当你说

  

url地址而不是ip地址

你是说dns的名字吗?在这种情况下,您需要使用dns类和Dns.GetHostEntry方法从IP地址中查找名称。

如果您正在寻找网址信息,那么正如@David Schwartz所说,您将不得不解码您捕获的所有数据,其中可能包含或可能没有网址。

相关问题