我正在为Windows Phone 8编写测试应用程序并在模拟器中进行测试。我想创建一个UDP套接字并确定使用的本地套接字端点。我得到的问题是模拟器有两个网络接口,其中只有一个具有外部网络连接。默认情况下,使用非连接接口。如何在创建套接字时确保使用正确的适配器?
Socket m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_socket.SetNetworkPreference(NetworkSelectionCharacteristics.NonCellular);
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
receiveArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
receiveArgs.SetBuffer(new Byte[2048], 0, 2048);
receiveArgs.Completed += SocketRead_Completed;
// If I bind to the specific IP address of the connected adapter it will work.
//m_socket.Bind(new IPEndPoint(IPAddress.Parse("10.1.1.7"), 0));
// if I leave it up to WP8 the wrong adapter gets used.
m_socket.Bind(new IPEndPoint(IPAddress.Any, 0));
m_socket.ReceiveAsync(receiveArgs);