Qt写入Linux机器上的UDP套接字并使用Python从Socket读取

时间:2014-04-28 21:32:39

标签: python qt udp python-sockets qudpsocket

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

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

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

我有一台linux机器使用Qt和QUDPSocket向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输出:

Initializing UDP Socket 
Determined ip address to be:  "10.6.0.2" 
Successfully bound to:   QHostAddress( "10.6.0.2" )   Port:  5678 
Sending UDP Datagram now! 
Wrote this many bytes:  8 
Message From:  "10.6.0.2" 
With Port Number  5678 
Message:  "Hello!!!" 

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()

1 个答案:

答案 0 :(得分:0)

我不得不通过Windows 7防火墙允许我的程序。现在它工作正常。