1. Engineering
  2. Computer Science
  3. do the programs in python and follow the requirements given...

Question: do the programs in python and follow the requirements given...

Question details

Introduction to socket programming --Python sockets

Reference code to start with.

A. Simple server


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

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution