0% found this document useful (0 votes)
2 views

Exp5&6_Socket Programming

Uploaded by

paraspatil029
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)
2 views

Exp5&6_Socket Programming

Uploaded by

paraspatil029
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/ 4

Experiment-5 &6

Aim: Study & implementation of TCP & UDP socket


programming
Socket programming is a way for computers to talk to each other over
a network. It allows programs to send and receive data between
different devices.
UDP Server
import socket
# Create a UDP socket
# AF_INET -> Address Family is IPv4
# SOCK_DGRAM -> UDP Protocol
server_socket = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)

# Bind the socket to an address and port


server_address = ("127.0.0.1", 12345)
server_socket.bind(server_address)

print("UDP Server is listening...")

while True:
data, client_address = server_socket.recvfrom(1024) # Receive
data from client
print(f"Received '{data.decode()}' from {client_address}")

# Send response
server_socket.sendto(b"Message received", client_address)

UDP Client

import socket
# Create a UDP socket
client_socket = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)

server_address = ("127.0.0.1", 12345)

# Send message
message = "Hello UDP Server"
client_socket.sendto(message.encode(), server_address)

# Receive response
data, server = client_socket.recvfrom(1024)
print(f"Server response: {data.decode()}")

# Close socket
client_socket.close()

TCP Server
import socket
# Create a TCP socket
server_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

# Bind the socket to an address and port


server_address = ("127.0.0.1", 12345)
server_socket.bind(server_address)

# Listen for incoming connections


server_socket.listen(5)
print("TCP Server is listening...")

while True: # TCP requires proper handshake before


sending/receiving data
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")

data = client_socket.recv(1024).decode()
print(f"Received: {data}")

# Send response
client_socket.send(b"Message received")

client_socket.close() # Close client connection

TCP Client

import socket
# Create a TCP socket
client_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

server_address = ("127.0.0.1", 12345)


client_socket.connect(server_address) # Connect to server

# Send message
message = "Hello TCP Server"
client_socket.send(message.encode())

# Receive response
data = client_socket.recv(1024)
print(f"Server response: {data.decode()}")

# Close socket
client_socket.close()

Experiment-8
Aim: Installation of wireshark tool and analyze network traffic.

Theory: Wireshark is a free and open-source packet analyzer used to


capture and inspect network traffic in real time. It helps in
troubleshooting networks, analyzing security vulnerabilities, and
understanding protocols.
Installing Wireshark
Windows: Download from Wireshark Official Website.
Run TCP server program and then TCP client and apply filter
“tcp.port==12345” and you can see handshake among PSH with their
corresponding message.

You might also like