Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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)

Server.py

import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

sock.bind(("127.0.0.1",12345))

print("UDP server is started.....")

print("UDP server is connected to client.....")

while True:

# fetching data from the client side

data,addr = sock.recvfrom(4096)

# sending data to the client side

sock.sendto(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)

sock.close()

Client.py

arr = input("Enter the elements of the arr :").split(" ")

sum_value = int(input("Enter sum value :"))

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))

# 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.

Server.py

import socket

client_socket = socket.socket(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"),("127.0.0.1",12345))

# fetching data from the server side

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))

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 = sock.recvfrom(4096)

# sending data to the client side

sock.sendto(bytes(data).encode('utf-8'),addr)

else:

sock.sendto(byte("Continous message is not acceptable"))


sock.close()

Output:

You might also like