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

Socket Prog

The document discusses sending and receiving messages between a client and server using UDP. It provides code samples for a UDP client and server that allow them to communicate by sending and receiving messages. It also discusses sending files from a local host to a server using FTP, including code samples for an FTP client and server that allow file transfer between them.

Uploaded by

Mudassar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Socket Prog

The document discusses sending and receiving messages between a client and server using UDP. It provides code samples for a UDP client and server that allow them to communicate by sending and receiving messages. It also discusses sending files from a local host to a server using FTP, including code samples for an FTP client and server that allow file transfer between them.

Uploaded by

Mudassar Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

11.

Send and Receive Messages using UDP


Aim: To send and receive messages between client and server using UDP.

Procedure:

UDP (User Datagram Protocol) is connection-less protocol which is suitable for applications
that require efficient communication that doesn't have to worry about packet loss.

Implementing the client:

To begin we will need to import the socket python module. Once we’ve got this we need to
declare the IP address that we will be trying to send our UDP messages to as well as the port number.
This port number is arbitrary but ensures that you aren’t using a socket that has already been taken.
Now that we've declared these few variables it’s time to create the socket through which we will be
sending our UDP message to the server. And finally, once we've constructed our new socket it's time
to write the code that will send our UDP messages.

Implementing the server:

Once we have coded our client we then need to move on to creating our server program which
will be continuously listening on our defined IP address and port number for any UDP messages. It is
essential that this server has to be run prior to the execution of the client python script or the client
script will fail. Once we’ve imported the socket module and declared our IP address and port number
we can create another socket which will look exactly like the socket we constructed in our client
program. And finally, once we've created our server socket, we need to write the code that will keep
our script continuously listening to this socket until its termination.
Client Coding: (client.py)

import socket #importing the socket library

host = '127.0.0.1' #host IP address

port = 5001 #assign random port value above 1024(up to 1024 ports are reserved)

obj = socket.socket() #next create a socket object

obj.connect((host,port)) #connecting to the server

message = input("Type message: ")

while message != 'bye':

obj.send(message.encode()) # Send the encoded message to Server

data = obj.recv(1024).decode() # Receive the message from Server, packet size 1024
bytes

print ('Received from server: ' + data)

message = input("Type message: ")

obj.send(message.encode())

obj.close() #close the connection

Server Coding: (server.py)

import socket

host = "127.0.0.1"

port = 5001

server = socket.socket()

server.bind((host,port)) #bind the socket to assigned host IP and port

server.listen() #listen for incoming connections

conn, addr = server.accept() #Establish connection with client

print ("Connection from: " + str(addr))


while True:

data = conn.recv(1024).decode() # Receive the message from Client

if str(data)== 'bye':

print ("Received from Client: " + str(data))

print ("Connection Terminated ")

conn.close()

break

print ("Received from Client: " + str(data))

data = input("Type message: ")

conn.send(data.encode()) # Send the message to Client

Result:

Window 1: (client.py)
Type message: Hai
Received from server: What is your favorite programming language?
Type message: Python
Received from server: OK. Number of Open Source Python Packages is available
in the Web.
Type message: Where it is available?
Received from server: Type "PyPI" in Google.
Type message: bye

Window 2: (server.py)
Connection from: ('127.0.0.1', 54363)
Received from Client: Hai
Type message: What is your favorite programming language?
Received from Client: Python
Type message: OK. Number of Open Source Python Packages is available in the
Web.
Received from Client: Where it is available?
Type message: Type "PyPI" in Google.
Received from Client: bye
Connection Terminated.
12. File Transfer using File Transfer Protocol
Aim: To send files from local host to server using FTP.

Procedure (Server): The server will host the network using the assigned host and port using
which the client will connect to the server. The server will act as the sender and the user will
have to input the filename of the file that he/she would like to transmit. The user must make sure
that the file that needs to be sent is in the same directory as the "server.py" program.

Procedure (Client): The client program will prompt the user to enter the host address of the
server while the port will already be assigned to the port variable. Once the client program has
connected to the server it will ask the user for a filename to be used for the file that will be
received from the server. Lastly the client program will receive the file and leave it in the same
directory under the same filename set as the user.

Server Coding:

import socket

s = socket.socket()
host = socket.gethostname() #Get localhost IP address
port = 8080 #assign port for session
s.bind((host,port)) #bind the socket to assigned host IP and port
s.listen(1) #put the socket into listening mode for 1 client
print(host)
print("Waiting for any incoming connection... ")
conn, addr = s.accept()
print(addr, "Has connected to the server")

filename = input(str("Enter the name of the file to be transmitted: "))


file = open(filename , 'rb') # Opens a file for reading only in binary format
file_data = file.read(1024)
conn.send(file_data)
print("File has been transmitted successfully")
Client Coding:

import socket
s = socket.socket()
host = input(str("Please enter the host address of the sender: "))
port = 8080
s.connect((host,port))
print("Connected ... ")

filename = input(str("Please enter a filename for the incoming file: "))


file = open(filename, 'wb') # Opens a file for writing only in binary format
file_data = s.recv(1024)
file.write(file_data)
file.close()
print("File has been received successfully.")

Result:

Windows 1: (server.py)

Dell (# localhost name)


Waiting for any incoming connection...
('192.168.43.193', 49395) Has connected to the server
Enter the name of the file to be transmitted: ex1.py
File has been transmitted successfully

Windows 2: (client.py)

Please enter the host address of the sender: Dell


Connected ...
Please enter a name for the incoming file: exercise1.py
File has been received successfully.

You might also like