我创建了一个“通用Windows 8”项目。我想要做的是将它部署在我的PC和我的WP上,并通过互联网(通过IP)连接。 我找到了以下代码:https://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931/view/SourceCode并尝试“翻译”它(在示例中,设备仅连接到自身 - “localhost”)。
以下是代码:
// those are global variables used between the functions
readString = "";
ssocket = ref new Sockets::StreamSocket();
Sockets::StreamSocketListener^ listener = ref new Sockets::StreamSocketListener();
#if WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP
create_task(listener->BindEndpointAsync(ref new HostName("W.P.I.P"), "80")).then([this](task<void> previousTask)
{
previousTask.get();
});
create_task(ssocket->ConnectAsync(ref new HostName("P.C.I.P"), "80")).then([this](task<void> previousTask) ////ERROR
{
previousTask.get();
DataReader^ reader = ref new DataReader(ssocket->InputStream);
// Start a receive loop.
ReceiveStringLoop(reader, ssocket);
});
#else
create_task(listener->BindEndpointAsync(ref new HostName("P.C.I.P"), "80")).then([](task<void> previous){previous.get(); });
create_task(ssocket->ConnectAsync(ref new HostName("W.P.I.P"), "80")).then([this](void)
{
Platform::String^ stringToSend = "asdfasdf";
DataWriter^ writer = ref new DataWriter(ssocket->OutputStream);
writer->WriteUInt32(writer->MeasureString(stringToSend));
writer->WriteString(stringToSend);
create_task(writer->StoreAsync()).wait();
});
#endif
ReceiveStringLoop函数的唯一区别在于:而不是:
NotifyUserFromAsyncThread(...)
功能,我有:
readString = reader->ReadString(actualStringLength);
我做的确切是在PC上启动应用程序~2秒后启动WP上的应用程序(它应该开始监听)。在调试时我检查了时间
BindEndpointAsync()
功能需要,它在两个设备上足够短。
错误是: “连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接主机无法响应而建立连接失败。”
它发生在
ConnectAsync()
功能。 (//// ERROR)
提前感谢您的帮助。