Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QUdpSocket不';t发出readyRead()信号_C++_Qt_Sockets_Qtnetwork_Qudpsocket - Fatal编程技术网

C++ QUdpSocket不';t发出readyRead()信号

C++ QUdpSocket不';t发出readyRead()信号,c++,qt,sockets,qtnetwork,qudpsocket,C++,Qt,Sockets,Qtnetwork,Qudpsocket,我遇到了QUdpSocket的问题。信号readyRead()似乎从未发出。所以,我决定创建QTimer并检查套接字读取队列的状态。通过这种方式,我确保了套接字正常工作(bytesavable()显示字节数),并且信号/插槽机制也正常工作(发生超时()信号)。但是为什么readyRead()不发射呢?谢谢 Qt 5.1 QString EthernetListener::listen() { udp_socket = new QUdpSocket(this); connect

我遇到了
QUdpSocket
的问题。信号
readyRead()
似乎从未发出。所以,我决定创建
QTimer
并检查套接字读取队列的状态。通过这种方式,我确保了套接字正常工作(
bytesavable()
显示字节数),并且信号/插槽机制也正常工作(发生超时()信号)。但是为什么
readyRead()
不发射呢?谢谢

Qt 5.1

QString EthernetListener::listen()
{
     udp_socket = new QUdpSocket(this);
     connect(udp_socket, SIGNAL(readyRead()), this, SLOT(process_messages()));
     QTimer *timer = new QTimer(this);
     connect(timer, SIGNAL(timeout()), this, SLOT(dummy_slot()));
     timer->start(1000);
     bool res = udp_socket->bind(QHostAddress::Any, 1947, QUdpSocket::ShareAddress);
     if (!res)
         return QString("Не удалось подключиться к хосту").toUtf8();
     return QString("Идет прослушка сети. Хост: ");
}

void EthernetListener::dummy_slot()
{
    int test = udp_socket->bytesAvailable();
}

void EthernetListener::process_messages()
{
     bool bp = true;
}

您可以尝试使用以下方法实现基本接收器:

可能您没有正确读取插槽中的数据,因此看起来信号没有发出。或者您在连接插槽后正在调用
bind
:可能您应该在连接插槽之前调用
bind

如果仍在处理第一个数据报时有更多数据可用,则可能出现此问题。将此项添加到插槽中,应该可以正常工作:

int readCount;
while (udpSock->hasPendingDatagrams())
{
    readCount = udpSock->readDatagram(buffer, 4096);
    cout << "readCount = " << readCount << endl;
}
int读取计数;
而(udpSock->hasPendingDatagrams())
{
readCount=udpSock->readDatagram(缓冲区,4096);

是的,我编译了这个示例,它成功了。此外,我使用udpreciver中的函数作为readyRead()插槽,但没有任何更改。而且,正如您在代码中看到的,插槽在bind()之前连接。仍然想知道为什么我的示例不起作用。我使用Win32 bitIt,这意味着您必须在连接插槽之前调用bind。您尝试过吗?延迟连接()吗希望信号触发没有任何意义。在连接之前调用bind。没有更改