DEV Community

Cover image for Ten Days of Code: Task 1
Arnab Sen
Arnab Sen

Posted on

3 2

Ten Days of Code: Task 1

Task-1

Date: 03-Oct-20
Codes: task1_arnabsen1729

Problem Statement

Ping-Pong Clients

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.

My Approach

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. client1.py and 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:

METHODS ARGS RETURNS DESCRIPTION
init() ADDR, DISCON_MSG='QUIT' Server Socket create a socket object and start listening on ADDR and declare an disconnect token
accept_client() none conn, addr accept a new client connection and returns the client socket and its address
recv_message() conn, CHUNKS=1024, FORMAT='utf-8' data recevied as String receives the message from the client socket conn in chunks of CHUNKS and decodes it to FORMAT
send_resp() conn, RESPONSE='PONG', FORMAT='utf-8' none sends the encoded response to the client socket conn, be default it os PONG. Follows the encoding specified in params

Then in the main body the client1.py I crated a socket object and follwed the usual order of methods.

  1. socket()
  2. bind()
  3. listen()
  4. accept()

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.

METHODS ARGS DESCRIPTION
init() ADDR, DISCON_MSG='Q' Initialises client socket and connects to ADDR
send_message() message, FORMAT='utf-8' send the encoded message to the connected server socket
recv_resp() CHUNKS=1024, FORMAT='utf-8' receive the response from the server (aka client1 ) in chunks of CHUNKS and decodes it in FORMAT and returns it

Sample

image

Resources

  1. https://docs.python.org/3/library/socket.html
  2. https://drive.google.com/file/d/17MDkLEWeBajEyqu-LzXZOhgxW9ZIUqv1/view?usp=sharing
  3. https://drive.google.com/file/d/1jaNf0lfPFdChh5KhM1nAlCHPgy6wTrnQ/view?usp=sharing

Thanks to @lugnitdgp for the mentoring and guiding

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay