Make 2 client files which send data to each other using two different ports. One should send data (ex. "PING") and then receive some data (ex. "PONG").
While the other should first receive data and then send an appropriate response.
First initialise a socket connection and connect it to different network interfaces, for testing you can use the same hostname: localhost and different ports using socket.gethostname(). Sending data is done using sendto() method. It accepts as arguments, the data and the network interface address as a tuple containing hostname and port number. Receiving data is done using the recv() function; it needs a buffsize argument to denote the size of the data packet to be received.
P.S Also do not forget to encode and decode the data.
Having a basic knowledge of sockets will suffice for this task. The @lugnitdgp team shared some resources (I will link them at the bottom).
I made two files.
client2.py. Client1 will act like a server. So in client1.py, I made a class for Server(I know it wasn't necessary but yeah clean code is always preferred <3 ) and it had the basic methods as described here:
||Server Socket||create a socket object and start listening on ADDR and declare an disconnect token|
||accept a new client connection and returns the client socket and its address|
||data recevied as
||receives the message from the client socket
||none||sends the encoded response to the client socket
Then in the main body the client1.py I crated a socket object and follwed the usual order of methods.
Also I have added a token
DISCON_MSG which will indicate when to close the connection.
For client2.py it is a very basic client script. Again I made a client Class, which has three methods only.
||Initialises client socket and connects to
||send the encoded message to the connected server socket|
||receive the response from the server (aka
Thanks to @lugnitdgp for the mentoring and guiding
Top comments (0)