Question: 4 SKET 4623 - SKET 4623 Network Programming, Final Exam Question 2 2 author Kamalny TOPMultithreads Server Socket.ly 4 impact Socket G import sys 7


4 SKET 4623 - SKET 4623 Network Programming, Final Exam Question 2 2 author Kamalny TOPMultithreads Server Socket.ly 4 impact Socket G import sys 7 from threading import Thread 8 from Socket Server import ThreadingMixin 9 10 class Client Thread Thread): 12 def __init__(self, clientip, client Port, connection): 12 Thread.init__(self) 23 self.ip = clientip self.port = client Port 25 self.connection connection print >>sys.stdout, "New server socket thread started for us clientip 17 19 - der run(self): 19 while True: 2e data connection, recy(1024) 21 1f data: 22 print 'Dato received from connected client: str(data) 23 print >>sys.stdout, "Data received from connected client: "" data 24 if data-na: 25 break 26 Server process data = stridata). upper() 28 *print 'Sending data back to the client str(data) 29 print >>sys.stdout, 'Sending data back to the client: "" data 30 connection.send(data) 31 else: 32- break 33 Clean up and close the connection 34 - print Close connection from client'striclient_address) 35 print >>sys.stdout, Close connection fron client clientip 36 print >>sys.stdout, Connection from', clientip 37 - connection.close() 38 39 - def Main(): 40 hostname = 127.0.0.1 41 port = 5000 42 connection.. 43 # Create a TCP/IP socket 44 try: 45 socket Server socket.socket (socket. AF_INET, socket.SOCK_STREAM) 46 socketServer.setsockopt(socket. SOL_SOCKET, socket.50_REUSEADDR, 1) 47 #Bind the socket to the port 48 socketServer.bind(hostname, port)) 49 threads = ] 50 # Listen for incoming connections 51 socket Server, listen (4) 52 except socket.error, errormassage: 53 print "Error establishing socket connection: 4" *errormassage 54 Wait for a connection SS print >>sys.stderr, "Multithreaded server is waiting for a connection from client 56 (connection, (clientip.client Port)) = socketServer.accept() 57 newthread = Client Thread(clientip, client Port, connection) 58 - newthread.start() 59 60 - if _name__=__main__': Main() (i) Figure Q.2 Provide method used to create multithreading. (2 marks) Explain the purposes of try and except blocks in socket programming? (ii) 5 SKET 4623 (iii) (iv) (4 marks) Identify the the main class in TCPMultiThreadsServerSocket.py (3 marks) What is the purposes of init in line 11: def __init__(self, clientIP, client Port, connection): (4 marks) Explain the purposes of the command in line 46 in Figure Q.2? (3 marks) (v)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
