Linux机器上Qt写入UDP套接字,Windows从Socket读取Python

2024-09-30 18:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

==========更新:=======

我现在使用的是广播ip 10.6.0.3和更高的端口57890。我可以在Windows网络监视器中看到流量,但仍然无法让Python读取它。在

===============================

我有一台linux机器使用Qt和qdpsocket向ip地址为10.6.0.2的UDP套接字写入一条“hello”消息。我还有一台Windows机器,我想用Python阅读“hello”消息。从我的Qt代码中,我可以看到数据正在被写入,然而,Windows上的Python程序却永远停留在“等待数据”上。windows机器确实可以ping 10.6.0.2。我做错什么了?在

试验顺序:

UdpBroadcaster client; // Binds socket
sleep(10); // During this time , startup Python program
client.WriteData(); // Write hello message

Qt输出:

^{pr2}$

Qt代码:

// Constructor
UdpBroadcaster::UdpBroadcaster(QObject *parent) :QObject(parent)

{

    // Init socket
    m_UdpSocket = new QUdpSocket(this);

    // Figure out our ip address to broadcast on
    QString ipAddress;
    foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
            if ( address.toString().contains("10.") ) {
                ipAddress = address.toString();
                qDebug() << "Determined ip address to be: " << ipAddress;
                break;
            }
    }

    // Bind to localhost
    m_Port = 5678;
    m_AddressToUse = QHostAddress(ipAddress);
    bool didBind = m_UdpSocket->bind(m_AddressToUse,m_Port);
    if ( !didBind ) {
        qDebug() << "Error! Cannot bind to: " << m_AddressToUse << " Port: " << m_Port;
    }
    else {
        qDebug() << "Successfully bound to: " << m_AddressToUse << " Port: " << m_Port;
    }

    // Connect the ready read socket
    connect(m_UdpSocket,SIGNAL(readyRead()),this,SLOT(readReady()));

}

// Write data to socket
void UdpBroadcaster::WriteData() {

    // Debug
    qDebug() << "Sending UDP Datagram now!";

    // Construct a message to send
    QByteArray msg;
    msg.append("Hello!!!");
    qint64 numberOfBytesWritten = m_UdpSocket->writeDatagram(msg,m_AddressToUse,m_Port);
    if ( numberOfBytesWritten == -1 ) {
        qDebug() << "Error! Could not write data to socket...";
    }
    else {
        qDebug() << "Wrote this many bytes: " << numberOfBytesWritten;
    }

}

// Do this when a datagram is available
void UdpBroadcaster::readReady() {

    // Buffer to receive data
    QByteArray buffer;
    buffer.resize(m_UdpSocket->pendingDatagramSize());

    QHostAddress sender;
    quint16 port;
    m_UdpSocket->readDatagram(buffer.data(),buffer.size(),&sender,&port);

    qDebug()<<"Message From: " << sender.toString();
    qDebug()<<"With Port Number " << port;
    qDebug()<<"Message: " << buffer;

}

Python程序:

import socket
import time

def main():

    UDP_IP = "10.6.0.2"
    UDP_PORT = 5678

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.connect((UDP_IP, UDP_PORT))
    cnt = 0
    while cnt < 10:
        print "Waiting for data"
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print "received message:", data
        time.sleep(1)
        cnt = cnt + 1

    ## Wait so we don't lose our terminal
    filename = raw_input()            

if __name__ == '__main__':
    main()

Tags: toipdataifportaddressbuffersocket