I have a subclass of QTcpSocket. And the problem is that when I connect to the server - everything is fine, but after connecting the socket, restart the server (python socketServer, just close and run the script again). Disconnecting the connector and trying to connect while the server is running, but when I start the server again nothing happened, socket.state () is always in ConnectionState .. what's wrong?
Here is a sample code:
# -*- coding: utf-8 -*- from PyQt4.QtCore import QVariant, QTimer, pyqtSignal, QCoreApplication import sys from PyQt4.QtNetwork import QTcpSocket from re import match import json MAX_WAIT_LEN = 8 class UpQSocket(QTcpSocket): data_ready = pyqtSignal(unicode) def __init__(self): QTcpSocket.__init__(self) self.wait_len = '' self.temp = '' self.setSocketOption(QTcpSocket.KeepAliveOption, QVariant(1)) self.readyRead.connect(self.on_ready_read) self.connected.connect(self.on_connected) self.disconnected.connect(self.on_disconnect) self.error.connect(self.on_error) self.data_ready.connect(self.print_command) def connectToHost(self, host, port): print 'connectToHost' self.temp = '' self.wait_len = '' QTcpSocket.abort(self) QTcpSocket.connectToHost(self, host, port) def close(self): print 'close!' self.disconnectFromHost() def send(self, data): self.writeData('%s|%s' % (len(data), data)) def on_ready_read(self): if self.bytesAvailable(): data = str(self.readAll()) while data: if not self.wait_len and '|' in data:
It displays here:
connectToHost 1 1 connected! data! data! 3 3 3 3 3 error The remote host closed the connection close! disconnected! connectToHost 2 2
source share