Link Search Menu Expand Document

Network Fundamentals and Socket Programming

Computer Network

A group of two or more computers connected for communication is called a network. Computer networking is the communication of various computers to exchange information and share digital resources among them.

Types of Networks

There are multiple types of computer networks:

1. Local Area Network (LAN):

The local connection of the computers, such as school, office, and home, is called Local Area Network (LAN).

2. Wide Area Network (WAN):

The wide-area connection of the computers where they are connected through communication lines or radio waves to communicate is called a Wide Area Network (WAN).

Socket Programming

A socket is the endpoint of two-way communication between two communication channels or programs over a computer network. The computer developers can create these sockets using socket APIs, a set of programming requests. Various high-level programming languages, such as Python have libraries that provide a generic interface for communication.

Socket Programming in Python

Socket programming is used to connect two nodes/computers to communicate over a network. An active socket listens on an assigned port at an IP to wait for the connection request, while the other socket reaches out to it to form a connection. Generally, the listener socket is the server, and the other is the client. In a client-server connection, clients reach out to the servers to make a connection and exchange information. In python, the users import the socket module to start the socket programming. This module provides built-in methods to create and connect sockets easily, saving the need to build them from scratch. Following are some of the important methods of the socket module:

  1. socket(): The users can use this method to create sockets for both servers and clients.
  2. connect(): The users can use this method to connect to the other socket or remote address by specifying its address.
  3. listen(): This method enables the server to listen for the incoming connection requests from the clients.
  4. accept(): This method accepts the connection request from the client and returns the new socket object for information exchange and the client’s address, i.e., the receiving end of the connection.
  5. bind(): This method binds the socket to the assigned address given to it as its parameter.
  6. close(): This method closes the socket for any upcoming connection request and closes the already formed network connections.

Client-Server Programming in python

Below is the explanation and programming demonstration of the client and server in the socket programming:

Server

It is either a computer or a device to manage the computer network and its resources. Moreover, it can be on the same device or locally or remotely connected to other devices without restrictions. This program uses the above-explained bind() method to bind it to a specific port and IP so the other devices or programs can send requests to the particular address, and it can listen to that specific port for the incoming connection requests. Therefore, it also uses listen() method to listen for the incoming connections and uses the accept() method of the socket package to accept any requests. The programs or devices can then share data after forming a connection and close the connection using close() after the work is done. Below is the coding example of a simple server:

import socket

server_socket = socket.socket()
server_port = 4444
server_socket.bind((‘’, server_port))

server_socket.listen(5)
while (true):
   c, addr = server_socket.accept()
   c.send(‘conection formed successfully’)
   c.close()

Client

The client is a socket that interacts with the server to exchange information or data. In the server-client communication, clients send a connection request to the server to ask for its services. For instance, web browsers send requests to the web servers to get the user-required data. Below is the coding example of the simple client that connects to the upper server:

import socket

client_socket = socket.socket()
server_port = 4444

#sending connection request on the local IP
client_socket.connect((‘127.0.0.1’, server_port))
print(client_socket.recv(1024))
client_socket.close()

References

https://www.codingninjas.com/blog/2020/08/31/network-fundamentals-socket-programming/

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy