Create a multithreaded client/server program. Each client should send a message to the server. The server then echoes the message, in all uppercase letters, back to the client.
What will be an ideal response?
```
# Server and client threads.
import socket
import threading
class serverThread( threading.Thread ):
"""Create a server thread to handle client requests"""
def __init__( self ):
"""Initialize socket connection"""
HOST = "127.0.0.1"
PORT = 5556
threading.Thread.__init__( self )
self.threadingCondition = threading.Condition()
# create server socket
self.serverSocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM )
# bind socket to address
self.serverSocket.bind( ( HOST, PORT ) )
# listen for connection - allow five
self.serverSocket.listen( 5 )
def run( self ):
"""Receive client message, capitalize it and
send it back to the client"""
while 1:
# accept client connection
self.clientSocket, addr = self.serverSocket.accept()
self.threadingCondition.acquire() # acquire lock
clientMessage = self.clientSocket.recv( 1024 )
# send message with all uppercase letters back to client
self.clientSocket.send( clientMessage.upper() )
if clientMessage == "kill server":
break
self.threadingCondition.notify() # notify waiting thread
self.threadingCondition.release() # release lock
self.clientSocket.close()
self.serverSocket.close()
class clientThread( threading.Thread ):
"""Create a client thread"""
def __init__( self, message ):
"""Initialize the socket connection"""
HOST = "127.0.0.1"
PORT = 5556
self.message = message
threading.Thread.__init__( self )
self.threadCondition = threading.Condition()
# create client socket
self.clientSocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM )
# connect socket to address
self.clientSocket.connect( ( HOST, PORT ) )
def run( self ):
"""Send server a message and print response"""
self.threadCondition.acquire() # acquire lock
print self.getName(), " sent to the server: ", \
self.message
self.clientSocket.send( self.message )
serverMessage = self.clientSocket.recv( 1024 )
print self.getName(), " received from the server: ", \
serverMessage
self.threadCondition.notify() # wake up waiting thread
self.threadCondition.release() # allow lock to be acquired
self.clientSocket.close()
def main():
# start the server thread
serverThread().start()
# create and start two client threads
thread1 = clientThread( "message one" )
thread2 = clientThread( "message two" )
# thread to end server execution
killThread = clientThread( "kill server" )
thread1.start()
thread2.start()
killThread.start()
if __name__ == "__main__":
main()
```
You might also like to view...
What is the Server Core version of Windows Server 2008?
What will be an ideal response?
The _________ operator returns the string representation of an identifier’s name.
Fill in the blank(s) with the appropriate word(s).
When WPA2 is configured on the access point, all wireless clients must use WPA2
Indicate whether the statement is true or false
Which of the following is a recommended way of informing users that a form is read-only?
A. Include a note in the form title. B. Have a message box appear any time the form is opened. C. There is no need to inform the user. D. There is never a need to make a form read-only.