[Tutor] SocketServer module not creating a TCP server
rashi gupta
rashigupta77@hotmail.com
Tue, 8 Jan 2002 17:01:30 +0530
While creating the client/server application using the SocketServer module,
the server is handling just one request from a client. It is however,
connecting to multiple clients at one time. Moreover the rfile.read()
function makes both the client and the server go into a hang. I am not too
sure whether the serve_forever method can handle multiple clients but single
request from each or multiple requests from the same client. The server and
client programs are as follows:
#server program
import SocketServer
from time import sleep
port=8888
class myR(SocketServer.StreamRequestHandler):
def handle(self):
print "connection from",self.client_address
try:
self.rfile.read()
self.wfile.write("SocketServer works!")
except IOError:
print "Connection from the client ",self.client_address,"
closed"
srvsocket=SocketServer.TCPServer(("",port),myR)
print "the socket is listening to the port", port
srvsocket.serve_forever()
#client program
from socket import *
Hostname = 'localhost'
PortNumber = 8888
Buffer = 500
ServerAddress = (Hostname, PortNumber)
TCP_Client_Socket = socket(AF_INET, SOCK_STREAM)
TCP_Client_Socket.connect(ServerAddress)
while 1:
print 'The client is conencted to the server'
DataStr = raw_input('Enter data to send to the server: ')
if not DataStr:
print 'The client has entered nothing; hence the connection to the
server is closed'
break
TCP_Client_Socket.send(DataStr)
ServerData = TCP_Client_Socket.recv(Buffer)
if not ServerData:
print 'The server has sent nothing'
break
print 'The server has sent this data string: ', ServerData
TCP_Client_Socket.close()