PDA

View Full Version : BroadCast message from Linux to windows using QUDPsocket



arunkumaraymuo
24th April 2015, 14:14
I am trying to send a broadcast message from (Raspberry Pi) linux to windows. In my linux system I am using Qt to send broadcast message and in my windows system C# is using to receive data.
I tried to send a broadcast message from my C# (Windows) program to Qt(linux) that worked fine but i didnt succeded to send a broadcast message from my linux system to windows.


Qt code to send boroadcast message


QUdpSocket *m_pUDPSocketSend = new QUdpSocket();

m_pUDPSocketSend->connectToHost(QHostAddress("255.255.255.255"), 4220);
string strVal="I am here\n";
const char * data = strVal.c_str();
m_pUDPSocketSend->write(data);

My C# listener code


private const int listenPort = 4220;

private static void StartListener()
{
bool done = false;

UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive(ref groupEP);

Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}

public static int Main()
{
for (int i = 0; i < 10; i++)
{
StartListener();
}



return 0;
}

Cruz
25th April 2015, 20:55
You could try copying the broadcast sender example (http://doc.qt.io/qt-5/qtnetwork-broadcastsender-example.html). Use Wireshark to sniff packets and test if they arrive on your host. Check the Windows firewall which can get in the way.