Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network Service Discovery on Windows Phone

I was looking to write a simple desktop app and a corresponding mobile app, the scenario would be:

  1. I run the desktop app on my laptop
  2. I run the mobile app on my Lumia (on the same wifi network)
  3. The phone app somehow finds and connects to the desktop app (I don't want to have to manually find and type the ip)
  4. The two can now communicate

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:

  1. Scenario: mobile app running on the actual phone connected to the same WiFi as PC app: messages are being sent but never arrive.
  2. Scenario: mobile app running on an emulator on the same PC as PC app: messages send and arrive.

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.

like image 291
Daniel Gruszczyk Avatar asked May 09 '15 16:05

Daniel Gruszczyk


1 Answers

Don't forget to set a valid port number and receive multicast data asynchronously

This sample might help you

like image 72
Hana Bzh Avatar answered Sep 18 '22 08:09

Hana Bzh