好的,这是我几天来一直试图弄清楚的。我们在 Windows Phone 7 上有一个应用程序,其中电话加入多播组,然后向该组发送和接收消息以相互交谈。注意 - 这是电话到电话的通信。
现在我正在尝试将此应用程序移植到 Windows Phone 8 - 使用 Visual Studio 2012 中的“转换为 Phone 8”功能 - 到目前为止一切顺利。直到我尝试测试电话到电话的通信。手机似乎很好地加入了该组,并且它们可以发送数据报。他们甚至会收到他们发送给群组的消息 - 但是,没有手机会收到来自另一部手机的消息。
这是我页面背后的示例代码:
// Constructor
public MainPage()
{
InitializeComponent();
}
// The address of the multicast group to join.
// Must be in the range from 224.0.0.0 to 239.255.255.255
private const string GROUP_ADDRESS = "224.0.1.1";
// The port over which to communicate to the multicast group
private const int GROUP_PORT = 55562;
// A client receiver for multicast traffic from any source
UdpAnySourceMulticastClient _client = null;
// Buffer for incoming data
private byte[] _receiveBuffer;
// Maximum size of a message in this communication
private const int MAX_MESSAGE_SIZE = 512;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
_client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT);
_receiveBuffer = new byte[MAX_MESSAGE_SIZE];
_client.BeginJoinGroup(
result =>
{
_client.EndJoinGroup(result);
_client.MulticastLoopback = true;
Receive();
}, null);
}
private void SendRequest(string s)
{
if (string.IsNullOrWhiteSpace(s)) return;
byte[] requestData = Encoding.UTF8.GetBytes(s);
_client.BeginSendToGroup(requestData, 0, requestData.Length,
result =>
{
_client.EndSendToGroup(result);
Receive();
}, null);
}
private void Receive()
{
Array.Clear(_receiveBuffer, 0, _receiveBuffer.Length);
_client.BeginReceiveFromGroup(_receiveBuffer, 0, _receiveBuffer.Length,
result =>
{
IPEndPoint source;
_client.EndReceiveFromGroup(result, out source);
string dataReceived = Encoding.UTF8.GetString(_receiveBuffer, 0, _receiveBuffer.Length);
string message = String.Format("[{0}]: {1}", source.Address.ToString(), dataReceived);
Log(message, false);
Receive();
}, null);
}
private void Log(string message, bool isOutgoing)
{
if (string.IsNullOrWhiteSpace(message.Trim('\0')))
{
return;
}
// Always make sure to do this on the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
string direction = (isOutgoing) ? ">> " : "<< ";
string timestamp = DateTime.Now.ToString("HH:mm:ss");
message = timestamp + direction + message;
lbLog.Items.Add(message);
// Make sure that the item we added is visible to the user.
lbLog.ScrollIntoView(message);
});
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
// Don't send empty messages.
if (!String.IsNullOrWhiteSpace(txtInput.Text))
{
//Send(txtInput.Text);
SendRequest(txtInput.Text);
}
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
SendRequest("start now");
}
为了简单地测试 UDP 堆栈,我从此处找到的 MSDN 下载了示例,并在一对 Windows Phone 7 设备上进行了测试,它按预期工作。然后我转换到 Windows Phone 8 并部署到我的手机上,设备似乎再次启动了它们的连接,用户可以输入他们的名字。但是,这些设备再次无法看到其他设备或与其他设备通信。
最后,我使用新的 DatagramSocket 实现实现了一个简单的通信测试,我再次看到成功启动,但没有设备间通信。
这是使用数据报套接字实现的页面后面的相同代码:
// Constructor
public MainPage()
{
InitializeComponent();
}
// The address of the multicast group to join.
// Must be in the range from 224.0.0.0 to 239.255.255.255
private const string GROUP_ADDRESS = "224.0.1.1";
// The port over which to communicate to the multicast group
private const int GROUP_PORT = 55562;
private DatagramSocket socket = null;
private void Log(string message, bool isOutgoing)
{
if (string.IsNullOrWhiteSpace(message.Trim('\0')))
return;
// Always make sure to do this on the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
string direction = (isOutgoing) ? ">> " : "<< ";
string timestamp = DateTime.Now.ToString("HH:mm:ss");
message = timestamp + direction + message;
lbLog.Items.Add(message);
// Make sure that the item we added is visible to the user.
lbLog.ScrollIntoView(message);
});
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
// Don't send empty messages.
if (!String.IsNullOrWhiteSpace(txtInput.Text))
{
//Send(txtInput.Text);
SendSocketRequest(txtInput.Text);
}
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
socket = new DatagramSocket();
socket.MessageReceived += socket_MessageReceived;
try
{
// Connect to the server (in our case the listener we created in previous step).
await socket.BindServiceNameAsync(GROUP_PORT.ToString());
socket.JoinMulticastGroup(new Windows.Networking.HostName(GROUP_ADDRESS));
System.Diagnostics.Debug.WriteLine(socket.ToString());
}
catch (Exception exception)
{
throw;
}
}
private async void SendSocketRequest(string message)
{
// Create a DataWriter if we did not create one yet. Otherwise use one that is already cached.
//DataWriter writer;
var stream = await socket.GetOutputStreamAsync(new Windows.Networking.HostName(GROUP_ADDRESS), GROUP_PORT.ToString());
//writer = new DataWriter(socket.OutputStream);
DataWriter writer = new DataWriter(stream);
// Write first the length of the string as UINT32 value followed up by the string. Writing data to the writer will just store data in memory.
// stream.WriteAsync(
writer.WriteString(message);
// Write the locally buffered data to the network.
try
{
await writer.StoreAsync();
Log(message, true);
System.Diagnostics.Debug.WriteLine(socket.ToString());
}
catch (Exception exception)
{
throw;
}
finally
{
writer.Dispose();
}
}
void socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
try
{
uint stringLength = args.GetDataReader().UnconsumedBufferLength;
string msg = args.GetDataReader().ReadString(stringLength);
Log(msg, false);
}
catch (Exception exception)
{
throw;
}
}
昨晚我把手机带回家在我的家庭无线网络上测试它们,低,看我成功的设备通信。
回顾一下 - 我的旧版 Windows Phone 7 代码在我的工作网络上运行良好。Windows Phone 8 的端口(没有实际代码更改)不发送设备间通信。此代码确实适用于我的家庭网络。代码在附加调试器的情况下运行,并且在执行期间任何地方都没有错误或异常的迹象。
我使用的手机是:
Windows Phone 7 - 诺基亚 Lumia 900 (* 2)、诺基亚 Lumia 800 (* 3) Windows Phone 8 - 诺基亚 Lumia 920 (* 1)、诺基亚 Limia 820 (* 2)
这些都运行最新的操作系统,并且处于开发者模式。开发环境是运行 Visual Studio 2012 Professional 的 Windows 8 Enterprise
我不能告诉你太多关于工作无线网络的信息——除了 Phone 7 设备没有任何问题。
至于我使用的家庭无线网络,那只是一个基本的 BT 宽带路由器,没有改变任何“开箱即用”设置。
显然,这两个网络的配置方式存在问题,但 Windows Phone 8 实现 UDP 消息的方式也很明显存在问题。
任何输入将不胜感激,因为这让我现在很生气。