question

Madhu-8808 avatar image
0 Votes"
Madhu-8808 asked ·

UWP Stream socket connection issue

Hi,

We have created 2 UWP apps as a Server app and a Client app to be used in a classroom. There can be 30-40 students who has a device with Client app per student. Teacher uses Server app to teach students.
There is a feature, when teacher shares a file from Server app all the students should receive that file with their Client app. we have implemented that feature using Stream Sockets as below.

When sharing files from Server app, some devices with Client app doesn't receive the file some times. It gives a A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond error. All the clients create connections to same port with Server.

What could be the reason for this error? Is there a connection limit to connect to a socket with given IP and port using Stream Sockets?

Any ideas on this is much appreciated.

Also would like to get these clarified

  • What is the difference between BindServiceNameAsync and BindEndpointAsync? Most examples seems to use the first one. When should we use the second one?


  • If we call sender.Dispose(); in SocketListener_ConnectionReceived, will that affect the other clients trying to join the same socket?


  • In the ShareFile() function, if we close args.Socket() after sending data, can it close the socket before the client actually read the data from that side? How is it handled?

    Server App
    public async Task StartServer(string serverIpPort)
    {
    try
    {
    string[] list = serverIpPort.Split(':');
    HostName serverAddress = new HostName(list[0]);
    //Create a StreamSocketListener to start listening for TCP connections.
    StreamSocketListener socketListener = new StreamSocketListener();

                       //Hook up an event handler to call when connections are received.
                         socketListener.ConnectionReceived += SocketListener_ConnectionReceived;
           
                         //Start listening for incoming TCP connections on the specified port.
                         await socketListener.BindEndpointAsync(serverAddress, list[1]);                                              
                     }              
                     catch (Exception e)
                     {
                         AppUtils.PrintDebug("====Exception : " + e.ToString(), CLASS_NAME, "StartServer()");            
                     }
                 }
           
           
                private async void SocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
                 {
                     try
                     {
                         AppUtils.PrintDebug("Test this machine : " + Thread.CurrentThread.ManagedThreadId);
                         await Task.Run(() => ShareFile(args.Socket));
                     }
                     catch (Exception e)
                     {
                         AppUtils.PrintDebug("====Exception : " + e.ToString(), CLASS_NAME, "SocketListener_ConnectionReceived()");
                     }
                 }
        
           
        
     ** Client App**
          public async Task ServerConnect(string serverIP, string serverPort)
                 {
                     try
                     {
                         HostName serverAddress = new HostName(serverIP);
                         StreamSocket socket = new StreamSocket();
           
                         socket.Control.KeepAlive = false;            
           
                         // Save the socket, so subsequent steps can use it.
                         App.clientSocket = socket;
           
                         // Connect to the server.
                         await socket.ConnectAsync(serverAddress, serverPort, SocketProtectionLevel.PlainSocket);            
                     }
                     catch (Exception e)
                     {
                         AppUtils.PrintDebug("====Exception : " + e.ToString(), CLASS_NAME, "ServerConnect()");
                         throw e;
                     }
                 }
    

Thanks,
Madhu






uwp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I found you have posted the same issue in stackoverflow, and I will follow the case there, please pay attention.

0 Votes 0 · ·

0 Answers