(c#)WinForm和uwp如何实现端口通信

时间:2018-05-31 09:40:55

标签: c# winforms sockets uwp

(c#)WinForm和uwp如何实现端口通信?端口发送的数据是否具有大小限制? throw:连接尝试失败,因为连接端在一段时间后没有正确响应,或者连接主机没有响应。

这是uwp中的代码:

const string serverPort = "38885";
            const string socketId = BgTaskConfig.TaskName;
            var sockets = SocketActivityInformation.AllSockets;
            if (!sockets.Keys.Contains(socketId))
            {
                var socket = new StreamSocketListener();
                socket.EnableTransferOwnership(_taskId, SocketActivityConnectedStandbyAction.DoNotWake);
                await socket.BindServiceNameAsync(serverPort);
                await Task.Delay(500);
                await socket.CancelIOAsync();
                socket.TransferOwnership(socketId);
                BgTaskConfig.ServerStatus = "Running";
            }

这是WinForm中的代码:

IPAddress ip = IPAddress.Parse("127.0.0.1");
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            clientSocket.Connect(new IPEndPoint(ip, 38885)); 
            Console.WriteLine("OK");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
            return;
        }

0 个答案:

没有答案