Sending Data from Client to Server
-
I got an output in server side is like:
-------NEW CONNECTION SLOT--------
Press <RETURN> to close this window...Inclient side is the oput like:
QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
INSIDe sendMessageToServerI have changed the server.cpp is like below. Like you said, I have used clientConnection object for readyRead slot. The connection has accomplished but I couldnt get the data.
#include "server.h" Server::Server(QObject *parent) : QObject(parent) { tcpServer = new QTcpServer(); connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection); initServer(); } void Server::incomingConnection(int socketfd){ clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater); // client->setSocketDescriptor(socketfd); // clients.insert(client); qDebug() << "New client from: " << clientConnection->peerAddress().toString(); connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected())); } void Server::newConnection(){ qDebug() << "-------NEW CONNECTION SLOT--------"; clientConnection->write("ABCDE"); //clientConnection->disconnectFromHost(); } void Server::readyRead(){ qDebug() << "|||||||||||READYREAD SLOT--------"; qDebug() << clientConnection->readAll(); } void Server::disconnected(){ qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------"; } void Server::initServer(){ if (!tcpServer->listen(QHostAddress::Any,45442)) { qDebug() << "AAAA"; qDebug() << tcpServer->errorString(); } }
@star673 said in Sending Data from Client to Server:
clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection();
Why do you have that first line here? All it does is leak a new
QTcpSocket
.The connection has accomplished but I couldnt get the data.
Is your
Server::readyRead()
called at all?Inclient side is the oput like:
QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
INSIDe sendMessageToServerThen you have an error in
client.cpp
, which has nothing to do with the server code you are showing....connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Press <RETURN> to close this window...
This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.
-
@star673 said in Sending Data from Client to Server:
clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection();
Why do you have that first line here? All it does is leak a new
QTcpSocket
.The connection has accomplished but I couldnt get the data.
Is your
Server::readyRead()
called at all?Inclient side is the oput like:
QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
INSIDe sendMessageToServerThen you have an error in
client.cpp
, which has nothing to do with the server code you are showing....connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Press <RETURN> to close this window...
This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.
@JonB
I have commented this line.clientConnection=new QTcpSocket(this);
When I commented the connect functions server application did not crashed. It seems the problem is related with these lines. Did I use them worng?
connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
-
@star673 said in Sending Data from Client to Server:
clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection();
Why do you have that first line here? All it does is leak a new
QTcpSocket
.The connection has accomplished but I couldnt get the data.
Is your
Server::readyRead()
called at all?Inclient side is the oput like:
QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
INSIDe sendMessageToServerThen you have an error in
client.cpp
, which has nothing to do with the server code you are showing....connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Press <RETURN> to close this window...
This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.
@JonB said in Sending Data from Client to Server:
Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Come back when you have done that....
[I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]
-
@JonB said in Sending Data from Client to Server:
Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Come back when you have done that....
[I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]
-
@JonB said in Sending Data from Client to Server:
Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Come back when you have done that....
[I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]
-
How to change this line?
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
connect(tcpSocket, &QTcpSocket::error, this, &ThisClass::displayError);
UPDATE
As per @Christian-Ehrlicher's link below, theQTcpSocket::error
signal is overloaded in this class and you will actually need theQOverload<QAbstractSocket::SocketError>::of()
shown there. You will also note that this method is now obsolete and you are advised to "Use errorOccurred() instead", which will look simpler. -
How to change this line?
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
@star673 said in Sending Data from Client to Server:
How to change this line?
By looking into the documentation... reading seems to be very hard.
-
@star673 said in Sending Data from Client to Server:
clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection();
Why do you have that first line here? All it does is leak a new
QTcpSocket
.The connection has accomplished but I couldnt get the data.
Is your
Server::readyRead()
called at all?Inclient side is the oput like:
QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
INSIDe sendMessageToServerThen you have an error in
client.cpp
, which has nothing to do with the server code you are showing....connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your
SIGNAL
/SLOT()
macro connections, everywhere, and see where you are.Press <RETURN> to close this window...
This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.
I hav changed the error slot like below.
connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } });
Now I dont have any runtime error like before. The connected signal is emitted by the client but than nothing happens. I could not read from client, too. There is no printed socket error. My incoming connection function like below.
void Server::incomingConnection(int socketfd){ // clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater); // client->setSocketDescriptor(socketfd); // clients.insert(client); qDebug() << "New client from: " << clientConnection->peerAddress().toString();
There is no print which starting "New client from" so it seems something wron with server side.
-
Please show your actual code with the new signal/slot syntax.
-
Please show your actual code with the new signal/slot syntax.
My server.cpp file is,
#include "server.h" Server::Server(QObject *parent) : QObject(parent) { tcpServer = new QTcpServer(); connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection); initServer(); } void Server::incomingConnection(int socketfd){ // clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater); // client->setSocketDescriptor(socketfd); // clients.insert(client); qDebug() << "New client from: " << clientConnection->peerAddress().toString(); connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected())); } void Server::newConnection(){ qDebug() << "-------NEW CONNECTION SLOT--------"; // QByteArray block; // QDataStream out(&block, QIODevice::ReadWrite); // out.setVersion(QDataStream::Qt_5_9); // out << "ABCderfHt"; clientConnection->write("ABCDE"); //clientConnection->disconnectFromHost(); } void Server::readyRead(){ qDebug() << "|||||||||||READYREAD SLOT--------"; qDebug() << clientConnection->readAll(); } void Server::disconnected(){ qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------"; } void Server::initServer(){ if (!tcpServer->listen(QHostAddress::Any,45442)) { qDebug() << tcpServer->errorString(); } else{ qDebug() << "Listening"; } }
My server.h file is,
#ifndef SERVER_H #define SERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> #include <QDataStream> #include <QByteArray> #include <QNetworkInterface> #include <QAbstractSocket> class Server : public QObject { Q_OBJECT public: explicit Server(QObject *parent = nullptr); QTcpServer *tcpServer = nullptr; void initServer(); QTcpSocket *clientConnection; // QDataStream out; protected: void incomingConnection(int socketfd); public slots: void newConnection(); void readyRead(); void disconnected(); private: QSet<QTcpSocket*> clients; }; #endif // SERVER_H
The main.cpp on the server side,
#include <QCoreApplication> #include "server.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Server server; return a.exec(); }
My client.cpp is,
#include "client.h" Client::Client() { tcpSocket = new QTcpSocket(); tcpSocket->connectToHost(QHostAddress::LocalHost,45442); //connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); // connect(tcpSocket,QOverload<QAbstractSocket::SocketError>::of(error),this,&Client::displayError); connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } }); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer); connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead); } void Client::displayError(QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } } void Client::sendMessageToServer(){ qDebug() << "INSIDe sendMessageToServer"; //tcpSocket->waitForReadyRead(); qDebug() << tcpSocket->canReadLine() << tcpSocket->readAll(); tcpSocket->write("MessageFromClient"); //tcpSocket->flush(); } void Client::readyRead(){ qDebug() << "///READYREAD SLOT///"; // in.startTransaction(); // QString nextFortune; // in >> nextFortune; // if (!in.commitTransaction()) // return; qDebug() << tcpSocket->readAll(); }
My client.h is,
#ifndef CLIENT_H #define CLIENT_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> #include <QDataStream> #include <QByteArray> #include <QNetworkInterface> #include <QAbstractSocket> class Client : public QTcpSocket { public: Client(); QTcpSocket *tcpSocket = nullptr; QDataStream in; public slots: void displayError(QAbstractSocket::SocketError socketError); void sendMessageToServer(); void readyRead(); }; #endif // CLIENT_H
The main.cpp on the client side is,
#include <QCoreApplication> #include "client.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Client client; return a.exec(); }
-
My server.cpp file is,
#include "server.h" Server::Server(QObject *parent) : QObject(parent) { tcpServer = new QTcpServer(); connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection); initServer(); } void Server::incomingConnection(int socketfd){ // clientConnection=new QTcpSocket(this); clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater); // client->setSocketDescriptor(socketfd); // clients.insert(client); qDebug() << "New client from: " << clientConnection->peerAddress().toString(); connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected())); } void Server::newConnection(){ qDebug() << "-------NEW CONNECTION SLOT--------"; // QByteArray block; // QDataStream out(&block, QIODevice::ReadWrite); // out.setVersion(QDataStream::Qt_5_9); // out << "ABCderfHt"; clientConnection->write("ABCDE"); //clientConnection->disconnectFromHost(); } void Server::readyRead(){ qDebug() << "|||||||||||READYREAD SLOT--------"; qDebug() << clientConnection->readAll(); } void Server::disconnected(){ qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------"; } void Server::initServer(){ if (!tcpServer->listen(QHostAddress::Any,45442)) { qDebug() << tcpServer->errorString(); } else{ qDebug() << "Listening"; } }
My server.h file is,
#ifndef SERVER_H #define SERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> #include <QDataStream> #include <QByteArray> #include <QNetworkInterface> #include <QAbstractSocket> class Server : public QObject { Q_OBJECT public: explicit Server(QObject *parent = nullptr); QTcpServer *tcpServer = nullptr; void initServer(); QTcpSocket *clientConnection; // QDataStream out; protected: void incomingConnection(int socketfd); public slots: void newConnection(); void readyRead(); void disconnected(); private: QSet<QTcpSocket*> clients; }; #endif // SERVER_H
The main.cpp on the server side,
#include <QCoreApplication> #include "server.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Server server; return a.exec(); }
My client.cpp is,
#include "client.h" Client::Client() { tcpSocket = new QTcpSocket(); tcpSocket->connectToHost(QHostAddress::LocalHost,45442); //connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); // connect(tcpSocket,QOverload<QAbstractSocket::SocketError>::of(error),this,&Client::displayError); connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } }); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer); connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead); } void Client::displayError(QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } } void Client::sendMessageToServer(){ qDebug() << "INSIDe sendMessageToServer"; //tcpSocket->waitForReadyRead(); qDebug() << tcpSocket->canReadLine() << tcpSocket->readAll(); tcpSocket->write("MessageFromClient"); //tcpSocket->flush(); } void Client::readyRead(){ qDebug() << "///READYREAD SLOT///"; // in.startTransaction(); // QString nextFortune; // in >> nextFortune; // if (!in.commitTransaction()) // return; qDebug() << tcpSocket->readAll(); }
My client.h is,
#ifndef CLIENT_H #define CLIENT_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> #include <QDataStream> #include <QByteArray> #include <QNetworkInterface> #include <QAbstractSocket> class Client : public QTcpSocket { public: Client(); QTcpSocket *tcpSocket = nullptr; QDataStream in; public slots: void displayError(QAbstractSocket::SocketError socketError); void sendMessageToServer(); void readyRead(); }; #endif // CLIENT_H
The main.cpp on the client side is,
#include <QCoreApplication> #include "client.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Client client; return a.exec(); }
@star673 said in Sending Data from Client to Server:
void Server::incomingConnection(int socketfd){
If you check the documentation you will see that incomingConnection is a protected virtual method in QTcpServer. If you want to use it you have to subclass QTcpServer and override it. But if you use QTcpServer::newConnection signal there is no need for incomingConnection - simply move the code from your incomingConnection to newConnection.
-
@star673 said in Sending Data from Client to Server:
void Server::incomingConnection(int socketfd){
If you check the documentation you will see that incomingConnection is a protected virtual method in QTcpServer. If you want to use it you have to subclass QTcpServer and override it. But if you use QTcpServer::newConnection signal there is no need for incomingConnection - simply move the code from your incomingConnection to newConnection.
Thank you for helping, it worked like below.
#include "server.h" Server::Server(QObject *parent) : QObject(parent) { tcpServer = new QTcpServer(); connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection); initServer(); timer = new QTimer(this); } void Server::newConnection(){ qDebug() << "-------NEW CONNECTION SLOT--------"; clientConnection = tcpServer->nextPendingConnection(); connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater); qDebug() << "New client from: " << clientConnection->peerAddress().toString(); connect(clientConnection,&QAbstractSocket::readyRead,this,&Server::readyRead); connect(clientConnection,&QAbstractSocket::disconnected,this,&Server::disconnected); clientConnection->write("ABCDEFFGHJKL"); connect(timer,&QTimer::timeout,this,&Server::TimerSlot); timer->start(1000); } void Server::readyRead(){ qDebug() << "|||||||||||READYREAD SLOT--------"; qDebug() << "||||||||" << clientConnection->readAll(); } void Server::disconnected(){ qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------"; } void Server::initServer(){ if (!tcpServer->listen(QHostAddress::Any,45442)) { qDebug() << tcpServer->errorString(); } else{ qDebug() << "Listening"; } } void Server::TimerSlot(){ clientConnection->write("ABCDEFFGHJKL"); }
The client side is,
#include "client.h" Client::Client() { tcpSocket = new QTcpSocket(); tcpSocket->connectToHost(QHostAddress::LocalHost,45442); connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } }); connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer); connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead); timer = new QTimer(this); } void Client::displayError(QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: break; case QAbstractSocket::HostNotFoundError: qDebug() << "HostNotFoundError. Connection was refused. Server is not working"; break; case QAbstractSocket::ConnectionRefusedError: qDebug() << "ConnectionRefusedError. Client. The host was not found"; break; default: qDebug() << "ErrorString : " << tcpSocket->errorString(); } } void Client::sendMessageToServer(){ qDebug() << "INSIDe sendMessageToServer"; qDebug() << tcpSocket->canReadLine() << tcpSocket->readAll(); tcpSocket->write("MessageFromClient"); connect(timer,&QTimer::timeout,this,&Client::TimerSlotClient); timer->start(1000); } void Client::readyRead(){ qDebug() << "///READYREAD SLOT///"; qDebug() << tcpSocket->readAll(); } void Client::TimerSlotClient(){ tcpSocket->write("MessageFromClient"); }