Reputation: 8384
I tried to create an UDP send and listen application with UWP based on this source code.
It should work like this:
I have the Listen
and Send
methods.
Constructor
public MainViewModel()
{
Listen();
Send();
}
Listen
private async void Listen()
{
listenerSocket = new DatagramSocket();
listenerSocket.MessageReceived += (x, y) =>
{
var a = "2";
};
await listenerSocket.BindServiceNameAsync("8080");
}
Send
private async void Send()
{
IOutputStream outputStream;
string localIPString = GetLocalIp();
IPAddress localIP = IPAddress.Parse(localIPString);
string subnetMaskString = "255.0.0.0";
IPAddress subnetIP = IPAddress.Parse(subnetMaskString);
HostName remoteHostname = new HostName(GetBroadcastAddress(localIP, subnetIP).ToString());
outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, "8080");
DataWriter writer = new DataWriter(outputStream);
writer.WriteString(localIPString);
await writer.StoreAsync();
}
It seems that the broadcast sending is not working.
I have another program (written in Java) that also sends a message to the broadcast address, and this listener receives the messages sent back from the broadcast.
What should I set to make the sending work?
Upvotes: 2
Views: 3544
Reputation: 560
i didn't see any GetBroadcastAddress(localIP, subnetIP)
code here. But as I see, your problem may be with this code:
HostName remoteHostname = new HostName(GetBroadcastAddress(localIP, subnetIP).ToString());
I changed it to:
HostName remoteHostname = new HostName(localIP.ToString());
outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, port);
Here is my sample code:
public sealed partial class MainPage : Page
{
DatagramSocket listenerSocket = null;
const string port = "8080";
public MainPage()
{
this.InitializeComponent();
Listen();
Send();
}
private async void Listen()
{
listenerSocket = new DatagramSocket();
//listenerSocket.MessageReceived += (x, y) =>
//{
// var a = "2";
//};
listenerSocket.MessageReceived += MessageReceived;
await listenerSocket.BindServiceNameAsync(port);
}
private async void Send()
{
IOutputStream outputStream;
string localIPString = GetLocalIp();
IPAddress localIP = IPAddress.Parse(localIPString);
string subnetMaskString = "255.0.0.0";
IPAddress subnetIP = IPAddress.Parse(subnetMaskString);
HostName remoteHostname = new HostName(localIP.ToString());
outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, port);
using (DataWriter writer = new DataWriter(outputStream))
{
writer.WriteString("aaaa");
await writer.StoreAsync();
}
}
//private object GetBroadcastAddress(IPAddress localIP, IPAddress subnetIP)
//{
// throw new NotImplementedException();
//}
async void MessageReceived (DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
{
DataReader reader = args.GetDataReader();
uint len = reader.UnconsumedBufferLength;
string msg = reader.ReadString(len);
string remoteHost = args.RemoteAddress.DisplayName;
reader.Dispose();
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
text.Text = msg;
});
}
private string GetLocalIp()
{
...
}
}
Upvotes: 2