The socket client

In the client part, we create a new socket that is listening in the same server host and port:

# Configure the data to connect to the server
# socket.AF_INET6 to indicate that we will use Ipv6
# socket.SOCK_STREAM to use TCP/IP
# These protocols must be the same as on the server

client = socket.socket (socket.AF_INET6, socket.SOCK_STREAM)
client.connect ((host, port))
print ("Connected to the server --->% s:% s"% (host, port))

Our socket is already created for sending data to the server:

#send initial data to server
message = "Hello from ipv6 client"
print ("Send data to server: %s" %message)
client.send(bytes(message.encode('utf-8')))

And finally we indicate what we want to do with the connection. In this case, we will also do it in a loop. Since the way of client and server interact is that the client sends a message to the server and the server will respond Received from server. When the client receives this message, they will ask for a message from the user to be able to send it back to the server. To close the connection, the user must write exit to the client and send that message to the server. When it reaches the server, it will send the exit message to the client, then it will show a message of Connection closed and it will close the connection. The client, upon receiving the exit message from the server, will do the same and the connection will end correctly on both sides. The code for this operation is the following:

while True:
message = input("Write your message > ")
client.send(bytes(message.encode('utf-8')))
data = client.recv(1024)
print ('Received from server:', data.decode())
if data == "exit":
break;

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

#!/usr/bin/env python3

import argparse
import socket

IPV6_ADDRESS = '::1'

def echo_client_ipv6(port, host=IPV6_ADDRESS):
# Configure the data to connect to the server
# socket.AF_INET6 to indicate that we will use Ipv6
# socket.SOCK_STREAM to use TCP/IP
# These protocols must be the same as on the server
try:
client = socket.socket (socket.AF_INET6, socket.SOCK_STREAM)
client.connect ((host, port))
print ("Connected to the server --->% s:% s"% (host, port))
except socket.error as err:
print ("Socket error:%s" %err)
client.close()

# send initial data to server
message = "Hello from ipv6 client"
print ("Send data to server: %s" %message)
client.send(bytes(message.encode('utf-8')))

while True:
message = input("Write your message > ")
client.send(bytes(message.encode('utf-8')))
data = client.recv(1024)
print ('Received from server:', data.decode())
if data.decode() == "exit":
break;

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

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

The most important part of the client is the infinite loop simulated with the while True: instruction. In this part, we implement sending the message to the server and the instruction for receiving the response from the server with the data = client.recv(1024) code line. At the end of the script, we establish the port where the client will send the messages 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