Thursday, August 1, 2013

WinRT DatagramSocket does not receive data in store app but receive in console app

Windows Store app code:



_socket = new DatagramSocket();
_socket.MessageReceived += (sender, args) =>
{
var dataReader = args.GetDataReader();
Console.WriteLine("Received {0} bytes from {1}:{2}",
dataReader.UnconsumedBufferLength, args.RemoteAddress.DisplayName, args.RemotePort);
};

await _socket.BindEndpointAsync(new HostName("192.168.0.24"), String.Empty);
LogMessage(String.Format("Socket bound on {0}:{1}", _socket.Information.LocalAddress.DisplayName, _socket.Information.LocalPort));

Same code but in a console app (followed method decrived here using winrt apis in desktop apps )



static void Main(string[] args)
{
StartClient().Wait();
}

private async static Task StartClient()
{
var socket = new DatagramSocket();
socket.MessageReceived += (sender, args) =>
{
var dataReader = args.GetDataReader();
Console.WriteLine("Received {0} bytes from {1}:{2}",
dataReader.UnconsumedBufferLength, args.RemoteAddress.DisplayName, args.RemotePort);
};
//await socket.BindServiceNameAsync(String.Empty);
await socket.BindEndpointAsync(new HostName("192.168.0.24"), String.Empty);
Console.WriteLine("Socket bound to {0}:{1}", socket.Information.LocalAddress.DisplayName, socket.Information.LocalPort);

while (true)
{
await Task.Delay(300);
await Task.Yield();
}
}



These 2 apps just bind a socket to a my local ip:some port.


The sender app is a normal dotnet console app:



static void Main(string[] args)
{
StartServer();
}

static void StartServer()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
Console.WriteLine("Socket bound to {0}", socket.LocalEndPoint);

Console.WriteLine("Enter port to connect to:");
var line = Console.ReadLine();
var address = IPAddress.Parse("192.168.0.24");
var port = int.Parse(line);
var remoteEndPoint = new IPEndPoint(address, port);

while (true)
{
var str = DateTime.Now.ToString();
var buffer = Encoding.ASCII.GetBytes(str);
var sentCount = socket.SendTo(buffer, remoteEndPoint);
Console.WriteLine("Sent {0} bytes to {1}", sentCount, remoteEndPoint);

Thread.Sleep(1000);
}
}


this sender app asks for a port and sends data to my local ip:that port.


OK so the store app does not receive anything but the same code in console app does.


the store app has the following capabities set : internet (client), internet (client & server), private networks (client & server).


Any ideas of what I'm doing wrong ?


thanks!


No comments:

Post a Comment