client2=new QTcpSocket(this);
client2->abort();//add this
client2->connectToHost("127.0.0.1",8989);
- 解決辦法 加上abort()函數(shù)即可; 另外,新的QTcpSocket對象不能在mainWindow中定義。
-官方API說明:
void QAbstractSocket::abort()
- Aborts the current connection and resets the socket. Unlike disconnectFromHost(), this function immediately closes the socket, discarding any pending data in the write buffer.
- 中止當(dāng)前連接并重置套接字。與disconnectFromHost)不同,此函數(shù)立即關(guān)閉套接字,丟棄寫緩沖區(qū)中的任何掛起數(shù)據(jù)。