I am developing an application in TCP in Qt communicating with each other. Server(receiver) gets connected with the client(sender) through “connectToHost” command and the client is writing 6 bytes of data to server but the server is not able to enter into the function reading the data from the client after getting ReadyRead() signal. Kindly suggest the improvement I should make in the code to read the data from the client.
//client.h
#ifndef PROJB_H
#define PROJB_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QtNetwork>
#include <QHostAddress>
class Client:public QObject
{
Q_OBJECT
public:
explicit Client(QObject *parent = 0);
// void connectreq();
signals:
public slots:
void connected();
void disconnected();
void readyRead();
void acceptConnection();
private:
QTcpServer* server_to_rec;
QTcpServer* server_to_send;
QTcpSocket *socket_to_send;
QTcpSocket* socket_to_rec;
};
#endif // PROJB_H
//client.cpp
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include "ProjB.h"
Client::Client(QObject *parent):QObject(parent)
{
socket_to_send = new QTcpSocket(this); //for sending
qDebug()<<"socket "<<socket_to_send<<" created";
socket_to_send->connectToHost("192.168.3.55",18006);
connect(socket_to_send, SIGNAL(connected()), this, SLOT(connected())) ;
connect(socket_to_send, SIGNAL(disconnected()), this, SLOT(disconnected())) ;
}
void Client::connected()
{
int bytes;
QByteArray buff("client");
qDebug()<<"Connection established";
while(1)
{
bytes =socket_to_send->write(buff);
qDebug() <<"data sent: "<<bytes<<"bytes "<<endl;
sleep(5);
}
}
void Client::disconnected()
{
qDebug()<<"trying to reestablish connection";
socket_to_send->connectToHost("192.168.3.55",18006);
}
void Client::readyRead()
{
while(socket_to_rec->canReadLine())
{
char buffer[1024]={0};
socket_to_rec->read(buffer, socket_to_rec->bytesAvailable());
qDebug()<<"buffer data:"<<buffer;
}
}
void Client::acceptConnection()
{
socket_to_rec = server_to_rec->nextPendingConnection();
connect(socket_to_rec, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
////output of client
sender output:
Starting /root/Desktop/26062013/ProjB-build-Desktop-Debug/ProjB…
socket QTcpSocket(0×8c6aa28) created
Connection established
data sent: 6 bytes
data sent: 6 bytes
data sent: 6 bytes
data sent: 6 bytes
//server.h
#ifndef PROJA_H
#define PROJA_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QtNetwork>
class Server: public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
QTcpServer* server_to_rec;
QTcpSocket* socket_to_rec;
//signals:
public slots:
void ReadData();
void incomingConnection();
private:
};
#endif // PROJA_H
//server.cpp
#include "ProjA.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
Server::Server(QObject *parent):QObject(parent)
{
server_to_rec = new QTcpServer(); //for receiving
qDebug()<<"server "<<server_to_rec<<" created";
if((server_to_rec->listen(QHostAddress::Any,18006))==TRUE)
qDebug()<<"listen successful";
else
qDebug()<<"listen failed";
connect(server_to_rec,SIGNAL(newConnection()),this,SLOT(incomingConnection()));
}
void Server::incomingConnection()
{
socket_to_rec =server_to_rec->nextPendingConnection();
while(!socket_to_rec)
{
qDebug()<<"Trying to Connect"<<endl;
socket_to_rec =server_to_rec->nextPendingConnection();
sleep(5);
}
qDebug()<<"socket "<<socket_to_rec<<" created";
connect(socket_to_rec, SIGNAL(readyRead()), this, SLOT(ReadData())) ;
}
void Server::ReadData()
{
qDebug()<<"start reading";
while(socket_to_rec->bytesAvailable()>0)
{
char buffer[1024]={0};
socket_to_rec->read(buffer, socket_to_rec->bytesAvailable());
qDebug()<<"buffer data:"<<QByteArray(buffer,70).toHex();
}
}
//output of server
Starting /root/Desktop/ProjA-build-Desktop-Debug/ProjA…
server QTcpServer(0×9a238e8) created
listen successful
socket QTcpSocket(0×9a2d180) created
↧