听特定端口c#

时间:2012-02-22 21:41:29

标签: c# .net port listen

我想在c#中侦听特定端口,但我不想在网上编写聊天程序。 我只想听一个端口并接收来自该端口的所有字节。 之前我问过这个问题,但我没有得到有用的答案。我再说一遍,我不想有一个客户端和服务器程序,我想只有一个程序在我的计算机上运行并显示从特定端口接收的字节,或者一个程序向我显示IP是什么连接到每个端口,如CMD中的“netstat”命令。(我不想在我的C#程序中使用CMD命令) 请帮帮我。

2 个答案:

答案 0 :(得分:3)

我认为这应该让你开始。这将向您显示与netstat

类似的信息
using System;
using System.Net;
using System.Net.NetworkInformation;

static void Main()
{

    IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
    TcpConnectionInformation[] tcpConnections = ipGlobalProperties.GetActiveTcpConnections();

    foreach (TcpConnectionInformation tcpConnection in tcpConnections)
    {
        Console.WriteLine("Local Address {0}:{1}\nForeign Address {2}:{3}\nState {4}",
                        tcpConnection.LocalEndPoint.Address,
                        tcpConnection.LocalEndPoint.Port,
                        tcpConnection.RemoteEndPoint.Address,
                        tcpConnection.RemoteEndPoint.Port,
                        tcpConnection.State);
    }
}

要收听端口,Microsoft here提供的示例代码可以帮助您。

答案 1 :(得分:0)

你需要一个嗅探器。检查Wireshark

相关问题