the Beej’s Guide to Network Programming explains the accept() as follows:
What’s going to happen is this: someone far far away will try to connect() to your machine on a port that you are listen()ing on. Their connection will be queued up waiting to be accept()ed. You call accept() and you tell it to get the pending connection. It’ll return to you a brand new socket file descriptor to use for this single connection!
but how do we know the port number of the “brand new socket”?
Pass the accepted
SOCKETtogetsockname()to retreive its local IP/Port, and togetpeername()to retreive its remote IP/Port.