1

我正在为 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);
4

0 回答 0