2012-08-05 3 views
0

Qt broadcastreceiver가 Linux에서 보낸 사람과 작동하지 않습니다. 발신자는 c로 작성됩니다.Linux에서 C의 UDP 발신자 및 Windows의 Qt에서 수신자가 작동하지 않습니까?

나는 창문에 리눅스에서 데이터를 수신하는 동안 문제가있을 수 있습니다 무엇

C.

에 리눅스 수신기 제대로 작동 확인도 Qt는 broadcastsender입니다 창에 Qt는 broadcastsender와 Qt는 브로드 캐스트 리시버 테스트했습니다.

여기에 같은 코드가 있습니다.

송신기 (Qt는/윈도우) MAIN.CPP

#include <QApplication> 
#include "receiver.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    Receiver receiver; 
    receiver.show(); 
    return app.exec(); 
} 

receiver.h

#ifndef RECEIVER_H 
#define RECEIVER_H 

#include <QtGui> 
#include <QtNetwork/QUdpSocket> 
#include <QWidget> 

class Receiver : public QWidget 
{ 
    Q_OBJECT 

public: 
    Receiver(QWidget *parent = 0); 

private slots: 
    void processPendingDatagrams(); 

private: 
    QLabel *statusLabel; 
    QPushButton *quitButton; 
    QUdpSocket *udpSocket; 
}; 

#endif 

receiver.cpp

#include <QtGui> 
#include <QtNetwork> 

#include "receiver.h" 

#define PORT 45454 

Receiver::Receiver(QWidget *parent): QWidget(parent) 
{ 
    statusLabel = new QLabel(tr("Listening for broadcasted messages")); 
    statusLabel->setWordWrap(true); 

    quitButton = new QPushButton(tr("&Quit")); 

    udpSocket = new QUdpSocket(this); 
    bool b = udpSocket->bind(QHostAddress::Any,PORT, QUdpSocket::ShareAddress); 

    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams())); 
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); 

    QHBoxLayout *buttonLayout = new QHBoxLayout; 
    buttonLayout->addStretch(1); 
    buttonLayout->addWidget(quitButton); 
    buttonLayout->addStretch(1); 

    QVBoxLayout *mainLayout = new QVBoxLayout; 
    mainLayout->addWidget(statusLabel); 
    mainLayout->addLayout(buttonLayout); 
    setLayout(mainLayout); 

    setWindowTitle(tr("Broadcast Receiver")); 
} 

void Receiver::processPendingDatagrams() 
{ 
    while (udpSocket->hasPendingDatagrams()) 
    { 
     QByteArray datagram; 
     datagram.resize(udpSocket->pendingDatagramSize()); 
     udpSocket->readDatagram(datagram.data(), datagram.size()); 
     statusLabel->setText(statusLabel->text() + tr("Received datagram: \"%1\"").arg(datagram.data())); 
    } 
} 

broadcastreceiver.pro

sender.c (C/리눅스)

#include <stdio.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <netinet/in.h> 

#define BUFLEN 512 
#define NPACK 10 
#define PORT 45454 

#define SRV_IP "xxx.xxx.xxx.xxx" 

void diep(char *s) 
{ 
    perror(s); 
    exit(1); 
} 


int main(void) 
{ 
    struct sockaddr_in si_other; 
    int s, i, slen=sizeof(si_other); 
    char buf[BUFLEN]; 

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) 
    { 
     diep("socket"); 
     printf("\n\tSocket error\n"); 
    } 

    memset((char *) &si_other, 0, sizeof(si_other)); 
    si_other.sin_family = AF_INET; 
    si_other.sin_port = htons(PORT); 
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) 
    { 
     fprintf(stderr, "inet_aton() failed\n"); 
     printf("\n\tInet_aton error\n"); 
     exit(1); 
    } 

    for (i=0;; i++) 
    { 
     printf("Sending packet %d\n", i); 
     sprintf(buf, "This is packet %d\n", i); 
     if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1) 
    { 
      diep("sendto()"); 
     printf("\n\tSendto error\n"); 
    } 

    printf("Data written on (IP = %s and port : %d\n\n",inet_ntoa(si_other.sin_addr),ntohs(si_other.sin_port)); 
    sleep(1); 
    } 

    close(s); 
    return 0; 
} 

안부, Gurmeet

+0

Windows에서도 방화벽을 해제했지만 여전히 작동하지 않습니다. –

답변

1

나는 코드를 그것을 밖으로 시도하고 거의 모든 것이 잘 작동한다. 유일한 문제는 도착한 패키지가보기에 올바르게 표시되지 않는다는 것입니다. 제 1 패키지가 도착하면 그것은 말한다 패킷의 콘텐츠는 QString 종료 0 바이트이므로

Listening for broadcasted messagesReceived datagram: "This is packet 

그런 다음 그것이 정지한다. 이것은 또한 모든 다음 메시지가 더 이상 표시되지 않는다는 것을 의미합니다.

이 방법으로 문제가 해결되는지는 모르겠지만 데이터가 전혀 도착하지 않을 수도 있습니다. 저는 리눅스를 사용하고 있습니다. Windows 용으로는 아무 것도 보장 할 수 없지만 (Qt는 Windows와 마찬가지로 Linux에서도 작동해야하지만) 코드가 Linux에서 작동하고 있음을 확신 할 수 있습니다.

관련 문제