tags:

views:

281

answers:

1

Hello, I'm struggling with following error:

"socket.error: [10053] 'Software caused connection abort'"

Traceback (most recent call last): ... self.send(json.dumps([0x02, subItems])+"\n") ... sent = self.handler.send(data) socket.error: [Errno 10053]

I established server properely, I can connect to it, send "hello" message, then I can send one more 0x1 type message. Next client actions are not being seen by server side. Client program throws above socket.error .

import SocketServer

class ThreadedTCPRequestHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        #data = json.loads(self.request.recv(1024))
        if(self.request in subengine.sub.get_all_sub()):
            pass
        else:
            #login = json.loads(self.request.recv(1024))
            login = json.loads(self.rfile.readline().strip())
            logging.debug("User : %s connected" % login )

        data = json.loads(self.rfile.readline().strip())
        if(data[0]==0x1): # subscribe
            logging.info("subscribe message received, from %d" % self.request.fileno())
            logging.info("symbols to subscribe: %s" % data[1])
        elif (data[0] == 0x2): # unsubscribe
            logging.info("unsubscribe message received")
        else:
            pass

#    def finish(self):
#        logging.info("terminating connection")

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

server = ThreadedTCPServer(('localhost',5555), ThreadedTCPRequestHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()

client side code example:

class Client:
    handler = None

    def __init__(self, uname = '', host = 'localhost', port = 0, sock = None):
        if sock is None:
            self.handler = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = host
        self.port = port
        self.uname = uname

    def feed_conection(self):
        self.handler.connect((self.host, self.port))
        if(self.handler != 0):
            self.handler.send(json.dumps(self.uname)+"\n")
        else:
            print ("Nie udalo sie polaczyc")

    def sub(self, subItems):
        self.send(json.dumps([0x01, subItems])+"\n")

    def unsub(self, subItems):
        self.send(json.dumps([0x02, subItems])+"\n")

    def send(self, data):
        data_len = len(data)
        total_sent = 0
        while(total_sent < data_len):
            sent = self.handler.send(data)
            if not sent:
                break
            data = data[sent:]
            total_sent += sent
A: 

If this, as you report, is really how your main server code ends...:

server_thread.setDaemon(True)
server_thread.start()

then your code is saying: let server_thread just die when the main thread dies, let server_thread start, and now let the main thread die -- i.e., are you really "falling off the end" right after starting a daemonized thread? That's just wrong -- as soon as the OS switches back to your main thread, boom, everything on the server side just dies, exactly as you programmed it to (so of course client code trying to continue a conversation with that server will see problems!-). Why are you making server_thread a daemon and yet letting the main thread just end? Maybe you're not really posting the actual code you're using...?

Alex Martelli
You were very close. I've just find some work around...but first.Main thread won't finish because I have serve_forever as a main function for thread (which I suppose Is regular "infinite" accept()). But In client code from my post I have two "reads", and thats why it was working... :), adding "while(cond)" to "def handle(self):" solved that.
bua