监听特定端口c#

发布于 2025-01-07 19:27:58 字数 209 浏览 0 评论 0原文

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

I want to listen specific port in c#, but I don't want to write a chat program in net.
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2025-01-14 19:27:58

我认为这应该让你开始。这将向您显示与 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 这里 应该可以帮助您。

I think this should get you started. This will show you similar information to 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.

别挽留 2025-01-14 19:27:58

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

You need a sniffer. Check Wireshark out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文