Write a UDP Server Python program to keep the track of external clients and store their IP address as well as Port number.

  The following program will help you to use the UDP Server using python and keep the track of external clients and store their IP address as well as port number.

Python Program

UDP Server

   
 
/*;==========================================
; Title: Write a UDP Server Python program to keep the track of external clients and store their IP address as well as Port number.
; Author: codenaive Santosh Kumar  <admin@codenaive.com> <s.8506.a@gmail.com>
; Date:   18 Dec 2021
;==========================================*/
import socket
MyLocalIP     = "127.0.0.1"  # Local IP
MyLocalPort   = 20001  # local Port
bSize  = 1024  # buffer size 

serverToClientMSG = "Hi, Thanks for joining Server";
bytesToClient = str.encode(serverToClientMSG)

IP_Address_caller=[];
Port_caller=[];
NotExist=True;
 
# First, Create datagram socket
UDP_Server_Socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDP_Server_Socket.bind((MyLocalIP, MyLocalPort))
 # Listen for incoming datagram packet
print("UDP server start and listening for clients.")
while(True):
    print("Listening...............\n")
    AddressPairbytes = UDP_Server_Socket.recvfrom(bSize)
    msg = AddressPairbytes[0]
    address = AddressPairbytes[1]
    clientMessage = "Message from Client:{}".format(msg)
    clientIP  = "Client IP Address:{}".format(address)
    # checking the client is on same server or not.
    if MyLocalIP != address[0]:
        for j in range(len(IP_Address_caller)):
            if IP_Address_caller[j]==address[0]:
                NotExist=False;
            
    else:
        NotExist=False;
        print("Client from the same server.\n");
        
    # check wether the client exist in the same server or not, if it doesn't exist then store their IP address and Port in List of an array.
    if NotExist == True:
        IP_Address_caller.append(address[0]);
        Port_caller.append(address[1])
    else:
        NotExist=True;
        print("client IP address already existing.\n");
       
    # print IP address and Port of external Client.
    print("\nIP Address with Port e.g <sever_id> and <port_Number> : ");
    for i in range(len(IP_Address_caller)):
        print(IP_Address_caller[i],' ',Port_caller[i]);

    #reply to client
    UDP_Server_Socket.sendto(bytesToClient, address)
							
												

UDP Client

   
 
/*;==========================================
; Title: Write a UDP Server Python program to keep the track of external clients and store their IP address as well as Port number.
; Author: codenaive Santosh Kumar  <admin@codenaive.com> <s.8506.a@gmail.com>
; Date:   18 Dec 2021
; Programming Language: Python
;==========================================*/							
import socket
MyLocalIP      = "127.0.0.1"
MyLocalPort   = 5000
msgFromClient       = "Sending (IP address and PORT)";
bytesToServer         = str.encode(msgFromClient)
server_Address_Port   = ("127.0.0.1", 20001)
bSize          = 1024


#Create UDP socket at client side

Client_UDPSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
Client_UDPSocket.bind((MyLocalIP , MyLocalPort))
Client_UDPSocket.sendto(bytesToServer, server_Address_Port)
messageFromServer = Client_UDPSocket.recvfrom(bSize)
message = "Message from Server {}".format(messageFromServer[0])
print(message);
Client_UDPSocket.close();


							
							
							

Output

External Client IP Address and Port  as <sever_id> and <port_Number> :
127.0.0.7   5500
Listening...............


External Client IP Address and Port  as <sever_id> and <port_Number> :
127.0.0.7   5500
127.0.0.9   5500
Listening...............


External Client IP Address and Port  as <sever_id> and <port_Number> :
127.0.0.7   5500
127.0.0.9   5500
127.0.0.10   5000
Listening...............

Client from the same server.

client IP address already existing.


External Client IP Address and Port  as <sever_id> and <port_Number> :
127.0.0.7   5500
127.0.0.9   5500
127.0.0.10   5000
Listening...............

  
						 

Leave a Reply

Your email address will not be published. Required fields are marked *