最近我开发了一个运行良好的c#metro应用程序,现在我想在c#中开发相同的应用程序。由于这个website库windows.networking.proximity仍在工作,所以我已经做了很少的东西来启用地铁应用程序库,windows.security.cryptography也显然windows.networking.sockets没有工作。 我这部分代码的目标只是接收通过wifi从智能手机发送的数据:
namespace OpenItForMeDesktop{
class Server
{
private StreamSocketListener serverListener;
public static String port = "3011";
public static String adressIP = "192.168.173.1";
private HostName hostName;
//Initialize the server
public Server()
{
serverListener = new StreamSocketListener();
hostName = new HostName(adressIP);
listen();
}
//Create the listener which is waiting for connection
private async void listen()
{
serverListener.ConnectionReceived += OnConnection;
try
{
//await serverListener.BindEndpointAsync(hostName, port);
await serverListener.BindServiceNameAsync(port);
MainWindow.Current.UpdateLog("Listening for Connection(s)");
}
catch (Exception exception)
{
MainWindow.Current.UpdateLog("Exception throw in Listen : " + exception);
}
}
//When a connection appears, this function his called
private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
MainWindow.Current.UpdateLog("A message has been received...");
if (MainWindow.Current.loadingPage)
{
MainWindow.Current.UpdateLog("wait please");
}
else
{
DataReader reader = new DataReader(args.Socket.InputStream);
try
{
while (true)
{
reader.InputStreamOptions = InputStreamOptions.Partial;
// Read first 4 bytes (length of the subsequent string).
uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
if (sizeFieldCount != sizeof(uint))
{
return;
}
// Read the string.
uint stringLength = reader.ReadUInt32();
uint actualStringLength = await reader.LoadAsync(stringLength);
if (stringLength != actualStringLength)
{
return;
}
String message = reader.ReadString(actualStringLength);
MainWindow.Current.receiveMessage(message);
}
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
MainWindow.Current.UpdateLog("Read stream failed with error: " + exception.Message);
}
}
}
}
}
` 当我构建这个代码时,没有抛出任何错误,当我使用wireshark查看我的smarpthone发送的数据包时,我只收到带有[SYN]标志的数据包,而不是我在使用时收到的3个第一个数据包我的地铁应用程序[SYN] / [SYN,ACK] / [ACK]用于握手。 有人知道为什么会这样吗?
答案 0 :(得分:1)
我找到了答案,这是非常愚蠢的,Windows的防火墙阻止了我的应用程序。
答案 1 :(得分:0)
你添加了
吗?<Capability Name="privateNetworkClientServer" />
到Package.appxmanifest?