Qt5 TcpSocket 클라이언트/서버 통신 실례

Qt Tcp 클라이언트/서버 통신 실례
클라이언트 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();
}

좋은 웹페이지 즐겨찾기