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

Computer Network 2

The document describes experiments related to networking protocols. It includes an index listing the experiments and their dates. One experiment involves implementing a file transfer protocol (FTP) client-server application where the client requests a file from the server and the server sends the file contents. Another experiment implements a UDP echo client-server application where the client sends a message to the server, which echoes it back. A third experiment develops a DNS server using UDP - the client queries the server for an IP address corresponding to a domain, and the server looks it up from its DNS table and responds with the IP.

Uploaded by

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

Computer Network 2

The document describes experiments related to networking protocols. It includes an index listing the experiments and their dates. One experiment involves implementing a file transfer protocol (FTP) client-server application where the client requests a file from the server and the server sends the file contents. Another experiment implements a UDP echo client-server application where the client sends a message to the server, which echoes it back. A third experiment develops a DNS server using UDP - the client queries the server for an IP address corresponding to a domain, and the server looks it up from its DNS table and responds with the IP.

Uploaded by

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

INDEX

S.NO. TITLE OF EXPERIMENT DATE INITIAL

1. Concurrent TCP/IP Day-Time Server 3/08/2022

2. Half Duplex Chat Using TCP/IP 10/08/2022

3. Implementation of File Transfer Protocol 24/08/2022

4. UDP Echo Client Server Communication 07/09/2022

5. DNS Server Using UDP 14/09/2022

6. ARP Implementation Using UDP 05/10/2022

7. Remote Command Execution Using UDP 12/10/2022

8. Full Duplex Chat Using TCP/IP 26/10/2022

9.

10.

11.

12.

13.

14.

15.
Experiment No: 3
Date:

IMPLEMENTATION OF FILE TRANSFER PROTOCOL


GIVEN REQUIREMENTS:
There are two hosts, Client and Server. The Client sends the name of the file it needs from the
Server and the Server sends the contents of the file to the Client, where it is stored in a file.

TECHNICAL OBJECTIVE:

To implement FTP application, where the Client on establishing a connection with the Server
sends the name of the file it wishes to access remotely. The Server then sends the contents of the
file to the Client, where it is stored.
METHODOLOGY:
Server:

▪ Include the necessary header files.


▪ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
▪ Initialize server address to 0 using the bzero function.
▪ Assign the sin_family to AF_INET, sin_addr to INADDR_ANY, sin_port to dynamically
assigned port number.
▪ Bind the local host address to socket using the bind function.
▪ Listen on the socket for connection request from the client.
▪ Accept connection request from the Client using accept function.
▪ Within an infinite loop, receive the file name from the Client.
▪ Open the file, read the file contents to a buffer and send the buffer to the Client
Client:
▪ Include the necessary header files.
▪ Create a socket using socket function with family AF_INET, type as SOCK_STREAM.
▪ Initialize server address to 0 using the bzero function.
▪ Assign the sin_family to AF_INET.
▪ Get the server IP address and the Port number from the console.
▪ Using gethostbyname function assign it to a hostent structure, and assign it to sin_addr of
the server address structure.
▪ Within an infinite loop, send the name of the file to be viewed to the Server.
▪ Receive the file contents, store it in a file and print it on the console.
CODE:
SERVER
# server.py

import socket # Import socket module

port = 60000 # Reserve a port for your


service. s = socket.socket() # Create a socket
object
host = socket.gethostname() # Get local machine name
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.

print 'Server listening.. .'

while True:
conn, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))

filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()

print('Done sending')
conn.send('Thank you for connecting')
conn.close()
CLIENT
# client.py
import socket # Import socket module

s = socket.socket() # Create a socket object


host = socket.gethostname() # Get local machine name
port = 60000 # Reserve a port for your
service.

s.connect((host, port))
s.send("Hello server!")

with open('received_file', 'wb') as f:


print 'file opened'
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)

f.close()
print('Successfully get the file')
s.close()
print('connection closed')
OUTPUT

INFERENCE:

Thus, the FTP client-server communication is established and data is transferred between the
client and server machine
Experiment No: 4
Date:

UDP ECHO CLIENT SERVER COMMUNICATION


GIVEN REQUIREMENTS:
There are two hosts, Client and Server. The Client accepts the message from the user and sends
it to the Server. The Server receives the message, prints it and echoes the message back to the
Client.

TECHNICAL OBJECTIVE:

To implement an UDP Echo Client-Server application, where the Client on establishing a


connection with the Server, sends a string to the Server. The Server reads the String, prints it and
echoes it back to the Client
METHODOLOGY:
Server:

▪ Include the necessary header files.


▪ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
▪ Initialize server address to 0 using the bzero function.
▪ Assign the sin family to AF_INET, sin_addr to INADDR_ANY, sin_port to
SERVER_PORT, a macro defined port number.
▪ Bind the local host address to socket using the bind function.
▪ Within an infinite loop, receive message from the client using recv from function, print it
on the console and send (echo) the message back to the client using send to function.
Client:
▪ Include the necessary header files.
▪ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
▪ Initialize server address to 0 using the bzero function.
▪ Assign the sin_family to AF_INET.
▪ Get the server IP address from the console.
▪ Using gethostbyname function assign it to a hostent structure, and assign it to sin_addr of
the server address structure.
▪ Within an infinite loop, read message from the console and send the message to
the server using the sendto function.
▪ Receive the echo message using the recvfrom function and print it on the console.
CODE:
SERVER
# server.py

import socket
localIP = "127.0.0.1"
localPort = 20001
bufferSize = 1024
msgFromServer = "Hello UDP Client"
bytesToSend = str.encode(msgFromServer)

# Create a datagram socket


UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

# Bind to address and ip


UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")

# Listen for incoming datagrams


while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:
{}".format(message) clientIP = "Client IP Address:
{}".format(address) print(clientMsg)
print(clientIP)

# Sending a reply to client


UDPServerSocket.sendto(bytesToSend, address)
CLIENT
# client.py
import socket

msgFromClient = "Hello UDP Server"


bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("127.0.0.1", 20001)
bufferSize = 1024

# Create a UDP socket at client side


UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

# Send to server using created UDP socket


UDPClientSocket.sendto(bytesToSend, serverAddressPort)

msgFromServer = UDPClientSocket.recvfrom(bufferSize)

msg = "Message from Server {}".format(msgFromServer[0])


print(msg)
OUTPUT

INFERENCE:

Thus, the UDP ECHO client server communication is established by sending the message from
the client to the server and server prints it and echoes the message back to the client.
Experiment No: 5
Date:

DNS SERVER USING UDP


GIVEN REQUIREMENTS:
There are multiple web domains. The IP addresses of all the domains are stored in the DNS
server to which a client can make a connection to query IP address of the respective
domain from the server. Input of domain name is given by user.

TECHNICAL OBJECTIVE:

Domain Name Server (DNS) is implemented through this program. The web address of any
website is given by the Client as the input. The DNS Server looks up for the corresponding
domain and returns the IP address as the output.
METHODOLOGY:

▪ Include the necessary header files.


▪ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM.
▪ Declare a dictionary which will hold the DNS records with all the domain names and
their IP addresses.
▪ Create an object of the socket as s which will bind to localhost at port 1234.
▪ Using the created socked the client will send the hostname for which the IP address
needs to be looked up by the DNS server.
▪ Ping the client and send the IP address that is fetched from the dictionary in the server.
▪ Print the size of data sent in the output console.
▪ Wait for more connections / requests for more domain to IP lookups.

CODE:
SERVER
# server.py

import socket
dns_table={"www.google.com": "192.165.1.1","www.youtube.com":
"192.165.1.2","www.gmail.com": "192.165.1.3"}
print("starting server.. .")
while True:
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(("127.0.0.1",1234))
data,address=s.recvfrom(1024)
print(f"{address} wants to fetch data")
data=data.decode()
ip=dns_table.get(data,"not found").encode()
send=s.sendto(ip,address)
s.close()

CLIENT
# client.py
import socket
hostname=socket.gethostname()
addr=("127.0.0.1",1234)
c = "Y"
while c.upper()=="Y":
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
req_domain = input("enter domain name for which the ip is needed:")
send=s.sendto(req_domain.encode(),addr)
data, address=s.recvfrom(1024)
reply_ip = data.decode().strip()
print(f"the ip for the domain name{req_domain}:{reply_ip}")
c=(input("continue?(y/n)"))
s.close()

OUTPUT
INFERENCE:

Thus the DNS Server is developed to get the domain name from the remote machine and to send
the IP address of the domain as response from server’s DNS table.

You might also like