Socket Prog
Socket Prog
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.
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.
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)
port = 5001 #assign random port value above 1024(up to 1024 ports are reserved)
data = obj.recv(1024).decode() # Receive the message from Server, packet size 1024
bytes
obj.send(message.encode())
import socket
host = "127.0.0.1"
port = 5001
server = socket.socket()
if str(data)== 'bye':
conn.close()
break
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")
import socket
s = socket.socket()
host = input(str("Please enter the host address of the sender: "))
port = 8080
s.connect((host,port))
print("Connected ... ")
Result:
Windows 1: (server.py)
Windows 2: (client.py)