[英]How to search for responsive socket servers on a particular port (How to connect to a server given the port but not having its ip)?
By having two (or more) computers (devices) connected via let's say a router, is it possible to connect to a socket server on a given port without knowing its IP address or at least get a list of all servers on that port (and communicate with each to see if that's the one I'm looking for)? 通过让两台(或更多台)计算机(设备)通过路由器连接,可以在不知道其IP地址的情况下连接到给定端口上的套接字服务器,或者至少获得该端口上所有服务器的列表(以及与每个人交流以查看是否是我要寻找的那个)?
I must add I'm a beginner in java socket programming (and socket programming in general) and have been using this only to initialize my client server: 我必须补充一点,我是Java套接字编程(和一般而言的套接字编程)的初学者,并且一直仅用于初始化客户端服务器:
Socket socket = new Socket(serverIP, serverPort);
I've been able to have communication between client and server on two different computers connected by a wireless router, and I'm trying to implement that on two android devices. 我已经能够通过无线路由器在两台不同的计算机上进行客户端和服务器之间的通信,而我正在尝试在两个android设备上实现该功能。 The issue is IP address is not a thing to just ask from a user.
问题是IP地址不是仅向用户询问的问题。 That's why I wanna know how to connect to a server on a port I know without having its IP, or find all IPs that are active on that port (to perhaps try to communicate with each and see if one is the one I'm looking for)
这就是为什么我想知道如何在我知道的端口上连接服务器而又没有它的IP或找到该端口上所有活动的IP(也许尝试与每个IP通信并查看是否是我正在寻找的IP)的原因对于)
I don't know the exact solution for you how to implement it with Java sockets, but my first thought was to try broadcasting a specific message from the server and when the clients connected to the same network they can receive this message and they will know where it came from. 我不知道如何使用Java套接字实现它的确切解决方案,但是我首先想到的是尝试广播服务器中的特定消息,并且当客户端连接到同一网络时,他们可以接收到此消息,并且他们会知道它来自哪里。 ( or vice versa)
( 或相反亦然)
But, Multicasting is a better approach because: 但是,多播是一种更好的方法,因为:
"Broadcasting is inefficient as packets are sent to all nodes in the network, irrespective of whether they are interested in receiving the communication or not. This may be a waste of resources."
“广播是无效的,因为数据包被发送到网络中的所有节点,而不管它们是否有兴趣接收通信。这可能会浪费资源。” see: https://www.baeldung.com/java-broadcast-multicast
参见: https : //www.baeldung.com/java-broadcast-multicast
Maybe this GitHub repo could be helpful for you: https://github.com/jbrucker/server-discovery 也许这个GitHub仓库可能对您有所帮助: https : //github.com/jbrucker/server-discovery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.