【问题标题】:device not open error + QIODevice::write设备未打开错误 + QIODevice::write
【发布时间】:2015-10-27 05:37:28
【问题描述】:

我得到错误:

QIODevice::write (QTcpSocket): 设备未打开。 尝试后,我认为问题是将参数 server->nextPendingConnection() 传递给对象。有人可以知道如何正确地做到这一点吗?

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

我正在使用 Qt 实现服务器。服务器部分有两个基于QTcpServerQTcpSocket 的类。 说ServerSocketClient。 我正在服务器中创建 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();
}

客户端类

 * 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:
};

【问题讨论】:

  • SockClient 构造函数的第二个参数 (socket) 是否有默认参数?
  • @Jan 1,不,它没有

标签: c++ qt client-server tcp-ip


【解决方案1】:

你使用:

socketClient = new QTcpSocket( socket );

尝试使用以下代码:

socketClient = socket;

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

而不是

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

【讨论】:

  • 不,实际上程序崩溃了,它不起作用,这是有道理的,因为 socketClient = socket 超出了范围。
  • 能否请您首先检查您的服务器应用程序是否在端口:9999 上侦听。在 Linux 上尝试:netstat -nap | grep 听
  • 嗯,穆拉德,我尝试了其他方法,它有效,但那不是我想要的。我所做的是-> 1) 在函数 void Server::incomingConnection() 我添加了以下行 QTcpSocket *socket = server->nextPendingConnection(); socket->write("你好客户端\r\n");套接字->冲洗();套接字->waitForBytesWritten(3000);套接字->关闭(); }
  • 它确实有效,因此证明服务器正在监听端口 9999
  • socketforClient = new SockClient( server_obj->nextPendingConnection()) 但在头文件构造函数中有两个参数:QObject* parent 和 QTcpSocket* socket,默认为 0。尝试使用:socketforClient = new SockClient (this, server_obj->nextPendingConnection())
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
相关资源
最近更新 更多