Introduction To Socket Programming Using Python
An Introduction to Socket programming using python and it's types

Search for a command to run...
An Introduction to Socket programming using python and it's types

No comments yet. Be the first to comment.
Scalable Wireless Distributed Consensus for Autonomous Vehicles at Unsignalized Intersections

The Ultimate VLSI Roadmap is designed to help students and professionals gain clarity in choosing a role for their future careers.

A convolutional neural network (CNN) is a type of artificial neural network used primarily for image recognition and processing, due to its ability to

Natural language processing (NLP) is a branch of artificial intelligence that helps computers understand, interpret, and manipulate human language.

Introduction to Python

Hi in this blog we are going to see about socket programming.
Socket Programming is a way of connecting two nodes on a network and can communicate with each other. Sockets listen on a particular port and host. Here the communication will be on client-server architecture, where the server listens for connection and the client connects to it.
There are three types of sockets
Stream sockets allow processes to communicate using TCP. A stream socket provides a bidirectional, reliable, sequenced, and unduplicated flow of data with no record boundaries. After the connection has been established, data can be read from and written to these sockets as a byte stream. The socket type is SOCK_STREAM.
Datagram sockets allow processes to use UDP to communicate. A datagram socket supports a bidirectional flow of messages. A process on a datagram socket can receive messages in a different order from the sending sequence and can receive duplicate messages. Record boundaries in the data are preserved. The socket type is SOCK_DGRAM.
Raw sockets provide access to ICMP. These sockets are normally datagram oriented, although their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not for most applications. They are provided to support developing new communication protocols or for access to more esoteric facilities of an existing protocol. Only superuser processes can use raw sockets. The socket type is SOCK_RAW.
Source https://docs.oracle.com/cd/E19455-01/806-1017/sockets-4/index.html
Sockets can be implemented in every language since it is a concept. Here we are using python.
To use a socket, you have to import it.
import socket
Next, we have to initialise it.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here AF_INET is the address family we are using. and the SOCK_STREAM is the stream socket.
HOST = 'localhost'
PORT = 3074
s.bind((HOST, PORT)
Here HOST and PORT are variables. you can give an IP address or localhost and on a port, you can give a port greater than 1024.
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(),1234))
#port number can be anything between 0-65535(we usually specify non-previleged ports which are > 1023)
s.listen(5)
print("[+] Server listening.....")
while True:
conn,addr=s.accept()
print(f"Connection to {addr[0]} on {addr[1]}")
#f string is literal string prefixed with f which
#contains python expressions inside braces
conn.send(bytes("Socket Programming in Python","utf-8 "))
OUTPUT

import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 1234))
msg=s.recv(1024)
print(msg.decode("utf-8"))
Here the connect code will connect to the server which listens on that particular port.
OUTPUT


Please refer to the documentation to learn more about it. https://docs.python.org/3/library/socket.html
Thanks for reading this blog. Have a good day❤️