Help needed in server and client communication !!
-
Hello everyone, I am facing a wierd problem in server and client communication. When I send any data from server to client, from server side its showing data is sent but on client side no data appears to come and vice versa. Can anyone of you help me with the above mentioned problem please.
Code and outputs are shown below:
serverapp.cpp
@
#include "serverapp.h"
#include "ui_serverapp.h"#include <stdlib.h>
#include <QtNetwork>
#include <QMessageBox>
#include <QString>#include <QtWidgets>
ServerApp::ServerApp(QWidget *parent):QMainWindow(parent),Ui::ServerApp()
{
setupUi(this);
quint16 portnumber = 4444;
statusLabel->setWordWrap(true);
quitButton->setAutoDefault(false);
if (!server.listen(QHostAddress::Any,portnumber))
{
QMessageBox::critical(this, tr("Server v1.0"),tr("Unable to start the server: %1.").arg(server.errorString()));
close();
return;
}
QString ipAddress;
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
statusLabel->setText(tr("The server is running on IP: %1 port: %2\n").arg(ipAddress).arg(server.serverPort()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
setWindowTitle(tr("Server v1.0"));
}ServerApp::~ServerApp()
{
}void ServerApp::updateRB(QString data)
{
receivingBrowser->append(data.toUtf8());
}FortuneServer::FortuneServer(QObject *parent):QTcpServer(parent)
{}
void FortuneServer::incomingConnection(qintptr socketDescriptor)
{
FortuneThread *thread = new FortuneThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}FortuneThread::FortuneThread(int socketDescriptor, QObject *parent): QThread(parent), socketDescriptor(socketDescriptor)
{}
void FortuneThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor))
{
emit error(tcpSocket.error());
return;
}
else
{
qDebug() << "Connected to client";
qDebug() << tcpSocket.readAll();//tmp.data();
tcpSocket.write("Hello client");
tcpSocket.disconnectFromHost();
}
}
@serverapp.h
@
#ifndef SERVERAPP_H
#define SERVERAPP_H
#include "ui_serverapp.h"
#include <QMainWindow>
#include <QTcpServer>
#include <QThread>
#include <QTcpSocket>#include <QMessageBox>
namespace Ui {
class ServerApp;
}class FortuneServer : public QTcpServer
{
Q_OBJECTpublic:
FortuneServer(QObject *parent = 0);protected:
void incomingConnection(qintptr socketDescriptor);private:
};class FortuneThread : public QThread
{
Q_OBJECTpublic:
FortuneThread(int socketDescriptor, QObject *parent); void run();
signals:
void error(QTcpSocket::SocketError socketError);
void displayRB(QString);private:
int socketDescriptor;
};class ServerApp : public QMainWindow, public Ui::ServerApp
{
Q_OBJECTpublic:
ServerApp(QWidget *parent = 0);
~ServerApp();private slots:
void updateRB(QString);private:
FortuneServer server;
};#endif // SERVERAPP_H
@rescueop.cpp
@
#include "rescueop.h"
#include "ui_rescueop.h"
#include <QTcpSocket>QTcpSocket socket;
rescueOp::rescueOp(QWidget *parent) : QMainWindow(parent),Ui::rescueOp()
{
qint16 i;
setupUi(this);socket.connectToHost("127.0.0.1",4444); if(socket.waitForConnected(500)) { status->setText("Connected to server"); qDebug() << "Connected to server"; for(i=0;i<5;i++) { qDebug() << socket.write("Hello server"); serverBrowser->append("Hello server "); } socket.waitForBytesWritten(); socket.readyRead(); qDebug() << socket.readAll(); socket.close(); } else status->setText("Socket not connected");
}
rescueOp::~rescueOp()
{
if(socket.isOpen())
socket.close();
}
@rescueop.h
@
#ifndef RESCUEOP_H
#define RESCUEOP_H#include <QMainWindow>
#include <QTcpSocket>
#include "ui_rescueop.h"namespace Ui {
class rescueOp;
}class rescueOp : public QMainWindow, public Ui::rescueOp
{
Q_OBJECTpublic:
rescueOp(QWidget *parent = 0);
~rescueOp();signals:
private:
};#endif // RESCUEOP_H
@output of server
@
Starting /home/chocky/Desktop/mtech_project/build-ServerApp-Desktop_Qt_5_3_GCC_64bit-Debug/ServerApp...
Connected to client
""
/home/chocky/Desktop/mtech_project/build-ServerApp-Desktop_Qt_5_3_GCC_64bit-Debug/ServerApp exited with code 0
@output of client
@
Starting /home/chocky/Desktop/mtech_project/build-rescue_original-Desktop_Qt_5_3_GCC_64bit-Debug/rescueOp...
Connected to server
12
12
12
12
12
""
/home/chocky/Desktop/mtech_project/build-rescue_original-Desktop_Qt_5_3_GCC_64bit-Debug/rescueOp exited with code 0
@ -
Did you modify Fortune example?
If yes, did you test original Fortune example?
Does the Fortune example work? -
Original fortune example works fine. But I couldn't make this code work. Everything goes well except the data transmission. Can you help me with this code.?
-
You can compare original Fortune example and your code and then modify original Fortune example to become your code gradually.
-
Even I tried that previously in every possible way for transmitting data. Can you provide any working code for the communication between server and client if possible.
-
original Fortune example shows how to perform communication between server and client.
-
I have got it solved by using readyRead signal, but now i got a new problem. Previously i used readyRead signal for serial port now that conflicts with the socket too. Do you know how can i use two readyRead signals for different purposes like the code shown below.?
@
connect(&serialPort,SIGNAL(QSerialPort::readyRead()),this,SLOT(serialReceived()));
connect(&socket, SIGNAL(QTcpSocket::readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);void rescueOp::serialReceived()
{
// get the information
QByteArray data = serialPort.readAll();// will write on console qDebug() << " Data in Serial Port: " << data;
}
void rescueOp::readyRead()
{
// get the information
QByteArray Data = socket.readAll();// will write on console qDebug() << " Data in Socket: " << Data;
}
@It shows below error now,
@
Connected to server
QObject::connect: No such signal QTcpSocket::QTcpSocket::readyRead() in ../rescue_original/rescueop.cpp:61
QObject::connect: (receiver name: 'rescueOp')
Serial Port Opened
@ -
try Qt5 syntax connection,
@
Counter a, b;
QObject::connect(&a, &Counter::valueChanged,
&b, &Counter::setValue);
@ -
Hi,
You are mixing the old and new connection syntax
@connect(&socket, SIGNAL(readyRead()), this, SLOT(readyRead()));@
is the correct standard syntax