TCP communication software
Software function description
(1) TCP server realizes the functions of opening the server, monitoring connections, and sending and receiving data based on the "IP address" and "port number".
(2) TCP client realizes the function of connecting to the server according to "IP address" and "port number" and sending and receiving data.
Currently, only one-to-one communication is implemented, and there can only be one client, and multiple clients can be implemented by dynamically creating sockets.
Implementation steps
Create project
Development environment: Qt Creator 5.12.8 qt-opensource-windows-x86-5.12.8
Use the default settings for the steps not shown in the figure below.
Add client
Now there are two uis
ui design
Server interface:
Client interface:
control program
TCP.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
client.cpp \
main.cpp \
server.cpp
HEADERS += \
client.h \
server.h
FORMS += \
client.ui \
server.ui
CONFIG += c++11
# Default rules for deployment.
qnx: target.path = /tmp/$${
TARGET}/bin
else: unix:!android: target.path = /opt/$${
TARGET}/bin
!isEmpty(target.path): INSTALLS += target
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class Client;
}
class Client : public QWidget
{
Q_OBJECT
public:
explicit Client(QWidget *parent = nullptr);
~Client();
private:
Ui::Client *ui;
//QTcpServer* server;//监听的套接字
QTcpSocket* client;//通信的套接字
};
#endif // CLIENT_H
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui {
class Server; }
QT_END_NAMESPACE
class Server : public QWidget
{
Q_OBJECT
public:
Server(QWidget *parent = nullptr);
~Server();
private:
Ui::Server *ui;
QTcpServer* server;//监听的套接字
QTcpSocket* conn;//通信的套接字
};
#endif // SERVER_H
client.cpp
#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent) :
QWidget(parent),
ui(new Ui::Client)
{
ui->setupUi(this);
//ui init
ui->IP->setText("192.168.50.116");
ui->PORT->setText("12321");
ui->state->setText("状态:未连接");
connect(ui->Connect, &QPushButton::clicked, this, [=]()
{
// init
client = new QTcpSocket(this);
//连接服务器
client->connectToHost(QHostAddress(ui->IP->text()),ui->PORT->text().toInt());
ui->state->setText("状态:已连接");
ui->record->append("已连接到服务器---IP:" + ui->IP->text() + " 端口:" + ui->PORT->text());
//接收数据
connect(client, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = client->readAll();
ui->record->append("Server: " + array);
});
//发送
connect(ui->send, &QPushButton::clicked, this, [=]()
{
//发送数据
client->write(ui->msg->toPlainText().toUtf8());
ui->record->append("Client: " + ui->msg->toPlainText());
//clear
//ui->textEdit_2->clear();
});
});
}
Client::~Client()
{
delete ui;
}
server.cpp
#include "server.h"
#include "ui_server.h"
Server::Server(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Server)
{
ui->setupUi(this);
//ui init
ui->IP->setText("192.168.50.116");
ui->PORT->setText("12321");
ui->state->setText("状态:未开启");
connect(ui->OpenServer, &QPushButton::clicked, this, [=]()
{
//实例化
server = new QTcpServer(this);
//监听
server->listen(QHostAddress(ui->IP->text()),ui->PORT->text().toInt());
ui->state->setText("状态:已开启");
//新的连接
connect(server, &QTcpServer::newConnection, this, [=]()
{
//接收客户端的套接字对象accept
// sock_addr 结构体 == 类 QTcpSocket
conn = server->nextPendingConnection();
QString str;
ui->record->append("客户端接入---IP: " + conn->peerAddress().toString() +" 端口:"+ str.sprintf("%d", (conn->peerPort())));
//接收数据
connect(conn, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = conn->readAll();
ui->record->append("Client: " + array);
});
});
//发送
connect(ui->send, &QPushButton::clicked, this, [=]()
{
//发送数据
conn->write(ui->msg->toPlainText().toUtf8());
//char data[1024];data[0]=0xeb;data[1]=0x90;
//conn->write(data,1024);
ui->record->append("Server: " + ui->msg->toPlainText());
//clear
//ui->textEdit_2->clear();
});
});
}
Server::~Server()
{
delete ui;
}
main.cpp
#include "server.h"
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Server w;
w.setWindowTitle("TCP Server");
w.show();
Client c;
c.setWindowTitle("TCP Client");
c.show();
return a.exec();
}