Server connected with client in tcp communication in Qt but not receiving data from client.
-
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.
#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();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"; sleep(5); }
}
void Client::disconnected()
{
qDebug()<<"trying to reestablish connection";
socket_to_send->connectToHost("192.168.3.55",18006);
}//output of client
sender output:Starting /root/Desktop/26062013/ProjB-build-Desktop-Debug/ProjB...
socket QTcpSocket(0x8c6aa28) 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)//canReadLine()){ 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(0x9a238e8) created
listen successful
socket QTcpSocket(0x9a2d180) created -
Hi and welcome to devnet,
Please enclose your code between coding tags. That will make your code readable.
From what I saw, your are not connecting anything. For example:
@connect(server_to_rec,SIGNAL),this,SLOT));@
This line shouldn't even compile
Have a look at the fortune example, it shows how to do what you want to achieve.
-
[quote author="SGaist" date="1372318994"]Hi and welcome to devnet,
Please enclose your code between coding tags. That will make your code readable.
From what I saw, your are not connecting anything. For example:
@connect(server_to_rec,SIGNAL),this,SLOT));@
This line shouldn't even compile
Have a look at the fortune example, it shows how to do what you want to achieve.[/quote]
If you hover your mouse over those connect parameters you can see the actual signals and slots. Its just a formating thing because he didn't enclose his code with '@' tags. -
[quote author="KA51O" date="1372322638"]
If you hover your mouse over those connect parameters you can see the actual signals and slots. Its just a formating thing because he didn't enclose his code with '@' tags.
[/quote]My bad, thanks for the tip :)