[UWP]设置Socket Broker侦听服务器 [英] [UWP]Setting up a Socket Broker listening server

查看:105
本文介绍了[UWP]设置Socket Broker侦听服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我一直在查看SocketActivityStreamSocket示例,我正在尝试修改它以便

Hello there. I have been looking at the SocketActivityStreamSocket example and am trying to modify it so that 

- 在服务器应用程序中,传递给套接字代理的套接字是一个没有任何活动连接的streamsocketlistener

- In the server app, the socket handed to socket broker is a streamsocketlistener without any active connections

- 客户端应用程序streamsocket连接到服务器

- A client app streamsocket makes connection to the server

我期望在建立连接时在服务器上调用后台任务。我发现后台任务崩溃之前它甚至命中运行方法与一个神秘的"BACKGROUNDTASKHOST.EXE"退出代码1(0x1)" 

What I expected was the background task to be invoked on the server when a connection was made. What I found was the background task crashes before it even hits the run method with a cryptic "BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1)". 

所以,你知道是否有可能将一个未连接的套接字监听器交给代理并让后台任务在连接时执行? 

So, do you know if its possible to hand off an unconnected socket listener to the broker and get the background task to execute on connection? 

谢谢

- Ahmed

- Ahmed

推荐答案

Hi hashfunction,



是的,您可以将未连接的StreamSocketListener用于套接字代理。在后台任务中,SocketActivityTriggerDetails的Reason枚举具有ConnectionAccepted成员,这意味着当您使用StreamSocketListener时接受连接。
我试图改变StreamSocketListenerServer解决方案,就像SocketActivityStreamSocket解决方案一样,它运行正常。请确保您在不同设备上使用客户端和服务器,因为对于客户端和服务器使用
相同的设备时存在环回限制。

注意 允许环回仅用于开发目的。不允许在Visual Studio外部安装Windows运行时应用程序。此外,Windows运行时应用程序只能将IP环回用作客户端网络请求的目标地址。
因此,使用DatagramSocket或StreamSocketListener监听IP环回地址的Windows运行时应用程序无法接收任何传入数据包。


https://msdn.microsoft.com/en-us/library/windows/apps/hh780593?f = 255& MSPPError = -2147217396
$


后台任务:

Hi hashfunction,

Yes, you could use the unconnected StreamSocketListener for socket broker. In background task the Reason enumeration of SocketActivityTriggerDetails has a member as ConnectionAccepted, it means a connection is accepted when you use the StreamSocketListener. I have tried to change the StreamSocketListenerServer solution just like the SocketActivityStreamSocket solution and it works fine. Please make sure that you are using the client and server on different devices since there are loopback restrictions when using the same device for client and server.
Note  Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets.
https://msdn.microsoft.com/en-us/library/windows/apps/hh780593?f=255&MSPPError=-2147217396

In background task:

var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
var socketInformation = details.SocketInformation;
switch (details.Reason)
{
    case SocketActivityTriggerReason.ConnectionAccepted:
        var listener = socketInformation.StreamSocketListener;
}

在前台app中:

        IBackgroundTaskRegistration task;
        string socketId = "SampleSocketListener";
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        { 
            foreach (var current in BackgroundTaskRegistration.AllTasks)
            {
                if (current.Value.Name == "SocketActivityListenerBackgroundTask")
                {
                    task = current.Value;
                    break;
                }
            }
            if (task == null)
            {
                var socketTaskBuilder = new BackgroundTaskBuilder();
                socketTaskBuilder.Name = "SocketActivityListenerBackgroundTask";
                socketTaskBuilder.TaskEntryPoint = "RuntimeComponent1.Class1";    // create RuntimeComponent1 with Class1 as entry and change the Package.appxmanifest file
                var trigger = new SocketActivityTrigger();
                socketTaskBuilder.SetTrigger(trigger);
                task = socketTaskBuilder.Register();
            }
            SocketActivityInformation socketInformation;
            if (!SocketActivityInformation.AllSockets.TryGetValue(socketId, out socketInformation))
            {
                tcpListener = new StreamSocketListener();
                tcpListener.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                tcpListener.ConnectionReceived += OnConnected;
                await tcpListener.BindServiceNameAsync(port);
                await tcpListener.CancelIOAsync();
                tcpListener.TransferOwnership(socketId);
            }
        }


最好的问候,

David

Best Regards,
David


这篇关于[UWP]设置Socket Broker侦听服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆