unable to establish two way communication using qt

2019-08-28 11:21发布

I have used QTcpSocket and QTcpServer class of qt to establish two way communication. I am able to send data from client to server. But am not getting the response back from server i.e my client.cpp never fires readyRead() signal. I have checked using Wireshark that my data from the server is available in specifed port.

I am posting my client.cpp code( Please help) :

Client::Client(QObject* parent): QObject(parent)
{
    socket = new QTcpSocket(this);
    connect(socket, SIGNAL(connected()),
            this, SLOT(startTransfer()));
    connect(socket, SIGNAL(readyRead()),this, SLOT(startRead()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(socketError(QAbstractSocket::SocketError)) );


}

Client::~Client()
{
    socket->close();
}

void Client::start(QString address, quint16 port)
{
    addr.setAddress(address);
    socket->connectToHost(addr,port,QTcpSocket::ReadWrite);
}

void Client::startTransfer()
{
    printf("Connection established.\n");
    char buffer[1024];
    forever
    {
    printf(">> ");
    gets(buffer);
    int len = strlen(buffer);
    buffer[len] = '\n';
    buffer[len+1] = '\0';
    socket->write(buffer);
    socket->flush();

}
}

void Client::startRead()
{
    cout<<"inside startRead()<<endl";
    while(socket->canReadLine())
       {
       QByteArray ba = socket->readLine();
       if(strcmp(ba.constData(), "!exit\n") == 0)
       {
       socket->disconnectFromHost();
       break;
       }
       printf(">> %s", ba.constData());
}
}

void Client::socketError(QAbstractSocket::SocketError )
    {
    qDebug()<<"error" ;
    }

标签: qt sockets
2条回答
手持菜刀,她持情操
2楼-- · 2019-08-28 11:48

Looks like you have forever loop here. This means that your Qt main eventloop never gets the control back after you call startTransfer(). How do you suppose the Qt should run the startRead() code if you block your execution thread with infinite loop?

查看更多
Anthone
3楼-- · 2019-08-28 11:58

For Amartel adding the server code:

Server::Server(QObject* parent): QObject(parent)
{
 // cout << "Before connect" << endl;
  connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));

  cout << "Listening.." << endl;
  server.listen(QHostAddress::Any, 9999);
//  cout << "Server started.." << endl;
}

Server::~Server()
{
  server.close();
}

void Server::acceptConnection()
{
 // cout << "In acceptConnection" << endl;
  client = server.nextPendingConnection();

  connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));
}

void Server::startRead()
{
    while(client->canReadLine())
       {
       QByteArray ba = client->readLine();
       if(strcmp(ba.constData(), "!exit\n") == 0)
       {
       client->disconnectFromHost();
       break;
       }
       printf(">> %s", ba.constData());

       int result = 0;
       bool ack = true;

       result = client->write("I Reached");
       cout<<result<<endl;
       if(result <= 0)
       qDebug("Ack NOT sent to client!!!");
       else
       qDebug("Ack sent to client.");
      // client->write("I Reached");
       client->flush();
}
}
查看更多
登录 后发表回答