Network Fundamentals and Socket Programming
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).
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:
- socket(): The users can use this method to create sockets for both servers and clients.
- connect(): The users can use this method to connect to the other socket or remote address by specifying its address.
- listen(): This method enables the server to listen for the incoming connection requests from the clients.
- 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.
- bind(): This method binds the socket to the assigned address given to it as its parameter.
- 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:
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()
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()
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- Python & Libraries
- Python with MySQL
- Python with MongoDB
- Path Planning Algorithm in Python
- Image Processing with Python
- Python and Machine Learning
- Numerical Computation with Python
- Web Automation in Python
- Network Fundamentals and Socket Programming