Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What more info do you require? The server class implements a server socket that can handle multiple client connections. It is really important to handle

What more info do you require?

The server class implements a server socket that can handle multiple client connections. It is really important to handle any exceptions that may occur because other clients are using the server too, and they may be unaware of the exceptions occurring. So, the server must not be stopped when a exception occurs. A proper message needs to be shown in the server console.

Create a TCP Server socket. This socket accepts clients client connections, and provide services to handle them in the server-side.

Finish the code by doing the TODO in Bold

import socket import pickle from threading import Thread

class Server(object): MAX_NUM_CONN = 10 # keeps 10 clients in queue

def __init__(self, host="127.0.0.1", port = 12000): """ Class constructor :param host: by default localhost. Note that '0.0.0.0' takes LAN ip address. :param port: by default 12000 """ self.host = host self.port = port self.serversocket = None # TODO: create the server socket

def _bind(self): """ # TODO: bind host and port to this server socket :return: VOID """

def _listen(self): """ # TODO: puts the server in listening mode. # TODO: if succesful, print the message "Server listening at ip/port" :return: VOID """ try: self._bind() # your code here except: self.serversocket.close()

def _handler(self, clienthandler): """ #TODO: receive, process, send response to the client using this handler. :param clienthandler: :return: """ while True: # TODO: receive data from client # TODO: if no data, break the loop # TODO: Otherwise, send acknowledge to client. (i.e a message saying 'server got the data

def _accept_clients(self): """ #TODO: Handle client connections to the server :return: VOID """ while True: try: clienthandler, addr = self.serversocket.accept() # TODO: from the addr variable, extract the client id assigned to the client # TODO: send assigned id to the new client. hint: call the send_clientid(..) method self._handler(clienthandler) # receive, process, send response to client. except: # handle exceptions here

def _send_clientid(self, clienthandler, clientid): """ # TODO: send the client id to a client that just connected to the server. :param clienthandler: :param clientid: :return: VOID """

def send(self, clienthandler, data): """ # TODO: Serialize the data with pickle. # TODO: call the send method from the clienthandler to send data :param clienthandler: the clienthandler created when connection was accepted :param data: raw data (not serialized yet) :return: VOID """

def receive(self, clienthandler, MAX_ALLOC_MEM=4096): """ # TODO: Deserialized the data from client :param MAX_ALLOC_MEM: default set to 4096 :return: the deserialized data. """ return None #change the return value after implemented.

def run(self): """ Already implemented for you Run the server. :return: VOID """ self._listen() self._accept_clients()

# main execution if __name__ == '__main__': server = Server() server.run()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

Describe somatic symptom and related disorders.

Answered: 1 week ago