Introduction To Socket Programming Using Python
An Introduction to Socket programming using python and it's types
Hi in this blog we are going to see about socket programming.
SOCKETS
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.
TYPES OF SOCKETS
There are three types of sockets
STREAM 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
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
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.
PROGRAMMING WITH SOCKETS
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.
SERVER PROGRAM
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
CLIENT PROGRAM
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. docs.python.org/3/library/socket.html
Thanks for reading this blog. Have a good day❤️