#!pythonfrom socket import*import time
from func_timeout import func_set_timeout
import func_timeout
import sys # In order to terminate the program
clientSocket = socket(AF_INET, SOCK_DGRAM)
clientSocket.settimeout(1)#Prepare a sever socket
serverName=''
serverPort=12000
serverName='localhost'# serverName=raw_input('ping :')defping():
clientSocket.sendto('hello'.encode(),(serverName,serverPort))
modifiedMessage,ServerAddress = clientSocket.recvfrom(2048)
i=0while i<10:
i=i+1
error=0try:print'ping '+serverName+' in times :',
start=time.time()
ping()
end=time.time()printformat((end-start),'.3f'),'s'except Exception as e:print("Time out")
#!python# UDPPingerServer.py# We will need the following module to generate randomized lost packetsimport random
from socket import*# Create a UDP socket # Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)# Assign IP address and port number to socket
serverSocket.bind(('',12000))whileTrue:# Generate random number in the range of 0 to 10
rand = random.randint(0,10)# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)# Capitalize the message from the client
message = message.upper()# If rand is less is than 4, we consider the packet lost and do not respondif rand <4:continue# Otherwise, the server responds
serverSocket.sendto(message, address)