sasuke
sasuke

Reputation: 6751

Socket programming in Python -- actual remote port

I've been doing a bit of network programming these days in Python and would like to confirm the flow I think happens between the client and the server:

As you can see, in the above flow there are 3 sockets involved:

I'm aware of getting the ports for the first two sockets (9999 and 1111) but don't know how to get the "real" port which communicates with the client on the server side.The snippet I'm using right now is:

def sock_request(t):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 9999))
    print('local sock name: ' + str(s.getsockname()))
    print('peer sock name: ' + str(s.getpeername()))
    s.send('a' * 1024 * int(t))
    s.close()

Any help on getting the "port" number on the server which actually communicates with the client would be much appreciated. TIA.

Upvotes: 4

Views: 4362

Answers (1)

dty
dty

Reputation: 18998

The new socket is on the same port. A TCP connection is identified by 4 pieces of information: the source IP and port, and the destination IP and port. So the fact that your server has two sockets on the same port (i.e. the listening socket and the accepted socket) is not a problem.

Upvotes: 5

Related Questions