0% found this document useful (0 votes)
11 views16 pages

Lab 02

The document outlines a laboratory session focused on socket programming within computer and data networks, detailing objectives such as understanding TCP and UDP, creating echo servers, and implementing the Capitalizer Service Protocol. It includes methodologies and code examples for exercises that demonstrate server-client interactions, including an echo server, a capitalizer service, and a time server. Each exercise successfully achieved its objectives, providing hands-on experience in network communication and socket programming.
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)
11 views16 pages

Lab 02

The document outlines a laboratory session focused on socket programming within computer and data networks, detailing objectives such as understanding TCP and UDP, creating echo servers, and implementing the Capitalizer Service Protocol. It includes methodologies and code examples for exercises that demonstrate server-client interactions, including an echo server, a capitalizer service, and a time server. Each exercise successfully achieved its objectives, providing hands-on experience in network communication and socket programming.
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/ 16

EC4060: COMPUTER AND DATA

NETWORK
LAB-02 Introduction to Socket
Programming (Transport layer)

NAME : JENARTHTHAN A.

REGISTRATION NO. : 2021/E/006

DATE ASSIGNED : 30 OCT 2023


Objective:

The primary objective of this laboratory session on Introduction to Socket Programming (Transport layer)
is to gain a fundamental understanding of socket programming and its application in computer and data
networks. Specifically, the objectives are as follows:

1. To learn the basics of socket programming and how it enables communication over computer
networks.
2. To explore the differences between Transmission Control Protocol (TCP) and User Datagram
Protocol (UDP) in the context of the transport layer.
3. To understand the roles of sockets, including creating and configuring sockets.
4. To create practical implementations of both UDP servers and clients.
5. To complete exercises that involve modifying and extending socket programs to solve specific
networking tasks.
6. To apply knowledge of socket programming to create networked applications and services.
7. To gain hands-on experience in working with sockets and network communication.

Exercise 1: Creating an Echo Server

Objective

The objective of this exercise is to modify a server to function as an echo server, capable of responding to
multiple clients indefinitely.

Methodology

Server Code (Python):

import socket

# Define the server's IP address and port number


UDP_IP = "127.0.0.1"
UDP_PORT = 5005

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the specified IP address and port


sock.bind((UDP_IP, UDP_PORT))

while True:
# Receive data from clients (buffer size is 1024 bytes)
data, addr = sock.recvfrom(1024)

print("Message received from", addr)

# Decode and print the received message


received_message = data.decode("utf-8")
print("Received message:", received_message)

print("Sending the received message back to the client")


# Send the received message back to the client
sock.sendto(data, addr)

print("Message sent successfully!")


print("\n")

Client Code (Python):

# Import the 'socket' module to work with sockets


import socket

# Define the server's IP address and port number


UDP_IP = "127.0.0.1"
UDP_PORT = 5005

# Create a message to send


MESSAGE = "HELLO WORLD!..."

# Convert the message to bytes using UTF-8 encoding (required for Python 3)
MESSAGE = bytes(MESSAGE, "utf-8")

# Print information about the message, target IP, and target port
print("UDP target IP: ", UDP_IP)
print("UDP target port:", UDP_PORT)
print("Message:", MESSAGE.decode("utf-8"))

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Send the message to the server at the specified IP and port


sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

# Receive a response from the server and the server's address


data, addr = sock.recvfrom(1024)

# Print the received message (echoed from the server)


print("Received message is: ", data.decode('utf-8'))

# Close the socket and finish the communication


sock.close()

Screenshots
Client Code for Sending Messages

Server Code for Echoing Messages


Client Output: Sending and Receiving Messages

Server Output: Receiving and Echoing Messages


Code Explanation

Server Code (Python):

• The server code begins by creating a socket using the socket module.
• It binds the socket to a specific IP address and port, making it available for incoming connections.
• The server enters a listening state, awaiting client connections.
• When a client connects, a new socket is created to handle communication with that client.
• Within a nested loop, the server continuously receives data from the client and sends the received
data back to the client (echoing).
• The server continues to listen for new client connections indefinitely.
Client Code (Python):

• The client code specifies the server's IP address and port.


• It creates a message to send and converts it to bytes using UTF-8 encoding.
• The client sends the message to the server using a UDP socket.
• It then receives a response from the server, which should be the echoed message.
• Finally, the client prints the received message.

Results

The objective of this exercise was to modify a server to function as an echo server capable of responding
to multiple clients indefinitely. This objective was successfully achieved.

• The server was modified to continuously listen for incoming client connections and echo the
messages it received.
• The client successfully connected to the server, sent a message, and received the same message
back.
• The server effectively echoed messages to clients, demonstrating the functionality of an echo
server.

In conclusion, Exercise 1 was completed without major challenges, and the desired objective of creating
an Echo Server was achieved. This exercise serves as a foundation for understanding socket programming
and network communication.

Exercise 2: Implementing the Capitalizer Service Protocol (CSP)

Objective

The objective of this exercise is to create a server that uses the Capitalizer Service Protocol (CSP) to
capitalize sentences sent by the client.

Methodology

Server Code (Python):

import socket

# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the specified IP address and port


sock.bind((UDP_IP, UDP_PORT))

# Receive the number of sentences the client will send


data, addr = sock.recvfrom(1024) # buffer size 1024
num_sentences = int(data)

print("Number of sentences to be received: ", num_sentences)


print("\n")
ack = "'ack'" # Acknowledgement

# Send acknowledgment back to the client


sock.sendto(bytes(ack, 'utf-8'), addr)

received_sentences = 0

while received_sentences < num_sentences:


# Receive a sentence from the client
sentence, addr = sock.recvfrom(1024)

# Print the received message


print('Received message: ', sentence.decode('unicode-escape'))
print('Sending message to client')

# Send back the capitalized sentence to the client


sock.sendto(bytes(sentence.decode('unicode-escape').upper(), 'utf-8'), addr)

received_sentences += 1

# Close the socket


sock.close()

Client Code (Python):

import socket

# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

print("UDP target IP: ", UDP_IP)


print("UDP target port: ", UDP_PORT)

num_messages = 3 # Number of messages to be sent


print("Messages to be sent: ", num_messages)

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Send the number of messages to the server


sock.sendto(bytes(str(num_messages), 'utf-8'), (UDP_IP, UDP_PORT))
data, addr = sock.recvfrom(1024)
print("Message received: ", data.decode('unicode-escape'))

# Dummy sentences for testing


sentences = ["Hello, how are you?", "This is a test.", "Sending data to the server."]

for i, sentence in enumerate(sentences, 1):


sock.sendto(bytes(sentence, 'utf-8'), (UDP_IP, UDP_PORT))
data, addr = sock.recvfrom(1024)
print(f"Capitalized sentence {i}: {data.decode('unicode-escape')}")

# Close the socket


sock.close()

Screenshots

Client Code for Implementing CSP (Capitalizer Service Protocol)


Server Code for Implementing CSP (Capitalizer Service Protocol)
Client Output: Sending Messages and Receiving Acknowledgment

Server Output: Receiving Sentences, Capitalizing, and Sending Back Capitalized Sentences

Code Explanation

Server Code (Python):

• The server code begins by creating a UDP socket using the socket module.
• It configures the server's IP address and port.
• The server binds the socket to its address to receive incoming data.
• It waits to receive the number of sentences the client will send.
• The server acknowledges the client's message.
• It continuously receives sentences from the client, capitalizes them, and sends the capitalized
sentences back to the client.

Client Code (Python):

• The client code specifies the server's IP address and port.


• It creates a UDP socket for communication.
• The client sends the number of sentences to be sent to the server.
• It waits for an acknowledgment from the server.
• The client then sends multiple sentences to the server.
• For each sentence sent, the client receives the capitalized version from the server.
Results

The objective of this exercise, which was to create a server that uses the Capitalizer Service Protocol
(CSP) to capitalize sentences sent by the client, was successfully achieved. Here are the key outcomes:

• The server code successfully implemented CSP, including receiving the number of sentences to be
capitalized and acknowledging the client.
• The client code sent the number of sentences to be capitalized to the server, and upon receiving
acknowledgment, it sent multiple sentences to the server for capitalization.
• The server capitalized each sentence received from the client and sent the capitalized version back
to the client.
• This exercise demonstrated the successful implementation of CSP for capitalizing sentences.

Exercise 3: Developing a Time Server


Objective

The objective is to create a time server that sends the current time to the receiver every second.

Methodology

Server Code (Python):

import socket
import time

# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the specified IP address and port


sock.bind((UDP_IP, UDP_PORT))

# Receive a connection message from the client


data, addr = sock.recvfrom(1024)
print("Client and server are connected:", data.decode('utf-8'))

# Send an acknowledgment message back to the client


sock.sendto(bytes("Server connected", 'utf-8'), addr)

while True:
# Receive a message from the client
msg, addr = sock.recvfrom(1024)

# Get the current local time and format it as a string


localtime = time.asctime(time.localtime(time.time()))
# Send the local time to the client
sock.sendto(bytes(localtime, 'utf-8'), addr)

# Add a 5-second delay before the next iteration


time.sleep(5)

Client Code (Python):

import socket

# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
Message = "Time server in a loop"

print("UDP target IP: ", UDP_IP)


print("UDP target port: ", UDP_PORT)
print("Message: ", Message)

# Create a UDP socket for Internet and UDP communication


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Send a message to the server to establish the connection


sock.sendto(bytes(Message, 'utf-8'), (UDP_IP, UDP_PORT))

# Receive and print the acknowledgment message from the server


data, server = sock.recvfrom(1024)
print(data.decode('unicode-escape'))

# Enter an endless loop to receive and print time updates from the server
while True:
sock.sendto(bytes(Message, 'utf-8'), (UDP_IP, UDP_PORT))
time, addr = sock.recvfrom(1024)
print(time.decode('unicode-escape'))
Screenshots

Server Code for Developing a Time Server


Client Code for Establishing Connection and Receiving Time Updates

Server Output: Receiving Connection Message and Sending Time Updates


Client Output: Sending Connection Message and Receiving Time Updates

Code Explanation

Server Code (Python):

• The server code begins by creating a UDP socket using the socket module.
• It configures the server's IP address and port.
• The server binds the socket to its address to receive incoming data.
• The server waits to receive a connection message from the client and acknowledges the
connection.
• It enters an infinite loop where it continuously receives a message from the client, retrieves the
current local time, and sends the time back to the client.
• A 5-second delay is added between sending time updates to the client.

Client Code (Python):

• The client code specifies the server's IP address and port.


• It creates a UDP socket for communication.
• The client sends a connection message to the server to establish the connection.
• It waits for an acknowledgment message from the server, confirming the connection.
• The client enters an endless loop, sending a message to the server to request time updates and
receiving and printing the current time from the server.
Results

The objective of this exercise, which was to create a time server that sends the current time to the receiver
every second, was successfully achieved. Here are the key outcomes:

• The server code continuously sends the current time to the receiver every 5 seconds.
• The client code successfully establishes a connection with the server, receives an
acknowledgment, and continuously receives and prints time updates from the server.

This exercise demonstrates the practical implementation of a time server that provides synchronized time
updates to networked clients. No significant challenges were encountered during the implementation.

You might also like