gpt4 book ai didi

c++ - 设备未打开错误 + QIODevice::write

转载 作者:可可西里 更新时间:2023-11-01 02:54:13 26 4
gpt4 key购买 nike

我得到错误:

QIODevice::write (QTcpSocket): device not open. After trying , I think problem is passing parameter server->nextPendingConnection() into object. Can someone has idea how to do it correctly?

我的理解是 socketClient 的对象未正确初始化。我正在使用带有 Qt 的 Ubuntu。

我正在使用 Qt 实现服务器。服务器部分有两个基于QTcpServerQTcpSocket 的类。说 ServerSocketClient。我正在服务器中创建 SocketClient 对象,出于测试目的,我打开了 telnet session 并希望看到该服务器在终端上写“hello”。但不知何故它不起作用。有人可以告诉我哪里出错了。

Server::Server(QObject *parent) : QObject(parent)
{
server_obj = new QTcpServer( this );
}

void Server::startServer()
{
connect( server_obj, SIGNAL( newConnection() ), this, SLOT( incomingConnection() ) );
if( !server_obj->listen( QHostAddress::Any, 9999) )
{
qDebug() << " Server failed to get started";
}
else
{
qDebug() << " Server started"; // this is successful
}
}


void Server::incomingConnection()
{
socketforClient = new SockClient( server_obj->nextPendingConnection() );// has a doubt on nextPendingconection??

//only for testing remove it
socketforClient->writeToClient();
}

客户类

 * Description: Its a constructor.I have changed default constructor to add QTcpSocket* object in parameter.I used this constructor in  void Server::incomingConnection()
*/
SockClient::SockClient(QObject *parent,QTcpSocket* socket ) : QObject(parent)
{
socketClient = new QTcpSocket( socket );

qDebug() << " we are in constructor of 1st sockclient ";

}



// this is for testing purpose only

void SockClient::writeToClient()
{
socketClient->write(" hello world\r\n");
socketClient->flush();
socketClient->waitForBytesWritten(3000);

}

//SockClient的头文件

class SockClient : public QObject
{
Q_OBJECT
public:

explicit SockClient( QObject *parent, QTcpSocket* socket= 0 ); // I have created
void writeToClient(); // This is for testing purpose
~SockClient();
signals:

private slots:
void readClient();

private:
void sendResponsetoMops();

QTcpSocket *socketClient;

quint16 nextBlockSize;

public slots:
};

最佳答案

您使用:

socketClient = new QTcpSocket( socket );

尝试改用下面的代码:

socketClient = socket;

并使用 socketforClient = new SockClient(this, server->nextPendingConnection() );

代替

 socketforClient = new SockClient( server->nextPendingConnection() );

关于c++ - 设备未打开错误 + QIODevice::write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com