1

(c#) How does WinForm and uwp realize port communication? Does the data sent by port have the size restriction? throw:The connection attempt failed because the connection side did not respond properly after a period of time or the host of the connection did not respond.

This is the code in 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";
            }

This is the code in 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;
        }
Values
  • 227
  • 2
  • 10
  • The size of data is handled by the ethernet driver on the PC and should not affect your application. Both WinnForm and uwp use the same underlying TCPIP interface in the Net library. The only real difference in the two code posted is the socket being used. There is a host file in windows that defines the variable localhost. On some PCs it is set to the loopback IP 127.0.0.1. In the Net library there is an IP.Any which should be used for the socket( not 127.0.0.1). The loopback will not always work depending on the host file in windows. – jdweng May 31 '18 at 10:05
  • UWP apps run in a security sandbox. Many consequences, one is that process interop is completely *verboten*. Sockets are not a workaround for that. There is an option to enable local-loopback for testing, but not for the final app. – Hans Passant May 31 '18 at 10:49
  • Is there any other way to do it, thank you! – Values Jun 01 '18 at 06:42
  • @Values It seems I have replied your issue here https://stackoverflow.com/questions/50638912/how-winform-and-uwp-communicate-in-two-directions, do you still have trouble with this issue :) ? – Breeze Liu - MSFT Jun 05 '18 at 10:33

0 Answers0