查找子网中侦听特定端口的所有 TCPListener/让所有 TCPListener 响应广播消息

发布于 2025-01-18 13:04:59 字数 1846 浏览 3 评论 0原文

我正在尝试获取在指定端口上运行的应用程序的所有 TCPListener,以便我可以在服务器浏览器中显示它们,我想我可以将 udp 数据包发送到广播,其中包含一条 ip 的消息询问计算机并让他们在看到它后做出回应。我不想手动遍历子网中所有可能的 ip,并尝试在指定端口使用 tcpclient 连接到它们,我希望它们响应消息。这可能吗? 作为参考,侦听器代码如下所示:

       public async Task ListenAsync()
        {
            listener.Start();
            
            while (!_serverShouldClose)
            {
                var handleClientsTask = HandleClientsAsync();
                var sendAliveTask = SendAlive();
                if (listener.Pending())
                {
                    var clientTask = listener.AcceptTcpClientAsync();
                    var client = await clientTask;
                    await client.GetStream().WriteAsync(Encoding.UTF8.GetBytes("Hello from server\n"));
                    _connectedClients.Add(client);
                }
                await Task.WhenAll(sendAliveTask, handleClientsTask);
            }
            
            listener.Stop();
        }


        public async Task HandleClientsAsync()
        {
            byte[] buffer = new byte[1024];
            foreach (var client in _connectedClients)
            {
                if (!client.Connected)
                {
                    _connectedClients.Remove(client);
                    client.Close();
                    client.Dispose();
                    continue;
                }

                if(client.GetStream().DataAvailable)
                {
                    await client.GetStream().ReadAsync(buffer);
                    if (Encoding.ASCII.GetString(buffer).Contains("gib settings"))
                    {
                        await client.GetStream().WriteAsync(Encoding.ASCII.GetBytes(JsonSerializer.Serialize(_settings)));
                    }
                }
                
            }
        }

I am trying to get all the TCPListener(s) of my application that are running on a specified port so I can display them in a server browser, I thought I could just send a udp packet to the broadcast with a message with the ip of the asking computer and have them respond to it once they see it. I don't want to manually go through all the possible ips in the subnet and try connect to them with a tcpclient at the specify port just to do it, I want them to respond to the message. Is that possible ?
For reference the listener code looks like this:

       public async Task ListenAsync()
        {
            listener.Start();
            
            while (!_serverShouldClose)
            {
                var handleClientsTask = HandleClientsAsync();
                var sendAliveTask = SendAlive();
                if (listener.Pending())
                {
                    var clientTask = listener.AcceptTcpClientAsync();
                    var client = await clientTask;
                    await client.GetStream().WriteAsync(Encoding.UTF8.GetBytes("Hello from server\n"));
                    _connectedClients.Add(client);
                }
                await Task.WhenAll(sendAliveTask, handleClientsTask);
            }
            
            listener.Stop();
        }


        public async Task HandleClientsAsync()
        {
            byte[] buffer = new byte[1024];
            foreach (var client in _connectedClients)
            {
                if (!client.Connected)
                {
                    _connectedClients.Remove(client);
                    client.Close();
                    client.Dispose();
                    continue;
                }

                if(client.GetStream().DataAvailable)
                {
                    await client.GetStream().ReadAsync(buffer);
                    if (Encoding.ASCII.GetString(buffer).Contains("gib settings"))
                    {
                        await client.GetStream().WriteAsync(Encoding.ASCII.GetBytes(JsonSerializer.Serialize(_settings)));
                    }
                }
                
            }
        }

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

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

发布评论

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

评论(1

单身情人 2025-01-25 13:04:59

可能吗?

从技术上讲,是的。

就个人而言,我将通过PowerShell或其他一些管理级别的解决方案(例如Windows Remote Management / WMI)来执行此操作。这样,您就可以直接询问操作系统,然后通过端口过滤插座。

Is that possible ?

Technically, yes.

Personally, I would do this via Powershell or some other management-level solution like Windows Remote Management / WMI. That way you could just ask the OS directly, and then filter open sockets by port.

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