This is basic socket programming, Study notes of Computer Networks

Basic socket programming in python and basic exercise attached to it.

Typology: Study notes

2022/2023

Uploaded on 07/23/2023

arvasu-gupta
arvasu-gupta 🇮🇳

3 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab Assessment 4 – Socket Programming
1. Implementation of simple client/server process using TCP socket program
Note: I have uploaded the sample client/server process in python programming language
with explanation in this document for understanding purpose. You shall create the same
in your own programming language(C/C++/Java/Python).
pf3
pf4
pf5

Partial preview of the text

Download This is basic socket programming and more Study notes Computer Networks in PDF only on Docsity!

Lab Assessment 4 – Socket Programming

  1. Implementation of simple client/server process using TCP socket program Note: I have uploaded the sample client/server process in python programming language with explanation in this document for understanding purpose. You shall create the same in your own programming language(C/C++/Java/Python).

A) Simple server/client program: Server code:

Explanation:

  • The server program starts by creating a socket object using socket.socket().
  • It binds the socket to a specific address and port using bind().
  • The server then listens for incoming connections using listen().
  • When a client attempts to connect, accept() is called to accept the connection.
  • The server receives data from the client using recv(), and the received message is decoded using decode().
  • After receiving the message, the server prepares a response and sends itback to the client using send(), after encoding it using encode().
  • Finally, the server closes the client socket using close() and then closes theserver socket.
  • The client program also creates a socket object, similar to the server.
  • It connects to the server using connect(), specifying the server's address andport.
  • The client then sends a message to the server using send(), after encoding it.
  • It receives the server's response using recv(), and the response is decodedusing decode().
  • Finally, the client closes the socket using close().
  • You can run the server program first and then the client program to see thecommunication between the server and the client.
  • Note that you should run the server program first, as the client programexpects the server to be running before attempting to connect.
  • The above code is an example of a TCP (Transmission Control Protocol)socket program.
  • TCP is a reliable, connection-oriented protocol that ensures the delivery ofdata packets in the order they were sent.
  • In the code, the socket.SOCK_STREAM argument is used when creating thesocket objects, which indicates that TCP should be used.
  • In TCP, a connection is established between the client and server, and datais transmitted in a stream-like manner.
  • The server listens for incoming connections, and the client initiates aconnection to the server.
  • Once the connection is established, data can be exchanged between theclient and server.