QIODevice::write (QTcpSocket): device not open please help me
-
I am trying to using one of the several clients sending data to server and server will send the data to all clients. But it always shows me QIODevice:: write (QTcpSocket): device not open
the code is
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pbConnect, &QPushButton::clicked,
this, &MainWindow::connectToMiddleServer);
statusBar()->showMessage("Ready");tcpServer = new QTcpServer(this); pcsSockets = new QTcpSocket(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::sendMessage);
tcpServer->listen(QHostAddress::Any, 42207);
}void MainWindow::sendMessage()
{QByteArray dataToSend; QDataStream out(&dataToSend, QIODevice::WriteOnly); out << (quint16)0; out << ui->lineEdit->text(); out.device()->seek(0); out << (quint16) (dataToSend.size() - sizeof(quint16));
pcsSockets->write(dataToSend);
}
Can any one help? thanks
-
Ur sending data without accepting connection. When new connection arrives come to slot and do nextpendingconnection to get socket for communication. U should use this socket for data tranfer. Refer examples in qt directory. U will get it.
-
Hi, I recommend that you look at either the fortune example or the threaded fortune example. I have written one based on those examples that works excellent.
But basically it doesn't look like you are setting the socketDescriptor to me. If you can paste all of the code I might be able to help you more.
Chris
-
Hi, I recommend that you look at either the fortune example or the threaded fortune example. I have written one based on those examples that works excellent.
But basically it doesn't look like you are setting the socketDescriptor to me. If you can paste all of the code I might be able to help you more.
Chris
@Chrisw01 thanks so much I upload the server and the clients I will appreciate you can help me out.
the header is:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtNetwork>namespace Ui {
class MainWindow;
}class QTcpSocket;
class MainWindow : public QMainWindow
{
Q_OBJECT
using Message = QPair<QString, QByteArray>;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void connectToMiddleServer();
void connectionSuccess();
void connectionError();
void newMessage();void sendMessage();
private:
Ui::MainWindow ui;
QTcpSocket socket{nullptr};
quint16 messageSize{0};QTcpServer *tcpServer; QTcpSocket *pcsSockets;
};
#endif // MAINWINDOW_H
the cpp is :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTcpSocket>
#include <QDataStream>
#include <QtNetwork>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pbConnect, &QPushButton::clicked,
this, &MainWindow::connectToMiddleServer);
statusBar()->showMessage("Ready");tcpServer = new QTcpServer(this); pcsSockets = new QTcpSocket(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::sendMessage);
tcpServer->listen(QHostAddress::Any, 6666);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::connectToMiddleServer() {
ui->leAddress->setEnabled(false);
ui->pbConnect->setEnabled(false);socket = new QTcpSocket(this); connect(socket, &QTcpSocket::readyRead, this, &MainWindow::newMessage); connect(socket, &QTcpSocket::connected, this, &MainWindow::connectionSuccess); connect(socket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &MainWindow::connectionError); messageSize = 0; //reset messageSize statusBar()->showMessage("Connecting. . ."); socket->connectToHost("192.168.2.39", 12345);
}
void MainWindow::connectionSuccess() {
statusBar()->showMessage("Connected to server");
}void MainWindow::connectionError() {
statusBar()->showMessage("Connection error");
ui->leAddress->setEnabled(true);
ui->pbConnect->setEnabled(true);
}void MainWindow::newMessage() {
QDataStream in(socket);if(messageSize == 0) { if(socket->bytesAvailable() < (int)sizeof(quint16)) return; in >> messageSize; } if(socket->bytesAvailable() < messageSize) return; Message message; in >> message; messageSize = 0; //write message ui->teMessages->append(message.first + ": " + QString::fromUtf8(message.second));
}
void MainWindow::sendMessage()
{QByteArray dataToSend; QDataStream out(&dataToSend, QIODevice::WriteOnly); out << (quint16)0; out << ui->lineEdit->text(); out.device()->seek(0); out << (quint16) (dataToSend.size() - sizeof(quint16));
pcsSockets->write(dataToSend);
}
-
Rather than try and figure this out I'll show you my code to point you in the right direction, it is derived from threadedFortuneServer example..
My server header file.
#ifndef SPEEDYSYNCSERVER_H #define SPEEDYSYNCSERVER_H #include <QTcpServer> class speedySyncServer : public QTcpServer { Q_OBJECT public: explicit speedySyncServer(QObject *parent = 0); signals: protected: void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE; }; #endif // SPEEDYSYNCSERVER_H
My server CPP file.
#include "speedysyncserver.h" #include "speedysyncremote.h" #include <QDebug> #include <QString> speedySyncServer::speedySyncServer(QObject *parent) : QTcpServer(parent) { } void speedySyncServer::incomingConnection(qintptr socketDescriptor) { QTcpSocket tcpSocket; if(!tcpSocket.setSocketDescriptor(socketDescriptor)) { return; } connect(&tcpSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); socketConnected = true; QByteArray data; QDataStream outStream(&data, QIODevice::ReadWrite); outStream.setVersion(QDataStream::Qt_5_7); outStream << "Elloh\r\n"; tcpSocket.write(data); while (socketConnected == true) { tcpSocket.waitForReadyRead(); inputString.append(tcpSocket.readLine()); if(inputString.endsWith("\r\n", Qt::CaseInsensitive) == true) { inputString.remove("\r\n"); if(inputString.compare("\[REQ-VERSION]", Qt::CaseInsensitive) == 0) { outputString.append("Version Requested from ").append(tcpSocket.peerAddress().toString()); emit sendMessage(outputString); tcpSocket.write("\[VERSION=1.0.1a]\r\n"); } else if(inputString.startsWith("\[REQ-FILE] ", Qt::CaseInsensitive) == true) { tcpSocket.write("[SEND-FILE]\r\n"); } inputString.clear(); outputString.clear(); } } } void speedySyncServer::socketDisconnected() { socketConnected = false; }
My Main header which would be your MainWindow header
#ifndef SPEEDYSYNCREMOTE_H #define SPEEDYSYNCREMOTE_H #include <QMainWindow> #include "speedysyncserver.h" #include <QTcpServer> namespace Ui { class speedySyncRemote; } enum { messageInfo, messageCritical, messageError }; class speedySyncRemote : public QMainWindow { Q_OBJECT public: explicit speedySyncRemote(QWidget *parent = 0); ~speedySyncRemote(); public slots: private slots: void socketDisconnected(); private: int socketDescriptor; int socketConnected = false; Ui::speedySyncRemote *ui; speedySyncServer server; }; #endif // SPEEDYSYNCREMOTE_H
My Main CPP class
#include "speedysyncremote.h" #include "speedysyncserver.h" #include "ui_speedysyncremote.h" #include <QDateTime> #include <QTextStream> #include <QHostAddress> #include <QFile> #include <QMessageBox> #include <QNetworkInterface> #include <QTimer> speedySyncRemote::speedySyncRemote(QWidget *parent) : QMainWindow(parent), ui(new Ui::speedySyncRemote) { ui->setupUi(this); if(!server.listen(QHostAddress(QHostAddress::AnyIPv4), 10200)) { QMessageBox::critical(this, tr("speedySyncRemote"), tr("Unable to start server: %1")); close(); return; } } speedySyncRemote::~speedySyncRemote() { delete ui; }
So as you can see, listen for the connection once connected the incomingConnection signal is generated where you set the socket descriptor and then just do what you need to do until you are done or the connection is lost.
Hope this helps..
Chris