开发者

Python lost package

I trying to write a client server application in Python, but I faced a problem, in the client side I'm not getting all the sent data. First I tried to send the numbers 1 to 10 and I received 1,2,5,6,10, so there are missing a lot of numbers.

Server side:

def __init__( self ):
        super( MCCommunication, self ).__init__()

        HOST, PORT = socket.gethostbyname( socket.gethostname() ), 31000
        self.server = SocketServer.ThreadingTCPServer( ( HOST, PORT ), MCRequestHandler )
        ip, port = self.server.server_address        
        # Start a thread with the server
        # Future task: Make the server a QT-Thread...
        self.server_thread = threading.Thread( target = self.server.serve_forever )
        # Exit the server thread when the main thread terminates
        self.server_thread.setDaemon( True )
        self.textUpdated.emit( 'Server Started!' )
        print( 'Server Started!' )
        self.server_thread.start() 

def handle( self ):       
        #self.request.setblocking( 0 )
        i = 10;
        while True:
            if( self.clientname == 'MasterClient' ):                
                try:                    
                    #ans = self.request.recv( 4096 )
                    #print( 'after recv' )   
                    """    Sendign data, testing purpose    """                 
                    while i:
                        mess = str( i );            
                        postbox['MasterClient'].put( self.creatMessage( 0, 0 , mess ) )
                        i = i - 1

                    while( postbox['MasterClient'].empty() != True ):                        
                        sendData = postbox['MasterClient'].get_nowait()                        

                        a = self.request.send( sendData )
                        print( a );
                        #dic = self.getMessage( sendData )
                        #print 'Sent:%s\n' % str( dic )                         
                except:                    
                    mess = str( sys.exc_info()[0] )
                    postbox['MasterClient'].put( self.creatMessage( 1, 0 , mess ) )
                    pass

    def creatMessage( self, type1 = 0, type2 = 0, message = ' ', extra = 0 ):        
        return pickle.dumps( {"type1":type1, "type2":type2, "message":message, "extra":extra} );

Where the postbox['MasterClient'] is a Queue with the serialized message.

And this is the client:

def run( self ):        
        sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )        
        addr = ( self.ip, self.port )        
        #print addr
        sock.connect( addr )
        #sock.setblocking( 0 )
        while True:         
            try:   
                ans = sock.recv( 4096 )                
                dic = self.getMessage( ans )               
      开发者_运维问答          self.recvMessageHandler( dic )  
                print 'Received:%s\n' % str( dic )       
            except:
               pass


The server may have sent multiple messages by the time the client attempts to read them, and if these fit within the same 4k buffer, the recv() call will obtain both of them.

You don't show the getMessage code, but I'd guess you're doing something like pickle.loads(msg), but this will only give you the first message and discard the rest of the string, hence the dropped messages. You'll also get another issue if more than 4096 bytes are buffered by the time you read, as you could end up getting a fragment of a message and thus an unpickling error.

You'll need to break up the string you get back into seperate messages, or better, just treat the socket as a stream and let pickle.load pull a single message from it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜