1

Greetings,
if I grasp the FTP protocol (RFC959) correctly, in active mode with default settings, the server establishes the data connections from its port 20 to the ip/port client has connected from.

Could someone please explain to me how this actually works with multiple clients? ( or tell me where I got it wrong )

To be more specific lets say I:

1) call socket()
2) bind() the descriptor to my ip/port 20
3) connect() the client to establish the data connection
meanwhile in another thread handling a different client i attempt to do the same the bind() call will fail for obvious reasons.

I am aware that you can use setsockopt() with SO_REUSEADDR to let sockets bind to the same address/port, but are packets going to be delivered properly? I am quite lost.

I wasn't able to find anything on this matter so far, so any help would be greatly appreciated.

MikeS
  • 11
  • 1
  • 2
  • Look at this question to find out why its sometimes necessary to use SO_REUSEADDR: http://stackoverflow.com/questions/5592747/bind-error-while-recreating-socket – quamrana May 10 '11 at 17:28
  • Welcome to SO. You can update your own question by editing it. You can vote for questions (upvote or downvote) and if you get the answer that satisfies your needs and answers your question, you can mark it as an answer – Eugene Mayevski 'Callback May 10 '11 at 20:56

4 Answers4

5

Socket connection is identified using 4 parameters - source IP, source port, dest.IP and dest.port. In case of active connection 4th parameter (destination port, the one on the client computer) will be different for each connection. Complexities come when routers/NAT are involved, and then in many cases Active mode doesn't work.

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
0

Clients don't normally bind. call socket then connect. A unique local port will be assigned to the connection.

On the server, socket, bind and listen set up the server port. accept returns the unique client connection.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Although this is true in general, in the specific case of active-mode FTP the server *does* call `bind()` then `connect()`. – caf May 11 '11 at 13:39
0

FWIW, the purpose of SO_REUSEADDR is not for a server to get multiple connections on a single TCP port... it is so a TCP server can restart itself quickly while connections are active; otherwise it would have to wait for them to timeout of the kernel's connection table.

Interestingly, UDP sockets can assert SO_REUSEADDR to allow unicast and multicast applications to listen on the same UDP port.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
0

As you've noted, as long as you set the SO_REUSEADDR socket option on both sockets, and neither of them is listening, then the bind() will not fail - they can both be bound to the same local address.

Packets are delivered correctly, because one socket is connected to one peer address, and the other socket is connected to another. That is, when a packet arrives the source address and port as well as the destination address amd port is used to determine which local socket to send it to (if any).

caf
  • 233,326
  • 40
  • 323
  • 462