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

Assignment 4: 1) File Transfer Using TCP Server and Client in Python. Code:server

1. The document describes how to implement a TCP server and client in Python to transfer a file. It provides code for a TCP server that listens for connections on port 60000, accepts an incoming connection, receives data from the client, opens and reads from a file and sends the file data to the client. It also provides code for a TCP client that connects to the server, sends a message, receives the file data and saves it to a new file. 2. The document also provides code for a UDP client-server model in Python. The UDP server code binds to port 20001, receives messages from clients, prints the client's message and IP and sends a reply. The UDP client code sends a message

Uploaded by

Shiny Prasanna T
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)
93 views6 pages

Assignment 4: 1) File Transfer Using TCP Server and Client in Python. Code:server

1. The document describes how to implement a TCP server and client in Python to transfer a file. It provides code for a TCP server that listens for connections on port 60000, accepts an incoming connection, receives data from the client, opens and reads from a file and sends the file data to the client. It also provides code for a TCP client that connects to the server, sends a message, receives the file data and saves it to a new file. 2. The document also provides code for a UDP client-server model in Python. The UDP server code binds to port 20001, receives messages from clients, prints the client's message and IP and sends a reply. The UDP client code sends a message

Uploaded by

Shiny Prasanna T
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/ 6

ASSIGNMENT 4

1)File Transfer using TCP Server and Client in Python.

Code:server

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='hey.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(bytes('Thank you for connecting','utf-8'))
conn.close()
Code:client

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(bytes("Hello server!",'utf-8'))
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
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)

Code:client

import socket msgFromClient = "client" bytesToSend


= str.encode(msgFromClient) serverAddressPort =
("127.0.0.1", 20001) bufferSize = 1024
# Create a UDP socket at client side e=input("enter ur msg :")
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)

You might also like