Overview:
- Making a TCP server involves a four-step process of making calls to the create(), bind(), listen() and accept() functions of the socket module.
- The function create_server() wraps the first two steps of this process into a single call.
- Creation of the server socket can be using an IPv4 address or an IPV6 address as specified by the argument family.
- The accepted client connections can be both IPv4 based and IPv6 based or just IPv4 based.
Example:
# Example Python program that uses # Create a socket and bind to the IP, port # Serve to connection requests |
Client:
#Example Python program that creates a clitn socket # Connect to the server # Receive the data socketObject.close() |
Server output:
Server starts to accept connections from clients... Connection accepted...client details: ('2402:e280:215d:21b:81f7:4515:f9ab:ebd8', 61270, 0, 0) Message received:Hello! Sending greetings to client Connection to client closed |
Client output:
Connected to server Server said:hi |