sending more then 2 messages at a time to a SocketServer fails

M

MrSmile

Hi people!
I have asked myself why I am not capable sending 2 messages a time to a
Socketserver. Why is that?!


Here the Server:

import SocketServer
from ast import literal_eval

class MKTest(object):
DSX = []
MKTestInst = None

def __init__(self,Daten):
MKTest.DSX.append(Daten)

def getObj(Daten):
if MKTest.MKTestInst == None:
MKTest.MKTestInst = MKTest(Daten)
return MKTest.MKTestInst

getObj = staticmethod(getObj)

class MySockX(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
data = literal_eval(data)
#MKTest.getObj(data[0])
#MKObj = MKTest(data[0])
MKObj = MKTest.getObj(data[0])
data = MKTest.DSX
data = '%s' % data
self.request.send(data)

if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST,PORT),MySockX)
server.serve_forever()



and the client:

import socket

data = [100]
received = [None,None]
HOST,PORT = "localhost",9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.send('%s' % data)
received[0] = sock.recv(1024)
sock.send('%s' % data)
received[1] = sock.recv(1024)
sock.close()

print received



The result is:

['[100]', '']



who can help me solving this problem



Tamer
 
M

Miki Tebeka

MKTest.getObj(data[0]) will return the same object on every call(with the same data that was initialized 1'st time). Any Daten parameter after the 1'st call is ignored.
 
M

MRAB

Hi people!
I have asked myself why I am not capable sending 2 messages a time to a
Socketserver. Why is that?!


Here the Server:

import SocketServer
from ast import literal_eval

class MKTest(object):
DSX = []
MKTestInst = None

def __init__(self,Daten):
MKTest.DSX.append(Daten)

def getObj(Daten):
if MKTest.MKTestInst == None:
MKTest.MKTestInst = MKTest(Daten)
return MKTest.MKTestInst

getObj = staticmethod(getObj)

class MySockX(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
data = literal_eval(data)
#MKTest.getObj(data[0])
#MKObj = MKTest(data[0])
MKObj = MKTest.getObj(data[0])
data = MKTest.DSX
data = '%s' % data
self.request.send(data)

if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST,PORT),MySockX)
server.serve_forever()
[snip]

I think that in the 'handle' function you should be putting the code in
a 'while' loop:

def handle(self):
# The client has opened a socket to the server.
while True:
data = self.request.recv(1024)
if not data:
# The client has closed the socket to the server.
break
...
 
T

Tamer Higazi

Am 01.11.2011 17:13, schrieb Miki Tebeka:
MKTest.getObj(data[0]) will return the same object on every call(with the same data that was initialized 1'st time). Any Daten parameter after the 1'st call is ignored.


Not true!
The singleton object has nothing todo. Here one more example for you:

Client:

import socket

data = ['Tamer']
received = [None,None]
HOST,PORT = "localhost",9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.send('%s' % data)
received[0] = sock.recv(1024)
sock.send('%s' % data)
received[1] = sock.recv(1024)
sock.close()

print received


Server:

import SocketServer
from ast import literal_eval

class MySockX(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
data = literal_eval(data)
data = '%s' % data[0]
self.request.send('%s %s' % ('Halloaaa',data))

if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST,PORT),MySockX)
server.serve_forever()



with it's result:

['Halloaaa Tamer', '']



the 2nd argument from the list is EMPTY. Now tell me why?!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top