def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() block = QByteArray() outstr = QDataStream(block, QIODevice.WriteOnly) outstr.setVersion(QDataStream.Qt_4_0) while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() outstr.writeUInt16(0) outstr.writeQString("Message to Server") outstr.device().seek(0) outstr.writeUInt16(block.size() - 2) socket.write(block) # socket.write(block) fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
def run(self): tcpSocket = QTcpSocket() if not tcpSocket.setSocketDescriptor(self.socketDescriptor): self.error.emit(tcpSocket.error()) return block = QByteArray() outstr = QDataStream(block, QIODevice.WriteOnly) outstr.setVersion(QDataStream.Qt_4_0) outstr.writeUInt16(0) outstr.writeQString(self.text) outstr.device().seek(0) outstr.writeUInt16(block.size() - 2) tcpSocket.write(block) tcpSocket.disconnectFromHost() tcpSocket.waitForDisconnected()
class Server(QTcpServer): def __init__(self): super(Server, self).__init__() self.socket = QTcpSocket() self.socket.readyRead.connect(self.read_receive_data) self.socket.disconnected.connect(self.disconnect_socket) def read_receive_data(self): data = self.socket.readAll() utf_str = data.data().decode() # format to json json_data = json.loads(utf_str) event_dispatcher.emit_event(event_key.RECV_LOG, json_data) def disconnect_socket(self): event_dispatcher.emit_event(event_key.DISCONNECT_CLIENT, "null") def incomingConnection(self, socket_desc): if not self.socket.setSocketDescriptor(socket_desc): self.error.emit(self.socket.error()) print("faild create socket") return event_dispatcher.emit_event(event_key.CONNECT_CLIENT, "null")