Lab 02
Lab 02
NETWORK
LAB-02 Introduction to Socket
Programming (Transport layer)
NAME : JENARTHTHAN A.
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.
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
import socket
while True:
# Receive data from clients (buffer size is 1024 bytes)
data, addr = sock.recvfrom(1024)
# 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"))
Screenshots
Client Code for Sending Messages
• 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):
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.
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
import socket
# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
received_sentences = 0
received_sentences += 1
import socket
# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
Screenshots
Server Output: Receiving Sentences, Capitalizing, and Sending Back Capitalized Sentences
Code Explanation
• 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.
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.
The objective is to create a time server that sends the current time to the receiver every second.
Methodology
import socket
import time
# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
while True:
# Receive a message from the client
msg, addr = sock.recvfrom(1024)
import socket
# Server configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
Message = "Time server in a loop"
# 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
Code Explanation
• 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.
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.