2016-11-03 2 views
1

Je veux envoyer des données du client au serveur, lorsque je tente de se connecter à serevr, show client erreur IDENTIFIE, et aucune donnée envoyéQTcpSocket erreur inconnue

son spectacle que chaîne vide « »,

tout l'aide sera appréciée.

Voici le code:

// client

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

    tcpSocket = new QTcpSocket(this); 

    connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected())); 

    QHostAddress ha; 
    ha.setAddress("myIP"); 

    tcpSocket->connectToHost(ha, 6401); 

    if(!tcpSocket->waitForConnected(3000)) { 
     ui->lineEdit->setText(tcpSocket->errorString()); 
    } 
    else 
     ui->lineEdit->setText("connected"); 
} 

void Widget::connected() 
{ 
    tcpSocket->write("hello this is client\r\n"); 
    tcpSocket->flush(); 
    tcpSocket->waitForBytesWritten(3000); 

    tcpSocket->close(); 
} 

// serveur

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

    tcpServer = new QTcpServer(this); 

    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); 

    if(!tcpServer->listen(QHostAddress::Any, 6401)) { 
      ui->lineEdit->setText("server not started"); 
     } 
     else 
      ui->lineEdit->setText("server started"); 
} 

void Widget::newConnection() 
{ 
    QTcpSocket *tcpSocket= tcpServer->nextPendingConnection(); 

    qDebug() << tcpSocket->readAll(); 
    tcpSocket->waitForReadyRead(3000); 

    tcpSocket->close(); 
} 
+0

Je suppose ' "myIP"' est juste un porte-lieu. Que se passe-t-il si vous spécifiez le bouclage local '" 127.0.0.1 "' à la place? –

+0

même erreur se produire, erreur inconnue et chaîne vide. –

+0

problème était en ordre lors de la lecture des données –

Répondre

0

voici le problème:

// mauvais ordre

qDebug() << tcpSocket->readAll(); 
tcpSocket->waitForReadyRead(3000); 
//

ordre:

tcpSocket->waitForReadyRead(3000); 
qDebug() << tcpSocket->readAll();