The socket server

First, we define the necessary variables for the connection, that is, the IP address for localhost in IPv6 format and the maximum number of connections from clients:

IPV6_ADDRESS = '::1'
# Up to 5 clients can connect
maxConnections = 5

Next, since we have the necessary data, we create the server. It is a socket-type object that is listening in a specific port using IPv6 and TCP/IP:

# Creating the server with ipv6 support
# socket.AF_INET6 to indicate that we will use Ipv6
# socket.SOCK_STREAM to use TCP/IP

server_socket = socket.socket(socket.AF_INET6,socket.SOCK_STREAM)
dataConection = (host,port)
server_socket.bind(dataConection)

Our socket is already created. Now we must accept connections from it:

print("Waiting connections in %s:%s" %(host, port))
connection, address = server_socket.accept()
print ('Connected to', address)

The socket.accept() method will remain listening until you receive a request. Then, in a loop, we indicate what the server should do when receiving each connection:

while True:
data = connection.recv(1024)
print ("Received data from the client: [%s]" %data.decode())
if data.decode() == "exit":
connection.send(bytes("exit".encode('utf-8')))
break
connection.send(data)
print ("Sent data echoed back to the client: [%s]" %data.decode())

The core of our program is in this loop, and it's where we indicate the way to act when receiving the client's frames.

Finally, when the connection is closed, we indicate with a message that it has been closed and we close the socket with the socket.close() method:

connection.close()

You can find the full code in the echo_server_ipv6.py file:

#!/usr/bin/env python3

import argparse
import socket

IPV6_ADDRESS = '::1'
# Up to 5 clients can connect
maxConnections = 5

def echo_server_ipv6(port, host=IPV6_ADDRESS):
# Creating the server with ipv6 support
# socket.AF_INET6 to indicate that we will use Ipv6
# socket.SOCK_STREAM to use TCP/IP
try:
server_socket = socket.socket(socket.AF_INET6,socket.SOCK_STREAM)
dataConection = (host,port)
server_socket.bind(dataConection)
# We assign the maximum number of connections
server_socket.listen(maxConnections)
except socket.error as err:
print ("Socket error: %s" %err)
server_socket.close()

print("Waiting connections in %s:%s" %(host, port))
connection, address = server_socket.accept()
print ('Connected to', address)

In the previous code block, we established a socket connection with IPv6 support, assigning the maximum number of connections the server can accept. Later, with the accept() method, the server will listen to requests waiting for connections from a client:

    while True:
data = connection.recv(1024)
print ("Received data from the client: [%s]" %data.decode())
if data.decode() == "exit":
connection.send(bytes("exit".encode('utf-8')))
break

connection.send(data)
print ("Sent data echoed back to the client: [%s]" %data.decode())

print("------- CLOSE CONNECTION ---------")
connection.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='IPv6 Socket Server')
parser.add_argument('--port', action="store", dest="port", type=int, required=True)
given_args = parser.parse_args()
port = given_args.port
echo_server_ipv6(port)

The most important part of the server is the infinite loop simulated with the while True: instruction. In this part, we implement receiving the message from the client and the instruction for sending the response to the client. At the end of the script, we establish the port where the server will send the response with the argparse module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset