Demo entry 6770491

lab

   

Submitted by anonymous on Nov 13, 2018 at 16:25
Language: Python. Code size: 1.2 kB.

9.Write a client/server program where the client program takes the expression (n1 op
n2 where n1 and n2 are operands and op can be +,-,*,/ ) from the user and sends
the expression to the server program. The server program performs the specified
operation and sends the result to the client program and displays it on the users
console

#server

import socket
import sys

host=socket.gethostname()
port= 9999

soc=socket.socket()
soc.bind((host,port))

soc.listen(5)
print("Listening ...")

while True:
    c,addr=soc.accept()
    print "Got connection from ",addr
    a=int(c.recv(1024))
    c.send("received a")
    op=c.recv(1024)
    c.send("received operator")
    b=int(c.recv(1024))
    r=0
    if op=='+':
        r=a+b
    elif op=='-':
        r=a-b
    elif op=='*':
        r=a*b
    else:
        r=a/b
    c.send("Result "+str(r))
c.close()


#client

import socket
import sys

host=socket.gethostname()
port= 9999

soc=socket.socket()
soc.connect((host,port))

a=raw_input("Enter a no")
b=raw_input("Enter a no")
op=raw_input("Enter a op")

soc.send(a)
print soc.recv(1024) 
soc.send(op)
print soc.recv(1024)
soc.send(b)

print soc.recv(1024)
soc.close()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).