Reputation: 893
I wrote a test program with a DatagramSocket to Receive data. but after receive 5 or 6 packet, this isn't receive more packet!
Does anybody have a solution for this?
My test Code is:
public MainPage()
{
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Listen();
}
private async void Listen()
{
try
{
Windows.Networking.Sockets.DatagramSocket socket = new Windows.Networking.Sockets.DatagramSocket();
socket.MessageReceived += Socket_MessageReceived;
//You can use any port that is not currently in use already on the machine.
string serverPort = "1337";
//Bind the socket to the serverPort so that we can start listening for UDP messages from the UDP echo client.
await socket.BindServiceNameAsync(serverPort);
}
catch (Exception e)
{
//Handle exception.
}
}
public async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
//Read the message that was received from the UDP echo client.
Stream streamIn = args.GetDataStream().AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
string message = await reader.ReadLineAsync();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Alpha.Text = message;
}
);
}
I have traced with wireshark to check that I am receiving data, it gets that but in my visual studio solutions it doesn't work.
Upvotes: 1
Views: 508
Reputation: 26
This is because it happens to be you open another instance of your UDP Listener, or you Initialize in wrong Place!
Upvotes: 1
Reputation: 893
I used Control.InboundBufferSizeInBytes then it works correctly!
listener = new DatagramSocket();
listener.MessageReceived += MessageReceived;
listener.Control.InboundBufferSizeInBytes = 1;
await listener.BindServiceNameAsync("1337");
Upvotes: 2