19BIT0123 Akash Sharma
Write a simple UDP server and client to evaluate “count triplets with sum smaller than a given
value”. The client-server system has the following functionalities: For Example: Input : arr[] = {5, 1, 3,
4, 7} sum = 12. Explanation : Below are triplets with sum less than 12 (1, 3, 4), (1, 3, 5), (1, 3, 7) and
(1, 4, 5)
[Link]
import socket
sock = [Link](socket.AF_INET,socket.SOCK_DGRAM)
[Link](("[Link]",12345))
print("UDP server is started.....")
print("UDP server is connected to client.....")
while True:
# fetching data from the client side
data,addr = [Link](4096)
# sending data to the client side
[Link](bytes(output).encode('utf-8'),addr)
# manipulating data recieved from the client side
arr = data[0]
sum_value = data[1]
n = len(data[0])
# function for count triplets with sum smaller than a given value”
def countTriplets(arr, n, sum):
ans = 0
for i in range( 0 ,n-2):
for j in range( i+1 ,n-1):
for k in range( j+1, n):
if (arr[i] + arr[j] + arr[k] < sum):
ans+=1
return ans
output = countTriplets(arr, n, sum_value)
[Link]()
[Link]
arr = input("Enter the elements of the arr :").split(" ")
sum_value = int(input("Enter sum value :"))
array=[arr,sum_value]
import socket
client_socket = [Link](socket.AF_INET,socket.SOCK_DGRAM)
# sending data to the server side
client_socket.sendto((array).endcode("utf-8"),("[Link]",12345))
# fetching data from the server side
data,addr = client_socket.recvfrom(4096)
print(str(data))
client_socket.close()
Output:
Write a udp chat program to restrict only alternative messages. If we send continuous message it
shows an error like not accepting continuous message. (1,3,5,7 ,9) Ex : alternative messages like
(1,3,5,7,9) messages.
[Link]
import socket
client_socket = [Link](socket.AF_INET,socket.SOCK_DGRAM)
while True:
message = input("Enter the elements of the arr :")
# sending data to the server side
client_socket.sendto(().endcode("utf-8"),("[Link]",12345))
# fetching data from the server side
data,addr = client_socket.recvfrom(4096)
print(str(data))
client_socket.close()
[Link]
import socket
sock = [Link](socket.AF_INET,socket.SOCK_DGRAM)
[Link](("[Link]",12345))
print("UDP server is started.....")
print("UDP server is connected to client.....")
count = 0
while True:
count=count+1
if(count%2!=0):
# fetching data from the client side
data,addr = [Link](4096)
# sending data to the client side
[Link](bytes(data).encode('utf-8'),addr)
else:
[Link](byte("Continous message is not acceptable"))
[Link]()
Output: