在自己的线程中将数据写入 TcpSocket [英] Writing data to a TcpSocket in own Thread

查看:14
本文介绍了在自己的线程中将数据写入 TcpSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的线程 TCP 服务器出现问题.
我可以打开我的服务器,创建一个新的 Socket,我可以通过套接字接收数据(我使用了 readyRead() 信号,然后使用了 readLine() 来读取,这工作正常.现在我想将数据写入这个 Socket来自另一个线程,所以我创建了一个公共插槽 writeData() 来处理这个问题.我将 writeData() 插槽与 QueuedConnection 连接起来(也尝试过 AutoConnection),但是当我调用 m_socket->write() 时我得到的只是一个错误留言:

I got a problem with my threaded TCP-Server.
I can open my Server, a new Socket is created an I can receive data over the socket (I used the readyRead() signal and then used readLine() to read, which is working fine. Now I want to write data to this Socket from another Thread so I created a public slot writeData() which should take care of this. I connected the writeData() slot with QueuedConnection (also tried AutoConnection) but all I get when I call the m_socket->write() is an error message:

QObject:无法为不同线程中的父级创建子级.(父为 QNativeSocketEngine(0x12f0f70),父线程为 ServerThread(0xfbbae8),当前线程是 QThread(0xfa7c48)

QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x12f0f70), parent's thread is ServerThread(0xfbbae8), current thread is QThread(0xfa7c48)

这里只是一个最小的例子:
我省略了从我的另一个线程到 writeData() 插槽的连接,因为我认为每个人都可以想象;)

Just a minimal example here:
I omitted the connect from my other Thread to the writeData() slot because I think everyone can imagine that ;)

class Server : public QTcpServer {
  Server();
protected:
  void incomingConnection(int socketDesc);
}

Server::Server() : QTcpServer() {
  this->listen(QHostAddress::Any, PORT);
}

void Server::incomingConnection(int socketDescriptor) {
  ServerThread *t = new ServerThread(socketDescriptor);
  t->start();
}

class ServerThread : public QThread {
  ServerThread(int socketDescriptor);
protected:
  void run();
public slots:
  void writeData(QString data);
private:
  int m_socketDescriptor;
  QTcpSocket *m_socket;
}

ServerThread::ServerThread(int socketDescriptor) : QThread(), m_socketDescriptor(socketDescriptor) {}

void ServerThread::run() {
  m_socket = new QTcpSocket();
  m_socket->setSocketDescriptor(m_socketDescriptor);
  exec();
}

void ServerThread::writeData(QString data) {
  m_socket->write(data.toAscii());
}

提前致谢.

推荐答案

您的错误信息意味着:

您的信号槽通信发生在创建并属于主线程的 ServerThread 实例上.然而,ServerThread 成员 m_socket 的实例被创建并属于其他线程.

Your signal-slot communication happens on ServerThread instance which was created and belongs to main thread. However instance of ServerThread member m_socket was created and belongs to other thread.

我建议您创建简单的 QThread 并将您的对象移动到其中.

I would suggest you to create simple QThread and move your object into it.

void Server::incomingConnection(int socketDescriptor) {
  QThread *t = new QThread();
  ClientConnection *client = new ClientConnection(socketDescriptor);
  client->moveToThread(t);

  // go
  t->start();
}

class ClientConnection : public QObject {
Q_OBJECT
public:
  ClientConnection(int socketDescriptor, QObject *parent = 0);
public slots:
  void writeData(QString data);
private:
  int m_socketDescriptor;
  QTcpSocket *m_socket;
}

这篇关于在自己的线程中将数据写入 TcpSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆