Client To server message sending
-
I have two application Client and HelloWorldServer
In client part
When I run these two application I want to see client message on Server status message .What is the problem and how can I solve it?
@ELIF
Please post code as code (and in Code tags), not as screenshots, so that people can copy & paste etc.- You do not say whether the client outputs
Connected!
. What happens in your code ifwaitForConnected(1000)
fails? You still proceed... - You start a transaction on the socket, no sign of ending the transaction.
- Does your code allow you to press
pushButton_2
more than once? Is that what you are doing? Is the error message because it is re-usingm_clientSocket
and trying to connect again when it is already connected?
At your server side you have
qDebug() << client->readAll()
. That will read and discard anything available after the first line is read. Is that what you intend?Otherwise put in
qDebug()
statements and actually show what they report if things are inexplicably not working. - You do not say whether the client outputs
-
You probably want to take a look at this tutorial: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application
-
I get the value of IP adress in below code on textEditMesaage (ui)
m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); //ITS OK
BUT in below codereadyRead() function is not working. I want to solve it I really struggle, so sad:(
Here is helloworldserver.cpp#include "helloworldserver.h" #include "server.h" #include "server.cpp" #include "ui_server.h" #include<list> #include<string.h> #include <QList> #include "server.h"//MAIN WINDOW #include "ui_server.h" HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent){ m_pHelloWindow = pHelloServer; } void HelloworldServer::readyRead(){ QTcpSocket * client = (QTcpSocket *)sender(); client->open(QIODevice::ReadWrite); QByteArray data=client->readAll(); client->waitForReadyRead(100); m_pHelloWindow->addMessage(" : " + client->write(data)); } void HelloworldServer::incomingConnection(qintptr handle){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(handle); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //it does not work connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
void HelloworldServer::readyRead(){
QTcpSocket * client = (QTcpSocket *)sender(); client->open(QIODevice::ReadWrite); QByteArray data=client->readAll(); client->waitForReadyRead(100); m_pHelloWindow->addMessage(" : " + client->write(data));
} void HelloworldServer::incomingConnection(qintptr handle){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(handle); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //bu satırın ha varlığı ha yokluğu connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
Here is the server.cpp
#include "server.h"//MAIN WINDOW #include "ui_server.h" Server::Server(QWidget *parent) : QMainWindow(parent), ui(new Ui::Server) { ui->setupUi(this); } Server::~Server() { delete ui; } void Server::addMessage(QString Msg){ ui->textEditMessage->setText(Msg); } void Server::on_pushButtonStart_clicked() { m_pBoxServer= new HelloworldServer(this); bool success = m_pBoxServer->listen(QHostAddress::Any,quint16(ui->textEditPort->toPlainText().toInt())); //bool success = m_pBoxServer->listen(QHostAddress::Any,45444); if(!success){ addMessage("Server Failed"); qDebug()<<"server failed"; } else{ addMessage("Server Started"); } }
What is the readyRead problem ? How can I solve readyRead() problem.
-
I get the value of IP adress in below code on textEditMesaage (ui)
m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); //ITS OK
BUT in below codereadyRead() function is not working. I want to solve it I really struggle, so sad:(
Here is helloworldserver.cpp#include "helloworldserver.h" #include "server.h" #include "server.cpp" #include "ui_server.h" #include<list> #include<string.h> #include <QList> #include "server.h"//MAIN WINDOW #include "ui_server.h" HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent){ m_pHelloWindow = pHelloServer; } void HelloworldServer::readyRead(){ QTcpSocket * client = (QTcpSocket *)sender(); client->open(QIODevice::ReadWrite); QByteArray data=client->readAll(); client->waitForReadyRead(100); m_pHelloWindow->addMessage(" : " + client->write(data)); } void HelloworldServer::incomingConnection(qintptr handle){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(handle); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //it does not work connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
void HelloworldServer::readyRead(){
QTcpSocket * client = (QTcpSocket *)sender(); client->open(QIODevice::ReadWrite); QByteArray data=client->readAll(); client->waitForReadyRead(100); m_pHelloWindow->addMessage(" : " + client->write(data));
} void HelloworldServer::incomingConnection(qintptr handle){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(handle); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //bu satırın ha varlığı ha yokluğu connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
Here is the server.cpp
#include "server.h"//MAIN WINDOW #include "ui_server.h" Server::Server(QWidget *parent) : QMainWindow(parent), ui(new Ui::Server) { ui->setupUi(this); } Server::~Server() { delete ui; } void Server::addMessage(QString Msg){ ui->textEditMessage->setText(Msg); } void Server::on_pushButtonStart_clicked() { m_pBoxServer= new HelloworldServer(this); bool success = m_pBoxServer->listen(QHostAddress::Any,quint16(ui->textEditPort->toPlainText().toInt())); //bool success = m_pBoxServer->listen(QHostAddress::Any,45444); if(!success){ addMessage("Server Failed"); qDebug()<<"server failed"; } else{ addMessage("Server Started"); } }
What is the readyRead problem ? How can I solve readyRead() problem.
@ELIF said in Client To server message sending:
readyRead() function is not working.
That is not a description of a problem.
I have no idea what you are trying to achieve, but
readyRead()
code looks quite wrong.Even if the sender is a
QTcpSocket *
how wouldclient->open()
make any sense here? If the client is not already open how would it send areadyRead()
signal? I'm afraid all the code in that method looks wrong.You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.
-
@ELIF said in Client To server message sending:
readyRead() function is not working.
That is not a description of a problem.
I have no idea what you are trying to achieve, but
readyRead()
code looks quite wrong.Even if the sender is a
QTcpSocket *
how wouldclient->open()
make any sense here? If the client is not already open how would it send areadyRead()
signal? I'm afraid all the code in that method looks wrong.You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.
If the
sender()
toHelloworldServer::readyRead()
isclient
, as per your code, I don't understand howclient->open()
would be appropriate. How issender()
/client()
not already open if it has signalledreadyRead()
?@JonB said in Client To server message sending:
You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.
Once you have understood that code and how it works you can doubtless make your own additions. If you do not understand how it works then you will struggle.
Meanwhile, if I understand your code/intention correctly, what exactly should we understand "readyRead() function is not working. " to mean? What is not working, what happens/does not happen? To echo back, do the following lines suffice:
QTcpSocket * client = static_cast<QTcpSocket *>(sender()); QByteArray data = client->readAll(); int count = client->write(data); m_pHelloWindow->addMessage(QString(" : %1").arg(count));
-
@JonB said in Client To server message sending:
le, if I understand your code/intention correctly, what exactly should we understand "readyRead() function is not working. " to mean? What is not working, what happens/does not happen? To echo back, do the following lines suffice:
readyRead function is for read message from server interface via socket .
If readyRead function run I will see the message that I write server textEdit. -
@ELIF
I believe/hope the code I put above would work, but not sure if you are saying it does or still does not.Meanwhile a new thread, https://forum.qt.io/topic/136465/sending-data-from-client-to-server, seems to have been opened by user @star673, and I have suggested there that is similar to what you are working on, and you two ought co-operate! I'm thinking the answers will be similar in both threads.
-
@ELIF
I believe/hope the code I put above would work, but not sure if you are saying it does or still does not.Meanwhile a new thread, https://forum.qt.io/topic/136465/sending-data-from-client-to-server, seems to have been opened by user @star673, and I have suggested there that is similar to what you are working on, and you two ought co-operate! I'm thinking the answers will be similar in both threads.
-
@JonB said in Client To server message sending:
'm thinking the answers will be similar in both threads.
Yes we are co-worker.