Question: Write a code in python by making modifications to the code given below Reference code to start with. A. Simple server #!/usr/bin/python import socket #
Write a code in python by making modifications to the code given below
Reference code to start with.
A. Simple server
#!/usr/bin/python
import socket # Import socket module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # Create a socket object
host = socket.gethostbyname(socket.gethostname()) # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print 'host ip', host
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close() # Close the connection
B. Simple client
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name/for this example
ports = 12345 #server port
portc = 32451 #client's own port for incoming connections (if any)
s.bind((host, portc))
s.connect((host, ports))
print s.recv(1024)
s.close # Close the socket when done
# Following would start a server in background.
$ python server.py &
# Once server is started run client as follows:
$ python client.py
Lab 1 Tasks to be done.
Task 1 : Modify the above code to make the client respond back to the server with an acknowledgement for the message it received from the server. Something like, "It was nice talking to you".
Task 2 : Distributed Set intersection:
This task requires you to work with another student. First find out the ip address of your neighbor's machine with ifconfig (Linux) and ipconfig (Windows). Then modify the existing code to reflect that you are connecting to your neighbor's machine. Execute the following task.
Ask the client to generate a random set of numbers (say 25 values between 1 and 100) and send them to server. The server generates a similar set of numbers between 1 and 100. The server identifies the common numbers in both sets and sends them back to the client. The server should sort the numbers and send them back to the client and the client should print the final output.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
