简体   繁体   English

侦听特定的端口C#

[英]listen to specific port c#

I want to listen specific port in c#, but I don't want to write a chat program in net. 我想在c#中侦听特定端口,但是我不想在网络中编写聊天程序。 I just want listen to a port and receive all of the bytes that come from that port. 我只想听一个端口并接收来自该端口的所有字节。 I asked this question before but I did't get a useful answer. 我之前问过这个问题,但没有得到有用的答案。 I say again, I don't want to have a client and server program, I want to just have a single program that run on my Computer and show me what bytes are received from specific port, or a program that show me what IP is connected to each port , like "netstat" command in CMD.(I don't want to use CMD command in my C# program) please help me. 我再说一遍,我不想有一个客户端和服务器程序,我只想有一个在我的计算机上运行的程序,向我展示从特定端口接收到的字节,或者一个向我展示什么IP的程序。连接到每个端口,例如CMD中的“ netstat”命令。(我不想在我的C#程序中使用CMD命令)请帮助我。

I think this should get you started. 我认为这应该让您入门。 This will show you similar information to netstat : 这将向您显示与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);
    }
}

To listen to a port, the sample code provided by Microsoft here should get you going. 要监听端口,Microsoft 在此处提供的示例代码应该可以助您一臂之力。

You need a sniffer. 你需要一个嗅探器。 Check Wireshark out. 检查Wireshark

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM