0

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.

If I were to write a server, i.e. 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. 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?

  • Servers by definition do no initiate connections. They server connections initiate. I think what you're trying to do is connect to a server using multiple clients possibly? – Cripto Feb 26 '16 at 01:29
  • Nope, there is no server in p2p, I was just saying that I know of ServerSocket for multiple connections on a single port but it is not a solution to my problem. – user1576490 Feb 26 '16 at 01:32
  • There is no **Central** server in P2P. P2P uses many clients. But the actual interaction been the clients can be considered a server-client relationship. Look at https://tools.ietf.org/html/rfc5694: "In the extremes, some architectures are clearly P2P while others are clearly client-server" – Cripto Feb 26 '16 at 01:39

3 Answers3

0

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.

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.

rootExplorr
  • 575
  • 3
  • 17
0

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) ). 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).

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 :)

xpa1492
  • 1,953
  • 1
  • 10
  • 19
  • The practical benefit is indeed in peer-to-peer systems, where both peers try to initiate the connection at more or less the same time (after discovering each other). – Bergi Sep 17 '22 at 00:34
0

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.

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. 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 (e.g. 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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375