qt开发TCP/IP网络通信下的服务器tcp server
.pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2021-03-05T22:26:18
#
#-------------------------------------------------
QT += core gui network //重点添加network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
SOURCES += \
main.cpp \
tcpserverwidget.cpp
HEADERS += \
tcpserverwidget.h
FORMS += \
tcpserverwidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
.h头文件
#ifndef TCPSERVERWIDGET_H
#define TCPSERVERWIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
#include <QNetworkInterface>//引入相应的头文件
namespace Ui {
class tcpserverWidget;
}
class tcpserverWidget : public QWidget
{
Q_OBJECT
public:
explicit tcpserverWidget(QWidget *parent = nullptr);
~tcpserverWidget();
QTcpServer *tcpserver;
QTcpSocket *tcpsocket;//实例化tcpserver,TCP socket
private slots:
void on_openbt_clicked();//按钮直接转到槽函数,自己定义的槽函数要在private slots声名
void on_closebt_clicked();
void newconnectslot();
void readyread();
void on_send_clicked();
private:
Ui::tcpserverWidget *ui;
};
#endif // TCPSERVERWIDGET_H
main.cpp
#include "tcpserverwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
tcpserverWidget w;
w.show();
return a.exec();
}
widget.cpp
#include "tcpserverwidget.h"
#include "ui_tcpserverwidget.h"
tcpserverWidget::tcpserverWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::tcpserverWidget)
{
ui->setupUi(this);
tcpserver = new QTcpServer(this);
ui->ipedit->setText(QNetworkInterface().allAddresses().at(1).toString());//读取自己本地ip地址
connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newconnectslot()));
tcpsocket = new QTcpSocket(this);
}
void tcpserverWidget::newconnectslot(){
tcpsocket = tcpserver->nextPendingConnection();
connect(tcpsocket,SIGNAL(QSerialPort::readyread()),this,SLOT(readyread()));
}
tcpserverWidget::~tcpserverWidget()
{
delete ui;
}
//编写自己定义的槽函数
void tcpserverWidget::readyread(){
qDebug()<<"收到信号";
QString buf;
buf = tcpsocket->readAll();
ui->rectextedit->appendPlainText(buf);
}
void tcpserverWidget::on_openbt_clicked()
{
tcpserver->listen(QHostAddress::Any,ui->portnumb->text().toUInt());//打开tcp服务器
}
void tcpserverWidget::on_closebt_clicked()
{
tcpserver->close();
}
void tcpserverWidget::on_send_clicked()
{
tcpsocket->write(ui->sendedit->text().toLocal8Bit().data());//将sendedit的text发送出去
}