Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:50:23+00:00 2026-06-09T10:50:23+00:00

Qt broadcastreceiver is not working with sender on Linux. sender is written in c.

  • 0

Qt broadcastreceiver is not working with sender on Linux. sender is written in c.

I have tested Qt broadcastreceiver with Qt broadcastsender on windows which is ok and also Qt broadcastsender working fine with linux receiver which is in C.

what may be the problem while receiving data from linux on windows.

here is the code for the same.

sender (Qt/windows):
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

HEADERS       = receiver.h
SOURCES       = receiver.cpp \
                main.cpp
QT           += network

# install
target.path = $$[QT_INSTALL_EXAMPLES]/network/broadcastreceiver
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS broadcastreceiver.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/network/broadcastreceiver
INSTALLS += target sources

symbian: {
    TARGET.CAPABILITY = NetworkServices
    include($$PWD/../../symbianpkgrules.pri)
}
maemo5: include($$PWD/../../maemo5pkgrules.pri)
contains(MEEGO_EDITION,harmattan): include($$PWD/../../harmattanpkgrules.pri)

sender.c (C/Linux)

#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;
}

Regards,
Gurmeet

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:50:26+00:00Added an answer on June 9, 2026 at 10:50 am

    I have tried it out your code and almost everything is working fine.
    The only problem is, that arriving packages are not displayed correctly in your view.
    After the first package arrives, it says:

    Listening for broadcasted messagesReceived datagram: "This is packet
    

    Then it stops, because the content of the packet is a 0 byte which terminates the QString.
    This also means that all following messages are not displayed anymore.

    I don’t know if this solves your problem, maybe no data arrives at all.
    I am using Linux, I can not assure you anything for Windows (although Qt should work as well on Windows as on Linux) but I can assure you that your code is working on Linux.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Sms Broadcast Receiver class that extends broadcastreceiver to handle incoming sms,
I have an AlertDialog which appears initiated by a BroadcastReceiver - so the AlertDialog
Possible Duplicate: Android SMS Receiver not working I am in the beginning stages of
somehow my mind is not working and mild fever didn't help. I have the
I have this Broadcast receiver registered public class NotifyAlarmBroadcast extends BroadcastReceiver{ public Context context;
My app uses a BroadcastReceiver and a service to perform background updates. I have
I have a BroadcastReceiver that listens for incoming sms messages. While a new sms
Currently I am working on BroadcastReceiver, Service & AlarmManager in Android to developed one
I have a problem with my BroadcastReceiver. Basically, I have a function in my
HI I have a working SmsReceiver and a method that will search to contacts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.