Professional Documents
Culture Documents
DCCN Labcat
DCCN Labcat
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)
Server.py
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(("127.0.0.1",12345))
while True:
data,addr = sock.recvfrom(4096)
sock.sendto(bytes(output).encode('utf-8'),addr)
arr = data[0]
sum_value = data[1]
n = len(data[0])
# function for count triplets with sum smaller than a given value”
ans = 0
ans+=1
return ans
sock.close()
Client.py
array=[arr,sum_value]
import socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# sending data to the server side
client_socket.sendto((array).endcode("utf-8"),("127.0.0.1",12345))
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.
Server.py
import socket
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
client_socket.sendto(().endcode("utf-8"),("127.0.0.1",12345))
data,addr = client_socket.recvfrom(4096)
print(str(data))
client_socket.close()
Server.py
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(("127.0.0.1",12345))
count = 0
while True:
count=count+1
if(count%2!=0):
data,addr = sock.recvfrom(4096)
sock.sendto(bytes(data).encode('utf-8'),addr)
else:
Output: