There are two type of sockets: SOCK_STREAM
and SOCK_DGRAM
. Below we have a comparison of both types of sockets.
SOCK_STREAM | SOCK_DGRAM |
---|---|
For TCP protocols | For UDP protocols |
Reliable delivery | Unrelible delivery |
Guaranteed correct ordering of packets | No order guaranteed |
Connection-oriented | No notion of connection(UDP) |
Bidirectional | Not Bidirectional |
To create a socket, we must use socket.socket()
function available in the Python socket module, which has the general syntax as follows:
S = socket.socket(socket_family, socket_type, protocol=0)
AF_UNIX
or AF_INET
. We are only going to talk about INET sockets in this tutorial, as they account for at least 99% of the sockets in use.SOCK_STREAM
or SOCK_DGRAM
. 0
.Now, if you remember we have discussed client-server socket program in the last tutorial as well. Now let's dig deeper into that program and try to understand the terms and methods used.
Following are some client socket methods:
To connect to a remote socket at an address. An address format(host, port) pair is used for AF_INET
address family.
Following are some server socket methods:
This method binds the socket to an address. The format of address depends on socket family mentioned above(AF_INET
).
This method listens for the connection made to the socket. The backlog is the maximum number of queued connections that must be listened before rejecting the connection.
This method is used to 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 which can be used to send and receive data on that connection, and address is the address bound to the socket on the other end of the connection.
For detailed view of methods refer documentation: https://docs.python.org/2/library/socket.html
For the below defined socket object,
s = socket.socket(socket_family, socket_type, protocol=0)
TCP Socket Methods | UDP Socket Methods |
---|---|
s.recv() → Receives TCP messages | s.recvfrom() → Receives UDP messages |
s.send() → Transmits TCP messages | s.sendto() → Transmits UDP messages |
close()
This method is used to close the socket connection.gethostname()
This method returns a string containing the hostname of the machine where the python interpreter is currently executing. For example: localhost.gethostbyname()
If you want to know the current machine's IP address, you may use gethostbyname(gethostname())
.