将Threaded TcpServer上的信息从一个线程传递到另一个线程

时间:2016-05-13 17:03:02

标签: c++ multithreading qt tcp server

我尝试在链接1中创建简单的服务器。 Youtube tutorial to create multithreaded server

void Test_Server::incomingConnection(int socketDescriptor_)
{
    qDebug() << socketDescriptor_ << "connecting...";
    Test_Thread *thread_ = new Test_Thread(number_,socketDescriptor_,this);
    connect(thread_,SIGNAL(finished()),thread_,SLOT(deleteLater()));
    thread_->start();
    number_++;
}

////

void Test_Thread::run()
{
    qDebug() << this->Socket_Descriptor_ << "starting thread";
    socket = new QTcpSocket();
    if(!socket->setSocketDescriptor(Socket_Descriptor_))
    {
        qDebug() << "ERROR";
    }
    connect(socket,SIGNAL(readyRead()),this,SLOT(Ready_read_()),Qt::DirectConnection);
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected_()),Qt::DirectConnection);
    qDebug() << this->Socket_Descriptor_ << "Client connected";
    QByteArray name = QByteArray::number(number_);
    server_->Socket_map_.insert(name,this);
    server_->show_all_connected_sockets_();
    exec();
}

我的目标是将两个客户端连接到服务器(我使用telnet),从客户端1写入服务器,服务器应将数据传递给客户端2。 为此,我已将QMap作为存储指向MyThreads的指针。当从客户端1收到数据时,我正在调用方法:

void Test_Server::write_to_client_(int number, QByteArray data)
{
QByteArray name = QByteArray::number(number);
    Test_Thread *pointer;
    pointer = client_socket_(name);
    connect(this,SIGNAL(send_data_(QByteArray)),pointer,SLOT(write_data_(QByteArray)));
    emit send_data_(data);
    disconnect(this,SIGNAL(send_data_(QByteArray)),pointer,SLOT(write_data_(QByteArray)));
    qDebug() << "void Test_Server::write_to_client_(int number, QByteArray data): data sent";
}

////

void Test_Thread::write_data_(QByteArray data) const
{
    socket->write(data);
    socket->waitForBytesWritten();
}

一般来说,传递信息是有效的,我在客户端1中写了一些数据,而客户端2则显示了它,但我正在测试:

  

TQObject:无法为不同的父级创建子级   线程。

     

父Test_Thread是QNativeSocketEngine(指针1),父线程是&gt;(指针2),当前线程是(指针3);

     

QsocketNotifier:无法从另一个线程启用或禁用套接字通知程序。

我的问题是:如何正确地将数据从客户端1传递到服务器,然后传递到客户端2?我已经完成了研究,问题在于正确使用信号和插槽,但我无法以正确的方式找到它。

2 个答案:

答案 0 :(得分:0)

Test_Thread::write_data未在创建套接字的同一线程上运行,即Test_Thread::run()。在QThread类中,只有run方法上运行的内容才会在单独的线程上运行。

答案 1 :(得分:0)

我终于解决了问题。为此,我遵循了此处描述的类似问题解决方案:PROBLEM & SOLUTION 我已经辞职使用MyThread类,而是创建了类Worker并将其移动到如下所示的线程:

void Test_Server::incomingConnection(int socketDescriptor_)
{
    qDebug() << "void Test_Server::incomingConnection current thread: " << QThread::currentThread();
    qDebug() << socketDescriptor_ << "connecting...";
    Socket_map_.insert(number_,QByteArray::number(socketDescriptor_));
    QThread *thread_= new QThread;
    qDebug() << "void Test_Server::incomingConnection new thread_: " << thread_->thread();
    Test_Worker *worker = new Test_Worker(socketDescriptor_);
    worker->moveToThread(thread_);
    connect(thread_,SIGNAL(started()),worker,SLOT(create_socket_()));
    connect(this,SIGNAL(pass_socket_descriptor_(int)),worker,SLOT(set_socket_descriptor_(int)));
    connect(worker,SIGNAL(finished()),thread_,SLOT(quit()));
    connect(worker,SIGNAL(finished()),worker,SLOT(deleteLater()));
    connect(thread_,SIGNAL(finished()),thread_,SLOT(deleteLater()));
    connect(worker,SIGNAL(pass_data_to_server_(QByteArray,QByteArray)),this,SLOT(data_from_socket_(QByteArray,QByteArray)));
    connect(this,SIGNAL(pass_data_to_client_(QByteArray,QByteArray)),worker,SLOT(show_data_received_from_server_(QByteArray,QByteArray)));
    number_++;
    thread_->start();
}
  

提示:当我通过test_server信号创建套接字时create_socket_(int)   和socket create_socket(int),程序没有正常工作。修理   将信号从起始线程连接到套接字 - create_socket _

现在成功编程,没有错误从客户端1接收数据并将其传递给客户端2。