Qt5 TcpSocket 클라이언트/서버 통신 실례
5437 단어 C++Qt 프레임워크C++Qt 프레임워크
클라이언트 QTcpSocket
Client.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-03-23T21:15:18
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Client
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT +=network
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButto_conn_clicked();
private:
QTcpSocket * socket;
private slots:
void connected();
void on_pushButton_discon_clicked();
void readyread();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket=new QTcpSocket(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButto_conn_clicked()
{
this->socket->connectToHost("127.0.0.1",80100,QTcpSocket::ReadWrite);
connect(this->socket,SIGNAL(connected()),this,SLOT(connected()));
}
void MainWindow::connected()
{
QMessageBox::about(this," "," ");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyread()));
}
void MainWindow::on_pushButton_discon_clicked()
{
this->socket->close();
}
void MainWindow::readyread()
{
QMessageBox::about(this," "," ");
QByteArray arr=this->socket->readAll();
QDataStream * dst=new QDataStream(&arr,QIODevice::ReadOnly);/****** ******/
QString str1;
QString str2;
(*dst)>>str1>>str2;
this->ui->textBrowser->setText(str1+str2);
QMessageBox::about(this,"x",str1+str2);
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
서버 통신 실례
QTcpServer
Server.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-03-23T20:48:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Server
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT +=network
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTcpServer * server;
QTcpSocket * socket;
private slots:
void newConnection();
void ReceiveData();
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->socket=0;
this->server=new QTcpServer(this);
this->server->listen(QHostAddress::Any,80100);
QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newConnection()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newConnection(){
this->socket=this->server->nextPendingConnection();
QMessageBox::about(this," "," !");
connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
}
void MainWindow::ReceiveData(){
QByteArray arr=this->socket->readAll();
QDataStream dst(arr);
QString str1;
QString str2;
dst>>str1>>str2;
this->ui->browser->setText(str1+str2);
}
void MainWindow::on_pushButton_2_clicked()
{
QString str=this->ui->lineEdit->text();
QByteArray arr;
QDataStream dst(&arr,QIODevice::ReadWrite);/*QDataStream QIODevice */
dst<socket->write(arr);
}
void MainWindow::on_pushButton_clicked()
{
this->socket->close();
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
OpenCV && AKAZE && Windows10 && VisualStudio2017작동하는 버전의 조합을 찾고 ... OpenCV에서 AKAZE로 이미지 간의 특징점을 일치시키고 싶다면, OS(Windows10 64bit)와 OpenCV 버전과 Visual Studio 버전의 조합을 찾는데 힘들었...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.