I made a simple server and a simple client with socket module in python.
server:
# server.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send(b'Thank you for your connecting')
c.close()
and client:
#client.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.connect((host, port))
print(s.recv(1024))
I started the server and then started 4 clients and got output in server’s console as below:
Got connection from ('192.168.0.99', 49170)
Got connection from ('192.168.0.99', 49171)
Got connection from ('192.168.0.99', 49172)
Got connection from ('192.168.0.99', 49173)
what is the second part in the tuple?
From the
socketdocumentation:So the second value is the port number used by the client side for the connection. When a TCP/IP connection is established, the client picks an outgoing port number to communicate with the server; the server return packets are to be addressed to that port number.