TCP Sockets defined as tuples and python

If a TCP socket is defined by a 4-tuple (source IP,source port,destination IP,destination port), then what would be the values for a TCP socket created in Python after the following Python code has been executed (i.e. when a server application is set to 'listen' to this socket)?

from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
serverSocket.listen(1)
print ‘The server is ready to receive’

In particular, what does Python set for the destination values of a socket that a server is listening to? Are these values randomly set, and if so how can I view them once a socket has been created?

The above code and question is taken from, and relates to, Kurose-Ross-Computer Networking. Thanks in advance.

Answers


To query the source and destination addresses of a socket, call the getsockname() and getpeername() methods on the socket object.

In your case, serverSocket.getsockname() returns ('0.0.0.0', 12000) because that is the bind address you specified. getpeername() on a server socket always raises a socket.error: Transport endpoint is not connected because the server socket cannot be connected to a remote peer.

For a server socket to accept incoming connections, you must call its accept() method. This will return a client socket for which the peer name will be the pair of (address, port), address being the IP address of the peer, and port being the local port number assigned for communication with the peer.


If a TCP socket is defined by a 4-tuple (source IP,source port,destination IP,destination port)

It isn't. A TCP connection is so defined. A TCP socket is just an endpoint.

then what would be the values for a TCP socket created in Python after the following Python code has been executed (i.e. when a server application is set to 'listen' to this socket)?

It would have a local port of serverPort, a local address of 0.0.0.0, and no remote address or port.


Need Your Help

Xcode 6.1.3 can't change simulated screen size

xcode interface-builder

Using Xcode 6.3.1 I needed to simulate sizes to make the app compatible with older devices, I went to menu just to find that I can't select different screen sizes.

Detect whether FocusEvent of component is lost or gained

java swing events awt

I implementing a EventQueue and get notified when AWTEvents are send.