简体   繁体   English

如何启动 2 个在同一端口上侦听的 Java Sockets?

[英]How can I initiate 2 Java Sockets that listen on the same port?

I am currently writing a BitTorrent client and from my understanding I can use a single local port to connect to different peers and communicate with them independently.我目前正在编写一个 BitTorrent 客户端,据我了解,我可以使用单个本地端口连接到不同的对等点并独立地与它们通信。

If I were to write a server, ie I would have to accept connections then I know I could use a Java ServerSocket to listen on the same port and handle connections independently otherwise.如果我要编写一个服务器,即我必须接受连接,那么我知道我可以使用 Java ServerSocket 来侦听同一端口并独立处理连接。 However, what I want to do however is to initiate connections rather than waiting for them to be initiated (as there is no server), and I want to use the same local port for these (so I can connect to say hundreds of peers sharing the same port).但是,我想做的是启动连接而不是等待它们启动(因为没有服务器),并且我想为这些使用相同的本地端口(所以我可以连接说数百个共享同一个端口)。 How can I correctly do this?我怎样才能正确地做到这一点?

I think in your scenario you need to establish connection with other peers which are themselves listening at some port.So in order to do this you need URL and port for all the peers that you need to connect and then create socket dedicated to each of those peers. 我认为在您的情况下,您需要与本身正在某个端口监听的其他对等方建立连接,因此,您需要为所有需要连接的对等方提供URL和端口,然后为每个对等方创建专用的套接字同行。

Additionally a server socket is basically used to accept all incoming connection at a specific port. 另外,服务器套接字基本上用于接受特定端口上的所有传入连接。 So all your peers would be listening for requests at a particular port using a Server Socket at their end.Similarly you could also listen to all incoming request to your application at a specific port using Server Socket. 因此,您的所有对等方都将在其末端使用服务器套接字来侦听特定端口上的请求。同样,您也可以使用服务器套接字来侦听特定端口上对应用程序的所有传入请求。

With a socket connections, you need to have an "initiator" (usually referred to as Client) and "acceptor" (referred to as Server), even in the case of peer-to-peer communications. 使用套接字连接时,即使在对等通信的情况下,也需要具有“启动器”(通常称为“客户端”)和“接受器”(称为“服务器”)。 That is, you can still talk about Client and Server as the roles of the different peers for this particular connection. 也就是说,您仍然可以将客户端和服务器视为该特定连接的不同对等方的角色。

You can indeed reuse the local port when you act as the client (use this constructor: http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html#Socket(java.net.InetAddress,%20int,%20java.net.InetAddress,%20int) ). 当您充当客户端时,确实可以重用本地端口(使用此构造函数: http : //docs.oracle.com/javase/6/docs/api/java/net/Socket.html#Socket ( java.net。 InetAddress,%20int,%20java.net.InetAddress,%20int) )。 A socket will be created (identified by Remote Address + Remote Port + Local Address + Local Port) as long as there is no other socket with the same 4 attributes (that is, you will not be able to establish a second connection to the same peer/server, but you can establish connections to other peers with the same local port). 套接字将被创建(通过远程地址+远程端口+本地地址+本地端口识别),只要没有其他插座用相同的4个属性(即,您将无法建立到同一第二连接对等/服务器,但您可以使用相同的本地端口建立与其他对等方的连接。

I cannot think of a practical benefit of doing this though (as opposed to letting the system assign a random local port for you). 不过,我没有想到这样做的实际好处(与让系统为您分配随机的本地端口相反)。 Then again, there may be something I am not thinking of :) 再说一遍,也许有些事情我没有想到:)

How can I correctly do this?我怎样才能正确地做到这一点?

You can set the source port for a socket either by passing the localPort parameter to the Socket constructor or by calling bind() before connect() ing the socket.可以通过将localPort参数传递给Socket构造函数或通过在connect()套接字之前调用bind()来设置套接字的源端口。

However, to have two sockets listen on the same port, you'll need to set the SO_REUSEPORT / SO_REUSEADDR flags on both the server and the client sockets before being able to bind the second one.但是,要让两个 sockets 在同一个端口上侦听,您需要在服务器和客户端 sockets 上设置SO_REUSEPORT / SO_REUSEADDR标志,然后才能bind第二个。 By using the same port on all peers, you can ensure that you'll end up with only a single connection between each pair.通过在所有对等点上使用相同的端口,您可以确保最终每对之间只有一个连接。 If both peers try to connect simultaneously (eg because they discovered each other at the same time), one of the client connection attempts will fail (where the peer's server socket accepted the connection), you'll have to handle (ignore) that.如果两个对等点同时尝试连接(例如,因为他们同时发现彼此),其中一个客户端连接尝试将失败(对等点的服务器套接字接受连接),您将不得不处理(忽略)它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM