Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the return value of Socket.accept() in python

Tags:

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('Thank you for your connecting')     c.close() 

and client:

#client.py import socket  s = socket.socket()  host = socket.socket() 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?

like image 982
imsrch Avatar asked Sep 17 '12 07:09

imsrch


People also ask

What does the socket accept () method return?

Upon successful completion, accept() shall return the non-negative file descriptor of the accepted socket. Otherwise, -1 shall be returned and errno set to indicate the error.

What is socket accept () in Python?

socket.accept() Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

What is the use of accept () method in socket programming?

The accept() method of ServerSocket class is used to accept the incoming request to the socket. To complete the request, the security manager checks the host address, port number, and localport.

Why does the accept () return a new socket for each accepted connection?

Because the initial socket is used to wait for communication while the second is used to communicate.


2 Answers

From the socket documentation:

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

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.

like image 143
Martijn Pieters Avatar answered Nov 10 '22 17:11

Martijn Pieters


Quote from python documentation:

socket.accept()

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

What address is you can find in same doc from words "Socket addresses are represented as follows".

like image 44
Alexey Kachayev Avatar answered Nov 10 '22 16:11

Alexey Kachayev