0% found this document useful (0 votes)
23 views7 pages

728 Assign 04

The document describes 3 programs that establish TCP connections between a client and server. The programs send data from client to server for processing and return results. The communication continues until the client sends 'Quit'.

Uploaded by

rohit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

728 Assign 04

The document describes 3 programs that establish TCP connections between a client and server. The programs send data from client to server for processing and return results. The communication continues until the client sends 'Quit'.

Uploaded by

rohit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT–04

NAME-ROHIT KUMAR
REG.NO.- 728
ROLL NO.-CSE/21068
SUBJECT CODE- CSC611
EMAIL:- [email protected]

1. Write a TCP socket program (in C/C++/Java/Python) to establish connection


between client and server. The client program will send an input value n to the
server and the server program will return the sum of the square of first n natural
numbers. Client will display the value send by server. The communication
between client and server will continue until client send ‘Quit’ message to the
server.

Server code:-

import socket

def sum_of_squares(n):
return sum([i**2 for i in range(1, n+1)])

def main():
host = '127.0.0.1'
port = 12345

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


server_socket.bind((host, port))
server_socket.listen(1)

print("Server listening on port:", port)

while True:
conn, addr = server_socket.accept()
print("Connection from:", addr)
while True:
data = conn.recv(1024).decode()
if not data:
break
elif data.lower() == 'quit':
conn.send('Goodbye!'.encode())
conn.close()
break
else:
try:
n = int(data)
result = sum_of_squares(n)
conn.send(str(result).encode())
except ValueError:
conn.send("Invalid input. Please send an integer or 'Quit'
to exit.".encode())

server_socket.close()

if __name__ == "__main__":
main()

Client code:-

import socket

def main():
host = '127.0.0.1'
port = 12345

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


client_socket.connect((host, port))

while True:
n = input("Enter a number (or 'Quit' to exit): ")
client_socket.send(n.encode())
if n.lower() == 'quit':
print(client_socket.recv(1024).decode())
break
else:
print("Sum of squares:", client_socket.recv(1024).decode())

client_socket.close()

if __name__ == "__main__":
main()
Output:-

2. Write a TCP socket program (in C/C++/Java/Python) to establish connection


between client and server. The client program will send a set of binary values to
the server and the server program will return the number of 1s present in the data
received. Client will display the value send by server. The communication
between client and server will continue until client send ‘Quit’ message to the
server.

Server code:-

import socket

def count_ones(data):
return data.count(b'1')

def main():
host = '127.0.0.1'
port = 12346 # Change the port number

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


server_socket.bind((host, port))
server_socket.listen(1)

print("Server listening on port:", port)

while True:
conn, addr = server_socket.accept()
print("Connection from:", addr)

while True:
data = conn.recv(1024)
if not data:
break
elif data.lower() == b'quit':
conn.send(b'Goodbye!')
conn.close()
break
else:
ones_count = count_ones(data)
conn.send(str(ones_count).encode())

server_socket.close()

if __name__ == "__main__":
main()

Client code:-

import socket

def main():
host = '127.0.0.1'
port = 12346

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


client_socket.connect((host, port))

while True:
data = input("Enter a set of binary values (or 'Quit' to exit): ")
if data.lower() == 'quit':
print(client_socket.recv(1024).decode())
break
else:
try:
int(data, 2)
except ValueError:
print("Invalid input. Please enter a set of binary values
(e.g., '101010').")
continue

client_socket.send(data.encode())
ones_count = client_socket.recv(1024).decode()
print("Number of 1s:", ones_count)

client_socket.close()

if __name__ == "__main__":
main()
Output:-

3. Write a TCP socket program (in C/C++/Java/Python) to establish connection


between client and server. The client program will send a postfix expression to the
server and the server program will return the result of the input expression. Server
program will use a stack to evaluate the postfix expression. Client will display the
value send by server. The communication between client and server will continue
until client send ‘Quit’ message to the server.

Server code:-

import socket

def evaluate_postfix(expression):
stack = []
operators = set(['+', '-', '*', '/'])
for token in expression.split():
if token.isdigit():
stack.append(int(token))
elif token in operators:
if len(stack) < 2:
return "Invalid expression"
else:
operand2 = stack.pop()
operand1 = stack.pop()
if token == '+':
result = operand1 + operand2
elif token == '-':
result = operand1 - operand2
elif token == '*':
result = operand1 * operand2
elif token == '/':
if operand2 == 0:
return "Division by zero error"
result = operand1 / operand2
stack.append(result)
else:
return "Invalid token: {}".format(token)
if len(stack) == 1:
return stack.pop()
else:
return "Invalid expression"

def handle_client_connection(client_socket):
while True:
data = client_socket.recv(1024).decode()
if not data:
break
elif data.lower() == 'quit':
client_socket.send(b'Goodbye!')
client_socket.close()
return
else:
result = evaluate_postfix(data)
client_socket.send(str(result).encode())

def main():
host = '127.0.0.1'
port = 12347

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


server_socket.bind((host, port))
server_socket.listen(1)

print("Server listening on port:", port)

while True:
conn, addr = server_socket.accept()
print("Connection from:", addr)
handle_client_connection(conn)

server_socket.close()

if __name__ == "__main__":
main()

Client code:-

import socket

def main():
host = '127.0.0.1'
port = 12347

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


client_socket.connect((host, port))
while True:
expression = input("Enter a postfix expression (or 'Quit' to exit): ")
client_socket.send(expression.encode())
if expression.lower() == 'quit':
print(client_socket.recv(1024).decode())
break
else:
result = client_socket.recv(1024).decode()
print("Result:", result)

client_socket.close()

if __name__ == "__main__":
main()

Output:-

You might also like