date:
updated:
Qt中的TCP服务端和客户端互发消息

作者:hackett
微信公众号:加班猿
废话不多说,上演示效果

由于我们用到socket跟Lamda表达式,所以工程.pro文件需要添加对应的库

为了方便,main.cpp中让程序显示两个Widget窗口,程序运行起来就可以测试
1 2 3 4
| ServerWidget w; w.show(); ClientWidget w2; w2.show();
|
一、服务端
服务端的UI界面布局:
2个PushButton(send,close)
2个textEdit(发送内容,接收内容)

新建一个监听套接字,监听端口号为9999的IP,等待连接,有连接过来提示成功连接同时服务端读就绪
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any, 9999);
setWindowTitle("服务器: 9999");
connect(tcpServer, &QTcpServer::newConnection, [=]() { tcpSocket = tcpServer->nextPendingConnection();
QString ip = tcpSocket->peerAddress().toString(); quint16 port = tcpSocket->peerPort(); QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->textEditRead->setText(temp);
connect(tcpSocket, &QTcpSocket::readyRead, [=]() { QByteArray array = tcpSocket->readAll(); ui->textEditRead->append(array); } ); } );
|
二、客户端
客户端的UI界面布局:
3个PushButton(connect,send,close)
2个lineEdit(端口号,IP)
2个textEdit(发送内容,接收内容)

客户端连接按钮主动与服务端建立连接
1 2 3 4 5 6 7 8 9 10
| void ClientWidget::on_buttonConnect_clicked() { QString ip = ui->lineEditIP->text(); qint16 port = ui->lineEditPort->text().toInt();
tcpSocket->connectToHost(QHostAddress(ip), port);
}
|
连接成功后会触发connected,提示”成功和服务器建立好连接”同时客户端读就绪
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| tcpSocket = new QTcpSocket(this);
setWindowTitle("客户端");
connect(tcpSocket, &QTcpSocket::connected, [=]() { ui->textEditRead->setText("成功和服务器建立好连接"); } );
connect(tcpSocket, &QTcpSocket::readyRead, [=]() { QByteArray array = tcpSocket->readAll(); ui->textEditRead->append(array); }
);
|
源码
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include "serverwidget.h" #include <QApplication> #include "clientwidget.h"
int main(int argc, char *argv[]) { QApplication a(argc, argv); ServerWidget w; w.show();
ClientWidget w2; w2.show();
return a.exec(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include "serverwidget.h" #include "ui_serverwidget.h" #include <QDebug>
ServerWidget::ServerWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ServerWidget) { ui->setupUi(this);
tcpServer = NULL; tcpSocket = NULL;
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any, 9999);
setWindowTitle("服务器: 9999");
connect(tcpServer, &QTcpServer::newConnection, [=]() { tcpSocket = tcpServer->nextPendingConnection();
QString ip = tcpSocket->peerAddress().toString(); quint16 port = tcpSocket->peerPort(); QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port); ui->textEditRead->setText(temp);
connect(tcpSocket, &QTcpSocket::readyRead, [=]() { QByteArray array = tcpSocket->readAll(); ui->textEditRead->append(array); } ); } );
}
ServerWidget::~ServerWidget() { delete ui; }
void ServerWidget::on_buttonSend_clicked() { if(NULL == tcpSocket) { return; } QString str = ui->textEditWrite->toPlainText(); tcpSocket->write( str.toUtf8().data() );
}
void ServerWidget::on_buttonClose_clicked() { if(NULL == tcpSocket) { return; }
tcpSocket->disconnectFromHost(); tcpSocket->close(); tcpSocket = NULL; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #ifndef SERVERWIDGET_H #define SERVERWIDGET_H
#include <QWidget> #include <QTcpServer> //监听套接字 #include <QTcpSocket> //通信套接字
namespace Ui { class ServerWidget; }
class ServerWidget : public QWidget { Q_OBJECT
public: explicit ServerWidget(QWidget *parent = 0); ~ServerWidget();
private slots: void on_buttonSend_clicked();
void on_buttonClose_clicked();
private: Ui::ServerWidget *ui;
QTcpServer *tcpServer; QTcpSocket *tcpSocket;
};
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include "clientwidget.h" #include "ui_clientwidget.h" #include <QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ClientWidget) { ui->setupUi(this);
tcpSocket = NULL;
tcpSocket = new QTcpSocket(this);
setWindowTitle("客户端");
connect(tcpSocket, &QTcpSocket::connected, [=]() { ui->textEditRead->setText("成功和服务器建立好连接"); } );
connect(tcpSocket, &QTcpSocket::readyRead, [=]() { QByteArray array = tcpSocket->readAll(); ui->textEditRead->append(array); }
);
}
ClientWidget::~ClientWidget() { delete ui; }
void ClientWidget::on_buttonConnect_clicked() { QString ip = ui->lineEditIP->text(); qint16 port = ui->lineEditPort->text().toInt();
tcpSocket->connectToHost(QHostAddress(ip), port);
}
void ClientWidget::on_buttonSend_clicked() { QString str = ui->textEditWrite->toPlainText(); tcpSocket->write( str.toUtf8().data() );
}
void ClientWidget::on_buttonClose_clicked() { tcpSocket->disconnectFromHost(); tcpSocket->close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #ifndef CLIENTWIDGET_H #define CLIENTWIDGET_H
#include <QWidget> #include <QTcpSocket> //通信套接字
namespace Ui { class ClientWidget; }
class ClientWidget : public QWidget { Q_OBJECT
public: explicit ClientWidget(QWidget *parent = 0); ~ClientWidget();
private slots: void on_buttonConnect_clicked();
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private: Ui::ClientWidget *ui;
QTcpSocket *tcpSocket; };
#endif
|
如果你觉得文章还不错,记得”点赞关注“
关注我的微信公众号【 加班猿 】可以获取更多内容
