# Sending and receiving UDP broadcast message in C++ in Windows Phone.

This topic is 663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't know. I try to rewrite this metronuggest code to c++ and I get this:

If you're not going to actually follow the questions and instructions that people offer in this thread, you are very unlikely to get good help from the thread.

You said:

DatagramSample runs very well

Does the datagram sample run well with broadcast?

You said:

I don't know.

I suggest that you actually change the datagram sample to use broadcast, without porting anything to another language.
If you can make that run well with broadcast, then you can prove that your network and hardware is not the problem.
If you cannot make that run with broadcast, then it's possible that your network is the problem.

##### Share on other sites

I don't know. I try to rewrite this metronuggest code to c++ and I get this:

If you're not going to actually follow the questions and instructions that people offer in this thread, you are very unlikely to get good help from the thread.

You said:

DatagramSample runs very well

Does the datagram sample run well with broadcast?

You said:

I don't know.

I suggest that you actually change the datagram sample to use broadcast, without porting anything to another language.
If you can make that run well with broadcast, then you can prove that your network and hardware is not the problem.
If you cannot make that run with broadcast, then it's possible that your network is the problem.

Ok, If you want to know, I reply to you: Broadcast of datagram sample runs really strengly, When I send datagram from PC, I get it on the phone, but when I send it on the phone, I don't get it on the PC, I don't know why, so I start to remake this short code, but it didn't runs well. Can you look at it and say, what can be wrong with it?

##### Share on other sites

When I send datagram from PC, I get it on the phone, but when I send it on the phone, I don't get it on the PC,

Maybe the phone hardware or networking doesn't actually support broadcasts?

Can you look at it and say, what can be wrong with it?

What have you tried to do yourself in debugging?
If you put a breakpoint and step through the code, what happens?
What do you expect to happen differently?

##### Share on other sites

When I send datagram from PC, I get it on the phone, but when I send it on the phone, I don't get it on the PC,

Maybe the phone hardware or networking doesn't actually support broadcasts?

Can you look at it and say, what can be wrong with it?

What have you tried to do yourself in debugging?
If you put a breakpoint and step through the code, what happens?
What do you expect to happen differently?

This code runs well, so I don't get any exeptions. I see a send packet in wireshark, but I don't get a info about messagereceive. I don't get it why. When I have a packet in LAN, messagereceive from datagramsocket should get it, but it doesn't, why?

Edited by MiXen

##### Share on other sites

I see a send packet in wireshark, but I don't get a info about messagereceive.

If so, it's likely that your listening port or some other listening filter (which may include the machine firewall) are incorrect on the socket.

##### Share on other sites
I see correct port and destination ip on wireshark. About firewall I think about it, but in Phone it's imposible :) i know, that a listener is properly working for unicast, but it doesn't see multicast packets(of Course with joinmulticastgroup). Any other ideas?

##### Share on other sites
I still don't get the exact problem here, because your different statemetns are ambiguous to me.
From your latest statement, I guess the following to be true:

1) You are running Wireshark on the PC
2) Your PC and your phone are on the same wireless network and wireless access point
3) The phone is not using mobile data

Now, assuming those are correct, here are some questions:

6) Does the program on your phone receive a packet if it's sent to the correct broadcast address and port? (My guess is "no")
7) Does the PC receive a packet if your phone sends a message to the broadcast address and the right port? (My guess is "yes")
8) Does the program on your phone receive a packet if you send to the phone's address (rather than the broadcast addres)? (My guess is "yes" but you have to test this)

If all the facts are as stated above, then one of two things are true:

You probably should post a couple of lines from the Wireshark capture (one with an outgoing packet that isn't received, and one with an incoming packet that is received) so we can compare data.

##### Share on other sites

Ok, Let's start:

1. yes

2. yes

3. yes

4. yes - that I said, Wireshark gives me this info

5. no - I'm afraid, but I cannot gets this sended packed, whenether it is send by PC or phone.

6. no

7. no

8. no - I try to do it on my ported code from metronugges c# to c++ and when I try send directly, I don't see any data

a ) I don't know, but I think, that problem isn't here.

b) I remake my code to use multicast istead of broadcast, but the main idea is the same(i use JoinMulticastGroup to connect to one multicast group)

This is dump from wireshark(no matter if it is the phone or PC, result is the same): 180    32.742286000    192.168.0.11    224.0.0.251    MDNS    48    Unknown operation (13) 0x4865 [Malformed Packet]

113    16.641300000    192.168.0.10    224.0.0.251    MDNS    48    Unknown operation (13) 0x4865 [Malformed Packet]

What does it mean malformed packet?

Edited by MiXen

##### Share on other sites

If you put a console log statement before the try, and inside the catch, does either of them print something useful?
(Or put a breakpoint at those two places to see what's going on.)

Also, what if you just send a few bytes using a simpler mechanism, like a raw array of four bytes?

##### Share on other sites

Hi MiXen,

While searching for a solution to the same problem,I found something in your code which made mine work (had forgot to use StoreAsync()).

This now works for me:

		int port=40964;
HostName ^bcast=ref new HostName("192.168.1.255");
try {
// Try getting all exceptions from the continuation chain above this point.
LogD("Start broadcast successful on port "+port);
writer->UnicodeEncoding=UnicodeEncoding::Utf8;
writer->ByteOrder=ByteOrder::BigEndian;
for (auto i=0; i<200; i++) {
writer->WriteString("Hallo"+i+" ...");
try {
} catch (Exception ^e) {
}
});
if (!action.get()) {
}
}
});
} catch (Exception^ exception) {
LogD("Error starting broadcast loop on port "+port);
}
});

As you can see, it is only a skeleton for debugging purposes. But I was able to check that the broadcast was working using an Android app which listens for messages on the same port.

LogD is just a procedure I wrote for outputting debugging messages on the console

##### Share on other sites

This topic is 663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

• 14
• 17
• 9
• 15
• 60