The server is, effectively, listening on a 'well known port' and then switching subsequent communications to a dedicated port per client. Requiring the client to send to the port + 1 is a little strange
Client 192.168.0.1 - port 12121 ------------------------> Server 192.168.0.2 - port 5050
Client 192.168.0.1 - port 12121 <------------------------ Server 192.168.0.2 - port 23232
Client 192.168.0.1 - port 12121 ------------------------> Server 192.168.0.2 - port 23232 + 1
<------------------------ Server 192.168.0.2 - port 23232
------------------------> Server 192.168.0.2 - port 23232 + 1
The server probably does this so that it doesn't have to demultiplex the inbound client data based on the client's address/port. Doing it this way is a little more efficient (generally) and also has some advantages, depending on the design of the server, as on the server there's a 'dedicated' socket for you which means that if they're doing overlapped I/O then the socket stays the same for the whole period of communications with you which can make it easier and more efficient to associate data with the socket (this way they can probably avoid any lookups or locking to process each datagram). Anyway, enough of that (see here, if you want to know why I do it that way).
From your point of view as a client (and I'm assuming async sockets here) you need to first Bind()
your local socket (just use INADDR_ANY
and 0
to allow the OS to pick the port for you) then issue a RecvFrom()
on the socket (so there's no race between you sending data to the server on this socket and it sending you data back before you issue a recv). Then issue a SendTo()
to the 'well known port' of the server. The server will then send you back some data and your RecvFrom()
will return you the data and the address that the server sent to you from. You can then take that address, add one to the port, store that address and from then on issue SendTo()
s to that new sending address whilst continuing to issue RecvFrom()
s for reading the server's data; or you could do something clever with Connect()
to bind the remote end of the socket to the server's 'send to address' and simply use Write()
and RecvFrom()
from then on.