1 文章概括
在qt中借助designer设计通信界面,然后使用QTcpServer,QTcpSocket编写服务器和客户端的程序。
首先建立两个工程,一个是客户端client,另一个是服务器端server。
2 首先建立server项目
使用网络编程要在pro文件下添加network,然后项目qmake,不执行可能会找不到socket头文件
然后是设计界面如下
头文件代码
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QTcpServer *tcpserver;
QTcpSocket *tcpsocket;
private slots:
void on_connectbt_clicked();
void newConnection_Slot();
void readyRead_Slot();
void on_disconnectbt_clicked();
void on_sendbt_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
cpp文件代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tcpserver=new QTcpServer(this);
tcpsocket=new QTcpSocket(this);
connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newConnection_Slot()));
}
void Widget::newConnection_Slot()
{
tcpsocket=tcpserver->nextPendingConnection(); //获取已经连接的客户端套接字
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));
}
void Widget::readyRead_Slot()
{
QString buf;
buf=tcpsocket->readAll();
ui->receivewd->appendPlainText(buf);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connectbt_clicked()//打开服务器
{
tcpserver->listen(QHostAddress::Any,ui->portnum->text().toUInt()); //监听端口号
}
void Widget::on_disconnectbt_clicked()//关闭服务器
{
tcpserver->close();
}
void Widget::on_sendbt_clicked()//发送
{
tcpsocket->write(ui->sendwd->text().toLocal8Bit().data());
}
3.客户端创建
创建过程和服务器端类似,下面介绍代码。
ui界面实现如下
头文件代码
#ifndef CLIENT_H
#define CLIENT_H
#include <QMainWindow>
#include <QTcpSocket>//服务端只用socket
QT_BEGIN_NAMESPACE
namespace Ui { class client; }
QT_END_NAMESPACE
class client : public QMainWindow
{
Q_OBJECT
public:
client(QWidget *parent = nullptr);
~client();
QTcpSocket *tcpsocket;
private slots:
void on_openclient_clicked();
void connected_SLOT();
void readyRead_Slot();
void on_closeclient_clicked();
void on_sent_clicked();
private:
Ui::client *ui;
};
#endif // CLIENT_H
cpp实现函数代码
#include "client.h"
#include "ui_client.h"
#include "QString"
#include "stdio.h"
client::client(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::client)
{
ui->setupUi(this);
tcpsocket=new QTcpSocket(this);
}
client::~client()
{
delete ui;
}
void client::connected_SLOT()
{
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));//将信号连接到槽,书写比较明确
}
void client::readyRead_Slot()//定义接收信号的槽
{
QString buf;
buf=tcpsocket->readAll();
ui->receivewd->appendPlainText(buf);//接收由tcp发送过来的信息
}
void client::on_openclient_clicked()
{
tcpsocket->connectToHost(ui->ipnum->text(),ui->portnum->text().toUInt());//转为无符号,连接服务器端口
connect(tcpsocket,SIGNAL(connected()),this,SLOT(connected_SLOT()));
printf("打开客户端 ");
}
void client::on_closeclient_clicked()
{
tcpsocket->close();
printf("关闭客户端 ");
}
void client::on_sent_clicked()
{
tcpsocket->write(ui->sendwd->text().toLocal8Bit().data());
}
4.演示实验
服务器首先点击连接服务器
客户端点击打开客户端
输入内容点击发送
然后就可以实现两者之间的通信
暂不支持中文发送