sending more then 2 messages to SocketServer fasils

Roy Smith roy at panix.com
Tue Nov 1 21:23:26 EDT 2011


In article <4eb00a7a$0$6560$9b4e6d93 at newsspool4.arcor-online.net>,
 MrSmile <no at mail.de> wrote:

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

There's a lot of confusing code here.  It would help when asking these 
kinds of questions to reduce it down to the smallest possible example 
that demonstrates your problem.  I'm not sure what this MTTest class is 
all about, but I'm guessing it's not essential to demonstrate what's 
going wrong.  Why not just send strings back and forth?

Also, it would help to have names that made more sense.  I have no idea 
what a MKTest is, or what DSX means.  Names like that make it more 
difficult to read your code and understand what you probably intended.

In any case, I think the problem is a basic misunderstanding of how 
stream sockets work.  You're sending hunks of data with send() calls and 
expecting that the recv() calls will get the same data.  That works with 
UDP (datagram sockets), but not TCP.

There are no record boundaries in TCP.  The system could buffer up the 
data from multiple send() calls and deliver them in a single recv().  Or 
break up one send() into multiple recv() calls.  Or any combination.  In 
any particular situation, the system will probably do whatever is most 
inconvenient, confusing, and damaging to your program :-)

My recommendation is to strip out all the test gunk and get it down to 
the bare network calls.  Send some strings back and forth, and print out 
what each send() call is sending and what each recv() call receives.  
Hopefully, things will start to make more sense then.


> 
> 
> 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



More information about the Python-list mailing list