Qt在Linux机器上写入UDP套接字,Windows使用Python从套接字读取
==========更新:=========== 我现在使用的是广播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。我做错了什么 测试顺序:Qt在Linux机器上写入UDP套接字,Windows使用Python从套接字读取,python,qt,udp,python-sockets,qudpsocket,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代码中,我可以看到数据正在被写入,然而,Win
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;
}
//构造函数
UdpBroadcaster::UdpBroadcaster(QObject*父对象):QObject(父对象)
{
//初始化套接字
m_UdpSocket=新的QUdpSocket(此);
//找出我们要广播的ip地址
QString IP地址;
foreach(常量QHostAddress&address,QNetworkInterface::allAddresses()){
if(address.protocol()==QAbstractSocket::IPv4Protocol&&address!=QHostAddress(QHostAddress::LocalHost))
if(address.toString()包含(“10”){
ipAddress=address.toString();
qDebug()我必须允许我的程序通过Windows 7防火墙。现在它可以正常工作。尝试使用“netcat”(也称为“nc”)作为发送方或接收方来调试代码。它非常简单,可在Windows、Mac和Linux上使用。
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()