设备未打开错误 + QIODevice::write
device not open error + QIODevice::write
我收到错误:
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 实现服务器。服务器部分有两个 类 基于 QTcpServer
和 QTcpSocket
。
说 Server
和 SocketClient
。
我正在服务器中创建 SocketClient 对象,出于测试目的,我打开了 telnet 会话并希望看到该服务器在终端上写入 "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();
}
Class 客户
* 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() );
我收到错误:
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 实现服务器。服务器部分有两个 类 基于 QTcpServer
和 QTcpSocket
。
说 Server
和 SocketClient
。
我正在服务器中创建 SocketClient 对象,出于测试目的,我打开了 telnet 会话并希望看到该服务器在终端上写入 "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();
}
Class 客户
* 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() );