这是我几天来一直在试图解决的问题。我们有一个运行在 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实现了一个简单的通信测试,再次看到成功的初始化,但没有设备间的通信。
以下是使用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 - Nokia Lumia 900 (*2),Nokia Lumia 800 (*3);Windows Phone 8 - Nokia Lumia 920 (*1),Nokia Limia 820 (*2)。这些设备都运行最新的操作系统,并处于开发者模式下。开发环境是运行Visual Studio 2012 Professional的Windows 8 Enterprise。我无法告诉你有关工作无线网络的太多信息-除了Phone 7设备没有问题之外。至于我使用的家庭无线网络,那只是一个基本的BT宽带路由器,没有改变任何“开箱即用”的设置。很明显,两个网络的配置方式存在问题,但Windows Phone 8实现UDP消息的方式也存在问题。如果有任何建议将不胜感激,因为这让我非常烦恼。
var stream = await socket.GetOutputStreamAsync(new Windows.Networking.HostName(IPAddress.Broadcast.ToString()), GROUP_PORT.ToString());
- Adam Stewart