0% found this document useful (0 votes)
25 views6 pages

Computer Network Roshan Jacob

Uploaded by

roshanjacob196
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)
25 views6 pages

Computer Network Roshan Jacob

Uploaded by

roshanjacob196
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/ 6

24-10-2024

Computer Network
Roshan Jacob
U2101162
32
ECE-Gamma

1.Capture packets using wire shark and analyse different protocols

The captured packets include traffic from multiple protocols but I have filtered 3 from them:
1. HTTP

2. DNS
24-10-2024

3. TCP

4. Analysis
24-10-2024

Q2. Analyse how TCP packets handle packet loss , congestion and retransmission

1. TCP Packets

2. TCP Retransmission

3. Duplicate Acknowledgement

Window Size Changes

4. Retransmission Timeout
24-10-2024

Q3. Write a client server application for chat using TCP

Server side code

import socket
import threading

# Server setup
host = '127.0.0.1' # Localhost (can be changed to your IP for a different network)
port = 55555

# Create a socket for IPv4 (AF_INET) and TCP (SOCK_STREAM)


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()

# List of clients
clients = []
nicknames = []

# Broadcast message to all clients


def broadcast(message):
for client in clients:
client.send(message)

# Handle client connection


def handle_client(client):
while True:
try:
# Receive and broadcast messages from a client
message = client.recv(1024)
broadcast(message)
except:
# Remove and close client if there is an error
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast(f'{nickname} left the chat!'.encode('utf-8'))
nicknames.remove(nickname)
break

# Receive clients and handle them


def receive():
while True:
client, address = server_socket.accept()
print(f"Connected with {str(address)}")

# Ask the client for a nickname


24-10-2024

client.send("NICK".encode('utf-8'))
nickname = client.recv(1024).decode('utf-8')
nicknames.append(nickname)
clients.append(client)

# Broadcast the new connection to all clients


print(f"Nickname of the client is {nickname}")
broadcast(f'{nickname} joined the chat!'.encode('utf-8'))
client.send("Connected to the server!".encode('utf-8'))

# Start a new thread to handle each client


thread = threading.Thread(target=handle_client, args=(client,))
thread.start()

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

Client side code

import socket
import threading

# Choose a nickname
nickname = input("Choose your nickname: ")

# Connect to the server


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 55555))

# Receive messages from the server


def receive_messages():
while True:
try:
# Receive messages from server
message = client_socket.recv(1024).decode('utf-8')

if message == 'NICK':
client_socket.send(nickname.encode('utf-8'))
else:
print(message)
except:
# Close connection if there is an error
print("An error occurred!")
client_socket.close()
break

# Send messages to the server


def send_messages():
while True:
24-10-2024

message = f'{nickname}: {input("")}'


client_socket.send(message.encode('utf-8'))

# Start threads for receiving and sending messages


receive_thread = threading.Thread(target=receive_messages)
receive_thread.start()

send_thread = threading.Thread(target=send_messages)
send_thread.start()

You might also like