赞
踩
故事的开始:(界面布局如下)
tcp通信.pro文件里加上 QT += network
服务器端:(监听套接字和通信套接字)
客户端:(只有通信套接字)
服务器端代码:
serverwidget.h
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include <QWidget>
#include<QTcpServer>//监听套接字
#include<QTcpSocket>//通信套接字
QT_BEGIN_NAMESPACE
namespace Ui { class ServerWidget; }
QT_END_NAMESPACE
class ServerWidget : public QWidget
{
Q_OBJECT
public:
ServerWidget(QWidget *parent = nullptr);
~ServerWidget();
private slots:
void on_buttonSend_clicked();//发送按钮
void on_buttonClose_clicked();//关闭按钮
private:
Ui::ServerWidget *ui;
QTcpServer *tcpServer;//监听套接字
QTcpSocket *tcpSocket;//通信套接字
};
#endif // SERVERWIDGET_H
serverwidget.cpp
#include "serverwidget.h"
#include "ui_serverwidget.h"
ServerWidget::ServerWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ServerWidget)
{
ui->setupUi(this);
//监听,指定父对象,让其自动回收空间
tcpServer=new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8888);
/*QTcpServer::newConnection()
This signal is emitted every time a new connection is available.
每次有新连接可用时都会发出此信号。
*/
connect(tcpServer,&QTcpServer::newConnection,
[=]()
{
//取出建立好链接的套接字
tcpSocket=tcpServer->nextPendingConnection();
//获取对方的IP和端口
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
QString temp = QString("[%1:%2]:成功连接!").arg(ip).arg(port);
ui->textEditRead->setText(temp);
//接收,对方通信套接字触发readyRead()
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//从通信套接字中取出内容
QByteArray array = tcpSocket->readAll();
ui->textEditRead->append(array);
}
);
}
);
}
ServerWidget::~ServerWidget()
{
delete ui;
}
void ServerWidget::on_buttonSend_clicked()
{
QString str =ui->textEditWrite->toPlainText();
//给对方发送数据,用tcpSocket
tcpSocket->write( str.toUtf8().data() );
}
void ServerWidget::on_buttonClose_clicked()
{
//主动和客户端连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
客户端代码:
clientwidget.h
#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 = nullptr);
~ClientWidget();
private slots:
void on_buttonConnect_clicked();
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::ClientWidget *ui;
QTcpSocket *tcpSocket;//只需通信套接字
};
#endif // CLIENTWIDGET_H
clientwidget.cpp
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include<QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
tcpSocket = nullptr;
//分配空间指定父对象
tcpSocket = new QTcpSocket(this);
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();//获取服务器ip
qint16 port = ui->lineEditPort->text().toInt();//获取服务器端口
tcpSocket->abort();
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();
}
源码及程序我打包好了,需要的评论,我给云盘连接。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。