QtのQTcpServer/QTcpSocket単純送受信情報(1)


QTで包装されたものでsocket類を作るのは、何がこんなに簡単なのかしか言えません.
waitForConnected()待機リンクの確立waitForReadyRead()新しいデータの到来を待つwaitForBytesWritten()待機データ書き込みsocket waitForDisconnected()待機リンク切断
開始前にプロジェクトでProには次のように追加されています.
       QT += core gui network
=========server==============
#include "testnet.h"
#include "ui_testnet.h"
#include 
Testnet::Testnet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Testnet)
{
    ui->setupUi(this);


    this->connect(ui->pushButton_start,SIGNAL(clicked()),this,SLOT(startTcpserver()));
    this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMessage()));
}

Testnet::~Testnet()
{
    delete ui;
}

void Testnet::startTcpserver()
{
    m_tcpServer = new QTcpServer(this);

    m_tcpServer->listen(QHostAddress::Any,19999); //      19999   ip

    connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect())); //       ,  newConnect()   ,         ,        。
}

void Testnet::newConnect()
{
        m_tcpSocket = m_tcpServer->nextPendingConnection(); //        socket
        connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); //      ,      

}

void Testnet::readMessage()	//    
{
//    ui->textEdit_rec->te
    QByteArray qba= m_tcpSocket->readAll(); //  
    qDebug()<textEdit_rec->setText(ss);

}

void Testnet::sendMessage() //    
{
    QString strMesg= ui->lineEdit_sendmessage->text();
    qDebug()<write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); //  
}


=======client========
#include "testnet_c.h"
#include "ui_testnet_c.h"



testnet_c::testnet_c(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::testnet_c)
{
    ui->setupUi(this);

    this->connect(ui->pushButton_con,SIGNAL(clicked()),this,SLOT(connectServer()));
    this->connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendMesg()));
}

testnet_c::~testnet_c()
{
    delete ui;
}


void testnet_c::connectServer()
{
    m_tcpSocket = new QTcpSocket(this);
    m_tcpSocket->abort();
    m_tcpSocket->connectToHost("192.168.1.178",19999);

    connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readMesg()));
}

void testnet_c::readMesg() //    
{
   QByteArray qba =   m_tcpSocket->readAll();

   ui->textEdit_recmesg->clear();

   qDebug()<textEdit_recmesg->setText(ss);
}

void testnet_c::sendMesg() //    
{
    QString ss= ui->lineEdit_sendmesg->text();
    m_tcpSocket->write(ss.toStdString().c_str(),strlen(ss.toStdString().c_str()));
    ui->lineEdit_sendmesg->clear();
}

 
実験結果は以下の通りである.