I was looking to write a simple desktop app and a corresponding mobile app, the scenario would be:
A very basic app like that would be sending simple text messages between the two.
I guess an example app would be something like a "remote control" for your PC on your phone, one can be found on the market here.
I started with looking at UDP multicast, and spent a day trying to get it to work. The idea would be both apps join the same multicast group, phone when joining sends a message to that group, PC picks it up and responds with it's IP, phone then makes TCP connection to the desktop app. Sounds like a valid solution, right? Well it seems like no matter what I tried, this would happen:
Here is a post with code when I tried to use sockets-for-pcl for that.
Here is my attempt to use what's already in .NET:
PC - sending a message only:
UdpClient udp = new UdpClient(port);
IPAddress group_ip = IPAddress.Parse("139.100.100.100");
IPEndPoint client_ipep = new IPEndPoint(group_ip, 3344);
byte[] b = System.Text.Encoding.UTF8.GetBytes(txtEntry.Text);
udp.Send(b, b.Length, client_ipep);
PHONE - receive only:
HostName hostName = new HostName("139.100.100.100");
DatagramSocket udp = new DatagramSocket();
udp.MessageReceived += (sender, args) =>
{
uint length = args.GetDataReader().ReadUInt32();
string text = args.GetDataReader().ReadString(length);
};
await udp.BindServiceNameAsync("3344");
udp.JoinMulticastGroup(hostName);
However, after a day I gave up, since the documentation is scarce and debugging would require me to get some network scanning tools...
When I was searching on the internet for some libraries for Network Service Discovery I found some Android docs but nothing for Windows Phone 8.1. So I guess my question would be: is there anything like that for Windows Phone? Or if you spot anything wrong with my code, what do I have to change to get it to communicate? Thanks.
Don't forget to set a valid port number and receive multicast data asynchronously
This sample might help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With