0% found this document useful (0 votes)
43 views5 pages

Socket

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Socket

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

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.

Difference between TCP and UDP

Transmission Control Protocol (TCP) User Datagram Protocol (UDP)

TCP is a connection-oriented protocol. UDP is the Datagram-oriented protocol. This


Connection orientation means that the is because there is no overhead for opening
communicating devices should establish a a connection, maintaining a connection, or
connection before transmitting data and terminating a connection. UDP is efficient
should close the connection after transmitting for broadcast and multicast types of network
the data. transmission.

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.

TCP provides extensive error checking


UDP has only the basic error-checking
mechanisms. It is because it provides flow
mechanism using checksums.
control and acknowledgment of data.

An acknowledgment segment is present. No acknowledgment segment.

Sequencing of data is a feature of


There is no sequencing of data in UDP. If the
Transmission Control Protocol (TCP). this
order is required, it has to be managed by the
means that packets arrive in order at the
application layer.
receiver.
Transmission Control Protocol (TCP) User Datagram Protocol (UDP)

UDP is faster, simpler, and more efficient


TCP is comparatively slower than UDP.
than TCP.

Retransmission of lost packets is possible in There is no retransmission of lost packets in


TCP, but not in UDP. the User Datagram Protocol (UDP).

Uses handshakes such as SYN, ACK, SYN- It’s a connectionless protocol i.e. No
ACK handshake

TCP doesn’t support Broadcasting. UDP supports Broadcasting.

TCP is used by HTTP, HTTPs, FTP, SMTP UDP is used by DNS, DHCP,
and Telnet. TFTP, SNMP, RIP, and VoIP.

The TCP connection is a byte stream. UDP connection is a message stream.

This protocol is used in situations where


This protocol is primarily utilized in situations
quick communication is necessary but where
when a safe and trustworthy communication
dependability is not a concern, such as VoIP,
procedure is necessary, such as in email, on
game streaming, video, and music
the web surfing, and in military services.
streaming, etc.

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.

Some Popular Port Numbers


Some common/Popular port numbers that are used by those applications/services which are
frequently used by us-

Port Number Used By

80 HTTP(Hyper Text Transfer Protocol)

23 Telnet

25 SMTP(Simple Mail Transfer Protocol)


Port Number Used By

53 DNS(Domain Name System)

7 Echo

20/21 FTP(File Transfer Protocol)

69 TFTP(Trivial File Transfer Protocol)

443 HTTPS(Hyper Text Transfer Protocol Secure)

22 SSH(Secure Shell)

110 POP3(Post Office Protocol version 3)

67/68 DHCP(Dynamic Host Configuration Protocol0

123 NTP(Network Time Protocol)

143 IMAP(Internet Messaging Access Protocol)

1433 Microsoft SQL

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.

IP Address: The IP address identifies a device on a network.

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

A socket address is essential for communication in a networked environment. It allows data to be


correctly routed to the intended service or application on a specific device. For example, in web
browsing, a client uses the socket address to send a request to a web server, which then processes the
request and sends back the requested web page to the client's socket address.
Program for server and client communication (Chat App) using socket programming:

Server.py
import socket

# Create a socket object


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the server to an address and port


server.bind(("localhost", 3000))

# Server listening for incoming connections


server.listen()

print("Server is listening on port 3000...")

# Accept the client connection


client, addr = server.accept()

print(f"Connected with {addr}")

# Chat loop
chat = False

while not chat:


# Receive message from the client
msg = client.recv(1024).decode("utf-8")

if msg.lower() == 'quit':
chat = True
print("Client has closed the connection.")
else:
print(f"Client: {msg}")

# Send a response back to the client


response = input("You (Server): ")
client.send(response.encode("utf-8"))

if response.lower() == 'quit':
chat = True
print("Server Closed")

# Close the client and server sockets


client.close()
server.close()

client.py
import socket

# Create a socket object


client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server at localhost on port 3000


client.connect(("localhost", 3000))

# Chat loop
chat = False

while not chat:


# Send a message to the server
msg = input("You (Client): ")
client.send(msg.encode("utf-8"))

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.")

# Close the client socket


client.close()

Output at Server side:

Output at Client side:

Conclusion : In this experiment , I have studied and implemented Socket programming using
TCP/IP .

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy