How to send data from client to server QT TCP/IP
-
wrote on 10 May 2022, 06:36 last edited by ELIF 5 Oct 2022, 07:22
I have two QT Widget project,Client and HelloWorldServer.My aim
1.Start server
2.Constitute a message part in client side then send this message client to server ui text element.In client mainwindow.cpp I have
void MainWindow::on_pushButton_clicked()//CONNECT TO SERVER(host) { m_clientSocket->connectToHost(ui->textEditIpAdrress->toPlainText(),quint16(ui->textEditPortAdrres->toPlainText().toInt())); if (m_clientSocket->waitForConnected(1000)) qDebug("Connected!"); connect(m_clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); }
void MainWindow::displayError(QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: QMessageBox::information(this,tr("connection was refused"),tr("RemoteHostClosedError")); break; case QAbstractSocket::HostNotFoundError: QMessageBox::information(this,tr("connection was refused"),tr("HostNotFoundError")); break; case QAbstractSocket::ConnectionRefusedError: QMessageBox::information(this,tr("Fortune Client"),tr("ConnectionRefusedError")); break;and default: QMessageBox::information(this,tr("Fortune Client"), tr("the following error occured %1.").arg(m_clientSocket->errorString())); } }
void MainWindow::on_pushButton_2_clicked()//send data via socket to host { QString message=ui->textEditMessageAdres->toPlainText().trimmed(); if(!message.isEmpty()){ m_clientSocket->write(QString(message + "\n").toUtf8()); qDebug() <<message; } ui->textEditMessageAdres->clear(); ui->textEditMessageAdres->setFocus(); }
In server helloworldserver.cpp I have
HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent) { m_pHelloWindow = pHelloServer; } void HelloworldServer::incomingConnection(int socketfd){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(socketfd); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client- >peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::readyRead(){ //I want to get the message from client QTcpSocket *client= (QTcpSocket*) sender(); while(client->canReadLine()){ QString line = QString::fromUtf8(client->readLine()).trimmed(); m_pHelloWindow->addMessage(line); qDebug() << " " << line; qDebug() << client->readAll(); } } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
When I click the Connect there is no connect message on console.
When I click send button I want to see it on server side (on status message) but there is no message on server side.
How to solve this issue?Any helps will be appreciated! Thanks.
-
I have two QT Widget project,Client and HelloWorldServer.My aim
1.Start server
2.Constitute a message part in client side then send this message client to server ui text element.In client mainwindow.cpp I have
void MainWindow::on_pushButton_clicked()//CONNECT TO SERVER(host) { m_clientSocket->connectToHost(ui->textEditIpAdrress->toPlainText(),quint16(ui->textEditPortAdrres->toPlainText().toInt())); if (m_clientSocket->waitForConnected(1000)) qDebug("Connected!"); connect(m_clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); }
void MainWindow::displayError(QAbstractSocket::SocketError socketError){ switch(socketError){ case QAbstractSocket::RemoteHostClosedError: QMessageBox::information(this,tr("connection was refused"),tr("RemoteHostClosedError")); break; case QAbstractSocket::HostNotFoundError: QMessageBox::information(this,tr("connection was refused"),tr("HostNotFoundError")); break; case QAbstractSocket::ConnectionRefusedError: QMessageBox::information(this,tr("Fortune Client"),tr("ConnectionRefusedError")); break;and default: QMessageBox::information(this,tr("Fortune Client"), tr("the following error occured %1.").arg(m_clientSocket->errorString())); } }
void MainWindow::on_pushButton_2_clicked()//send data via socket to host { QString message=ui->textEditMessageAdres->toPlainText().trimmed(); if(!message.isEmpty()){ m_clientSocket->write(QString(message + "\n").toUtf8()); qDebug() <<message; } ui->textEditMessageAdres->clear(); ui->textEditMessageAdres->setFocus(); }
In server helloworldserver.cpp I have
HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent) { m_pHelloWindow = pHelloServer; } void HelloworldServer::incomingConnection(int socketfd){ QTcpSocket *client=new QTcpSocket(this); client->setSocketDescriptor(socketfd); clients.insert(client); m_pHelloWindow->addMessage("New client from: " + client- >peerAddress().toString()); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(client,SIGNAL(disconnected()),this,SLOT(disconnected())); } void HelloworldServer::readyRead(){ //I want to get the message from client QTcpSocket *client= (QTcpSocket*) sender(); while(client->canReadLine()){ QString line = QString::fromUtf8(client->readLine()).trimmed(); m_pHelloWindow->addMessage(line); qDebug() << " " << line; qDebug() << client->readAll(); } } void HelloworldServer::disconnected(){ QTcpSocket *client= (QTcpSocket*) sender(); qDebug()<<"Client disconnected :"<< client->peerAddress().toString(); clients.remove(client); }
When I click the Connect there is no connect message on console.
When I click send button I want to see it on server side (on status message) but there is no message on server side.
How to solve this issue?Any helps will be appreciated! Thanks.
@ELIF From the code you posted it is not clear whether your server is listening for incoming connections at all.
Is your HelloworldServer a subclass of QTcpServer? -
wrote on 10 May 2022, 07:29 last edited by
@jsulm said in How to send data from client to server QT TCP/IP:
HelloworldServer a subclass of QTcpServer?
yes HelloworldServer is a subclass of QTcpServer? We can see it below
HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent): QTcpServer(parent)
-
@ELIF From the code you posted it is not clear whether your server is listening for incoming connections at all.
Is your HelloworldServer a subclass of QTcpServer?wrote on 10 May 2022, 07:32 last edited by ELIF 5 Oct 2022, 07:32@jsulm said in How to send data from client to server QT TCP/IP:
your server is listening for incoming connections at all.
-
@jsulm said in How to send data from client to server QT TCP/IP:
HelloworldServer a subclass of QTcpServer?
yes HelloworldServer is a subclass of QTcpServer? We can see it below
HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent): QTcpServer(parent)
@ELIF Then the signature here is wrong: void incomingConnection(int socketfd)
Correct one is void incomingConnection(qintptr socketDescriptor)
int != qintptr.
You should add "override" keyword after incomingConnection declaration, so the compiler can check whether you're really overriding this method.
1/5