在网络中查找开放的TCP端口 [英] Finding open TCP port in a network

查看:247
本文介绍了在网络中查找开放的TCP端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建netowrk应用程序.我已经成功地制作了一个发送和接收包的服务器.到目前为止,已经在一台指向127.0.0.1的计算机上进行了测试(没有地方像家一样).现在,我想切换到网络.如何找到局域网中正在监听我的特定端口的计算机?

I'm trying to build a netowrk app. I have succesfully made a server that sends and receives packages. So far, testing has been done on one computer pointing to 127.0.0.1 (No place like home). Now I want to switch to the network. How can I find computers on a LAN network that are listening to my specific port?

推荐答案

该服务将需要监听在IANA上注册程序和端口号),当它听到广播消息时,它将向发送方答复服务器的IP以及服务正在侦听传入连接的端口.

The service will need to listen for broadcast messages on a known port (if you want to be really well behaved you can register the program and port number with the IANA), when it hears a broadcast message it replies to the sender the server's IP and what port the service is listening for incoming connections on.

这是上面链接中的一个简单示例,它仅显示到控制台中连接了谁和在哪个端口上的控制台,但是您可以使用此信息在两个端点之间建立TCP或UDP连接.

Here is a simple example from the link above, this just prints to the console who connected and on what port, but you can use this information to establish a TCP or UDP connection between the two endpoints.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class RecvBroadcst
{
  public static void Main()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork,
           SocketType.Dgram, ProtocolType.Udp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
   sock.Bind(iep);
   EndPoint ep = (EndPoint)iep;
   Console.WriteLine("Ready to receive…");
   byte[] data = new byte[1024];
   int recv = sock.ReceiveFrom(data, ref ep);
   string stringData = Encoding.ASCII.GetString(data, 0, recv);
   Console.WriteLine("received: {0} from: {1}",
              stringData, ep.ToString());
   data = new byte[1024];
   recv = sock.ReceiveFrom(data, ref ep);
   stringData = Encoding.ASCII.GetString(data, 0, recv);
   Console.WriteLine("received: {0} from: {1}",
              stringData, ep.ToString());
   sock.Close();
  }
}


作为一个伪示例,这是有关我将如何执行的事件序列.


As a psudo example here is the sequence of events on how I would do it.

对于此示例,我们有一个IP地址为192.168.1.0且子网为255.255.255.0的网络.我们有两个服务器,在192.168.1.2上的Server1,该服务侦听端口1234,在192.168.1.3上的Server2,该服务的端口为4567.两者都在端口3000上列出以广播消息.客户端连接将位于IP 192.168.1.4

For this example lets say we have a network with a IP of 192.168.1.0 and a subnet of 255.255.255.0. We have two servers, Server1 at 192.168.1.2 with the service listening on port 1234, and Server2 at 192.168.1.3 with a port of 4567 for the service. Both are listing on port 3000 for broadcast messages. The client connecting will be at the IP 192.168.1.4

  1. 客户端在动态端口范围(49152-65535)并绑定到UDP(在此示例中为端口50123)并监听.
  2. 客户端使用侦听时使用的同一端口发送广播到广播地址和本地子网(192.168.1.255:3000)的已知端口.他发送某种有效负载,因此服务器仅发送回您的客户端,而不发送给其他碰巧与您使用相同端口的人. (让我们说它发送字符串Send me your info for XYZ app!)
  3. Server1接收广播.检查消息是否为Send me your info for XYZ app!并将UDP消息Name:Server1 IP:192.168.1.2 Port:1234发送回发件人源端口和IP组合(192.168.1.4:50123)
  4. Server2也接收广播.检查消息是否为Send me your info for XYZ app!并将UDP消息Name:Server2 IP:192.168.1.3 Port:4567消息发送回发件人源端口和IP组合(192.168.1.4:50123)
  5. 客户端在其发送消息的同一端口上接收到两条UDP消息.他解析答复,并向用户显示可用于连接的两台服务器.
  1. Client chooses a random port in the dynamic port range(49152-65535) and binds to it on UDP (port 50123 for this example) and listens.
  2. The client broadcasts to the broadcast address and the known port for his local subnet (192.168.1.255:3000) using the same port to send as he is listening on. He sends some kind of payload so the servers only send back to your clients, instead of someone else who happened to use the same port as you. (lets say it sends the string Send me your info for XYZ app!)
  3. Server1 receives the broadcast. Checks that the message is Send me your info for XYZ app! and sends the UDP message Name:Server1 IP:192.168.1.2 Port:1234 back to the senders source port and IP combination (192.168.1.4:50123)
  4. Server2 receives the broadcast also. Checks that the message is Send me your info for XYZ app! and sends the UDP message Name:Server2 IP:192.168.1.3 Port:4567 message back to the senders source port and IP combination (192.168.1.4:50123)
  5. The client receives two UDP messages on the same port he sent the message on. He parses the replies and displays to the user the two servers available to connect to.

这篇关于在网络中查找开放的TCP端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆