Qt C++服务端
通过QT编写的界面软件,向unity发送控制命令,实现qt对unity场景的控制。主要是用C++做服务器,unity做客户端实现TCP网络通信。这篇博客是QT部分,unity部分见另一篇博客。
https://blog.csdn.net/weixin_42521239/article/details/96173965
建立一个Qt的GUI项目,在界面上放一个label显示连接状态,两个button作为指令发送控制。
Unity_Qt.pro
关键就是要加一句
QT += network
#-------------------------------------------------
#
# Project created by QtCreator 2019-07-16T15:44:37
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Unity_Qt
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
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QTcpServer;//前向声明
class QTcpSocket;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
private:
QString statusText; //状态信息
QTcpServer *tcpServer; //服务器
QTcpSocket *clientTcpSocket; //客户端socket
void SocketSend(QString sendStr);
private slots:
void SocketConnet();
void SocketReceive();
void on_leftBtn_clicked();
void on_rightBtn_clicked();
};
#endif // WIDGET_H
widget.cpp
#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QDebug>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化server并监听
tcpServer=new QTcpServer(this); //qt自己内存管理
if(!tcpServer->listen(QHostAddress::Any,6666)) //监听所有网络地址,端口6666
qDebug()<<tcpServer->errorString();
statusText=statusText+"wait for connecting..."+"\n";
ui->statusLabel->setText(statusText);
//绑定信号槽,当有连接时作出反应
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(SocketConnet()));
}
void Widget::SocketConnet()
{
//获得client socket
clientTcpSocket=tcpServer->nextPendingConnection();
//绑定信号槽,接收数据,并且当连接关闭是删除连接
connect(clientTcpSocket,SIGNAL(readyRead()),this,SLOT(SocketReceive()));
connect(clientTcpSocket,SIGNAL(disconnected()),clientTcpSocket,SLOT(deleteLater()));
//显示客户端连接信息
QString clientIp=clientTcpSocket->peerAddress().toString();
QString clientPort=QString::number(clientTcpSocket->peerPort());
statusText=statusText+"conneted with "+clientIp+":"+clientPort+"\n";
ui->statusLabel->setText(statusText);
}
void Widget::SocketSend(QString sendStr)
{
clientTcpSocket->write(sendStr.toStdString().c_str());
}
void Widget::SocketReceive()
{
//接收数据并显示,字节转换成了字符串
QString recvStr=clientTcpSocket->readAll();
statusText=statusText+recvStr+"\n";
ui->statusLabel->setText(statusText);
//经处理后发送回去
SocketSend("From server: "+recvStr);
}
Widget::~Widget()
{
delete ui;
}
//发送unity物体左旋消息
void Widget::on_leftBtn_clicked()
{
SocketSend("leftrotate");
}
//发送unity物体右旋消息
void Widget::on_rightBtn_clicked()
{
SocketSend("rightrotate");
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
运行结果如下
这篇博客的代码已经上传到CSDN上了,程序可以直接运行,有需要的可以下载,链接如下
https://download.csdn.net/download/weixin_42521239/11368413
如果没有积分的小伙伴也可以在评论中留下你的邮箱,我稍后发给你。