查找子网中侦听特定端口的所有 TCPListener/让所有 TCPListener 响应广播消息
我正在尝试获取在指定端口上运行的应用程序的所有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,是的。
就个人而言,我将通过PowerShell或其他一些管理级别的解决方案(例如Windows Remote Management / WMI)来执行此操作。这样,您就可以直接询问操作系统,然后通过端口过滤插座。
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.