QT之TCP/IP 通信:
很简单不多说,直接上代码。
服务器:
#include "servewidget.h"
#include "ui_servewidget.h"
ServeWidget::ServeWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServeWidget)
{
ui->setupUi(this);
//初始化套接字
tcpServer = NULL;
tcpSocket = NULL;
//监听套接字,指定父对象自动回收空间
tcpServer = new QTcpServer(this);
//设定端口
tcpServer->listen(QHostAddress::Any,8888);
//新连接建立的信号槽
connect(tcpServer,&QTcpServer::newConnection,[=]()
{
//取出建立好连接的套接字
tcpSocket = tcpServer->nextPendingConnection();
//获取对方的IP和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
//将信息显示到UI
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui->textEditRead->setText(temp);
//连接建立后,读取到数据的信号槽
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//从通信套接字取出内容
QByteArray array = tcpSocket->readAll();
//显示到UI
ui->textEditRead->append(array);
} );
});
setWindowTitle("TCP服务器,端口:8888");
}
ServeWidget::~ServeWidget()
{
delete ui;
}
void ServeWidget::on_ButtonSend_clicked()
{ if(NULL == tcpSocket)
{return;}
//获取编辑区内容
QString str = ui->textEditWrite->toPlainText();
//给对方发数据,使用的套接字为tcpSocket
tcpSocket->write( str.toUtf8().data() );
}
void ServeWidget::on_ButtonClose_clicked()
{ if(NULL == tcpSocket)
{return;}
//主动和客户端断开连接
tcpSocket->write("断开连接");
tcpSocket->disconnectFromHost();
//关闭套接字
tcpSocket->close();
tcpSocket = NULL;
ui->textEditRead->append("断开连接");
}
客户端:
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include<QHostAddress>
#include <QDebug>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
//初始化套接字
tcpSocket = NULL;
//分配空间,指定父对象
tcpSocket = new QTcpSocket(this);
//建立连接的信号槽
connect(tcpSocket,&QTcpSocket::connected,
[=]()
{
ui->textEditRead->setText("恭喜,成功连接服务器!");
}
);
//连接建立后收到服务器数据的信号槽
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//获取对方发送的内容
QByteArray array = tcpSocket->readAll();
//显示到编辑框
ui->textEditRead->append(array);//append,添加内容
}
);
//设置窗口标题
setWindowTitle("TCP客户端v");
}
ClientWidget::~ClientWidget()
{
delete ui;
}
void ClientWidget::on_buttonConnect_clicked()
{
//获取服务器IP和端口
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->write("断开连接");
tcpSocket->disconnectFromHost();
ui->textEditRead->append("断开连接");
}