Socket
Socket
12
Roll no: A-557
Aim: Socket Programming using TCP/UDP
Theory:
TCP (Transmission Control Protocol) is one of the main protocols of the Internet protocol suite. It
lies between the Application and Network Layers which are used in providing reliable delivery
services. It is a connection-oriented protocol for communications that helps in the exchange of
messages between different devices over a network. The Internet Protocol (IP), which establishes the
technique for sending data packets between computers, works with TCP.
User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a part of the Internet Protocol
suite, referred to as UDP/IP suite. Unlike TCP, it is an unreliable and connectionless protocol. So,
there is no need to establish a connection prior to data transfer. The UDP helps to establish low-
latency and loss-tolerating connections establish over the network. The UDP enables process to
process communication.
TCP is reliable as it guarantees the delivery of The delivery of data to the destination cannot
data to the destination router. be guaranteed in UDP.
Uses handshakes such as SYN, ACK, SYN- It’s a connectionless protocol i.e. No
ACK handshake
TCP is used by HTTP, HTTPs, FTP, SMTP UDP is used by DNS, DHCP,
and Telnet. TFTP, SNMP, RIP, and VoIP.
Port
Port is a logical address of a 16-bit unsigned integer that is allotted to every application on the
computer that uses the internet to send or receive data.
Now every time any application sends any data, it is identified by the port that which the application
sent that data and the data is to be transferred to the receiver application according to its port. We
often call port as port number.
Ports are assigned by computer i.e. operating system to different applications. Ports help computer
to differentiate between incoming and outgoing traffic.
23 Telnet
7 Echo
22 SSH(Secure Shell)
3306 MySQL
5432 PostgreSQL
27017 MongoDB
Socket Address
A socket address is a combination of an IP address and a port number, which together form a unique
identifier for a specific communication endpoint in a network. This combination enables data to be
sent to and received from the correct destination in a networked environment.
Port Number: The port number is a 16-bit unsigned integer that ranges from 0 to 65535. It's used to
identify a specific service or process running on a device
Server.py
import socket
# Chat loop
chat = False
if msg.lower() == 'quit':
chat = True
print("Client has closed the connection.")
else:
print(f"Client: {msg}")
if response.lower() == 'quit':
chat = True
print("Server Closed")
client.py
import socket
# Chat loop
chat = False
if msg.lower() == 'quit':
chat = True
print("Chat closed")
else:
# Receive response from the server
response = client.recv(1024).decode("utf-8")
print(f"Server: {response}")
if response.lower() == 'quit':
chat = True
print("Server closed the chat.")
Conclusion : In this experiment , I have studied and implemented Socket programming using
TCP/IP .