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

Started by
30 comments, last by frob 6 years, 9 months ago

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


I asked:

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.
enum Bool { True, False, FileNotFound };
Advertisement

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


I asked:

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?

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?
enum Bool { True, False, FileNotFound };

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?

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


In Wireshark, do you see the correct broadcast address and port?

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.
enum Bool { True, False, FileNotFound };
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?
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
4) The PC can properly send broadcast packets to the broadcast address on the correct port
5) If the PC receives a broadcast packet on the correct port, your program receives the packet

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:

a) Your phone doesn't receive broadcast packets. (Does DNLA or other broadcast-based protocols work on the phone?)
b) If you are using the global broadcast address, you need to use the network-specific broadcast address, or vice versa.

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.
enum Bool { True, False, FileNotFound };

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?


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?

Finaly, the address you're using is not a broadcast address.

Your code doesn't tell us what previousTask is, or how the tasks get started.
enum Bool { True, False, FileNotFound };

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;
		DatagramSocket ^broadcaster;
		broadcaster=ref new DatagramSocket();
		//broadcaster->Control->MulticastOnly=true;
		HostName ^bcast=ref new HostName("192.168.1.255");
		create_task(broadcaster->ConnectAsync(bcast, port.ToString())).then([this, port, broadcaster](task<void> previousTask) {
			try {
				// Try getting all exceptions from the continuation chain above this point.
				previousTask.get();
				LogD("Start broadcast successful on port "+port);
				create_task([broadcaster, port]() {
					DataWriter ^writer=ref new DataWriter(broadcaster->OutputStream);
					writer->UnicodeEncoding=UnicodeEncoding::Utf8;
					writer->ByteOrder=ByteOrder::BigEndian;
					LogD("Broadcast on port "+port+" now starting broadcast loop...");
					task<bool> action;
					for (auto i=0; i<200; i++) {
						writer->WriteString("Hallo"+i+" ...");
						create_task(writer->StoreAsync()).then([](task<unsigned int> writeTask) {
							try {
								writeTask.get();
								LogD("Broadcast StoreAsync() successful");
							} catch (Exception ^e) {
								LogD("Broadcast error in StoreAsync()");
							}
						});
						action=create_task(writer->FlushAsync());
						if (!action.get()) {
							LogD("Broadcast error flushing...");
						}
						this_thread::sleep_for(chrono::milliseconds(200));
						LogD("Broadcast on port "+port+" broadcasting Hallo"+i+"...");
					}
				});
			} 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

This topic is closed to new replies.

Advertisement