Overview:
-
The recvmsg() function receives messages through TCP sockets, UDP sockets and Unix domain sockets.
-
The recvmsg() function returns a tuple consisting of data received from the socket, ancillary data(i.e., control information), flags associated with the data and the address of the sender.
-
The Berkeley sockets implementation has the recvmsg() function receive data into multiple buffers. The Python implementation of recvmsg_into() function receives data into multiple buffers. The recvmsg() function returns the received message as a bytes object in a tuple along with other message related information.
-
The recvmsg() is versatile that it works with TCP sockets, UDP sockets and Unix domain sockets.
Example - Multi-threaded TCP server:
# A Python TCP server program that waits # Function that creates a server socket and # Bind to ip and port # Start listening # Function that processes requests from clients msgs = []
con.close() break # Start the server # Process requests |
Output:
TCP server listening at IP address 127.0.0.1 port 34000 b'GET / HTTP/1.1\r\nHost: localhost\r\n Connection: close\r\n\r\n' Connection closed |
Example - TCP client:
# Example Python program that connects to a TCP server import socket # Create a TCP socket httpGet = "GET / HTTP/1.1\r\nHost: localhost\r\n Connection: close\r\n\r\n" while(True): print("Connection closed") |
Output:
Connected to the localhost b'Read client message\nConnection being closed' Connection closed |