服务端代码
#pragma execution_character_set("utf-8")
class MyTcpSocket : public QTcpSocket
MyTcpSocket(QObject *parent = 0);
void sigSocketRecvData(QString, int);
void sigSocketDisconnect(int);
void OnReadyReadSignal();
void OnDisconnectedSignal();
int MyTcpSocket::m_nIndex = 0;
MyTcpSocket::MyTcpSocket(QObject *parent):
connect(this, SIGNAL(readyRead()), this, SLOT(OnReadyReadSignal()));
connect(this, SIGNAL(disconnected()), this, SLOT(OnDisconnectedSignal()));
MyTcpSocket::~MyTcpSocket()
void MyTcpSocket::ServerWriteData()
QString strMsg = QString("World ... %1").arg(m_nIndex++);
int nSendRet = write(strMsg.toLatin1(), strMsg.length());
qDebug() << "Failed to send data " << strMsg;
qDebug() << "Send data: " << strMsg << endl;
void MyTcpSocket::OnReadyReadSignal()
qDebug() << "On singal ... readyRead";
while(this->bytesAvailable() > 0)
int length = bytesAvailable();
QString message = QString(QLatin1String(buf));
qDebug() << "socket recv: " << message;
emit sigSocketRecvData(message, length);
void MyTcpSocket::OnDisconnectedSignal()
qDebug() << "On singal ... Disconnected!";
emit sigSocketDisconnect(this->socketDescriptor());

class MyTcpServer : public QTcpServer
explicit MyTcpServer(QObject *parent = 0);
void MyTcpServerListen();
void incomingConnection(qintptr socketDescriptor);
void sigServerRecvData(QString, int);
void OnSocketRecvDataSignal(QString,int);
void OnSocketDisconnectSignal(int);
MyTcpServer::MyTcpServer(QObject *parent) :
MyTcpServer::~MyTcpServer()
for (int n = 0; n < tcpSocketList.size(); ++n)
MyTcpSocket *item = (MyTcpSocket *)tcpSocketList.at(n);
qDebug() << "MyTcpServer destruct ... " << n;
void MyTcpServer::MyTcpServerListen()
this->listen(QHostAddress::Any, m_nPort);
qDebug() << "Start listening port:" << m_nPort;
waitForNewConnection(10000);
void MyTcpServer::incomingConnection(qintptr socketDescriptor)
qDebug()<< "new client connect ... ";
MyTcpSocket *tcpSocket = new MyTcpSocket(this);
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
qDebug() << "IP: " << ip << " ... Port: " << (uint16_t)port << endl;
connect(tcpSocket, SIGNAL(sigSocketRecvData(QString,int)), this, SLOT(OnSocketRecvDataSignal(QString,int)));
connect(tcpSocket, SIGNAL(sigSocketDisconnect(int)), this, SLOT(OnSocketDisconnectSignal(int)));
tcpSocket->setSocketDescriptor(socketDescriptor);
tcpSocketList.append(tcpSocket);
tcpSocket->waitForDisconnected(8000);
void MyTcpServer::OnSocketRecvDataSignal(QString message, int length)
qDebug() << "server recv ..." << message;
void MyTcpServer::OnSocketDisconnectSignal(int descriptor)
qDebug() << "server disconnect ..." ;
for(int i = 0; i < tcpSocketList.count(); i++)
QTcpSocket *temp = tcpSocketList.at(i);
if(temp->socketDescriptor() == descriptor)
tcpSocketList.removeAt(i);
qDebug() << "temp destruct ..." << i;

int main(int argc, char *argv[])
QCoreApplication a(argc, argv);
MyTcpServer *tcpServer = new MyTcpServer(nullptr);
tcpServer->MyTcpServerListen();
qDebug() << "server over ...";
客户端代码
#pragma execution_character_set("utf-8")
class MyTcpClient : public QObject
MyTcpClient(QObject *parent);
void OnDisconnectSignal();
void OnReadyReadSignal();
QTcpSocket *m_pTcpSocket;
MyTcpClient::MyTcpClient(QObject *parent)
MyTcpClient::~MyTcpClient()
if (NULL != m_pTcpSocket)
qDebug() << "MyTcpClient destruct";
void MyTcpClient::MyTcpConnect()
qDebug() << "client connect ... ";
m_pTcpSocket = new QTcpSocket(this);
connect(m_pTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyReadSignal()));
m_pTcpSocket->connectToHost(m_strIP, m_nPort);
m_pTcpSocket->waitForConnected(30000);
void MyTcpClient::MyTcpDisconnect()
void MyTcpClient::MyWriteData()
QString strMsg = QString("Hello ... %1").arg(m_nIndex++);
char bufferMsg[1024] = { 0 };
strcpy_s(bufferMsg, strMsg.toStdString().c_str());
int nSendRet = m_pTcpSocket->write(bufferMsg, strlen(bufferMsg));
m_pTcpSocket->waitForBytesWritten();
qDebug() << "Send data: " << strMsg;
qDebug() << "Failed to send data " << strMsg;
void MyTcpClient::OnDisconnectSignal()
qDebug() << "client disconnect ... ";
m_pTcpSocket->waitForDisconnected(1000);
m_pTcpSocket->disconnectFromHost();
void MyTcpClient::OnReadyReadSignal()
while (m_pTcpSocket->bytesAvailable() > 0)
datagram.resize(m_pTcpSocket->bytesAvailable());
m_pTcpSocket->read(datagram.data(), datagram.length());
QString message = datagram.data();
qDebug() << "Recv data: "<< message;

int main(int argc, char *argv[])
QCoreApplication a(argc, argv);
MyTcpClient* client = new MyTcpClient(nullptr);
for (int n = 0; n < 3; n++)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::this_thread::sleep_for(std::chrono::seconds(1));
client->MyTcpDisconnect();
qDebug() << "client over ...";