0

I recently started writing a program for sending a file from one system to another using the TCPClient protocol and .Net.

The program has a client method and a server method. Client for sending the data and server to receive and build the file at the other end.

When I initially wrote the program, I did so with Winforms and everything was working perfectly. Since then, I tried to move everything over to UWP but I've had nothing but issues since doing so.

I've managed to fix most of the issues but I am having an issue I can't seem to get past. It appears as though the program is not accepting any TCP connections.

If I send from the Winforms version to the UWP version it fails, but doing it reversely (sending from UWP to the Winforms version) everything works. This also means that I am unable to send from the UWP to the UWP version.

Any advice would be brilliant, I've attached the method for the server side and the client side.

Thanks!

private void btnAcceptConn_Tapped(object sender, TappedRoutedEventArgs e)
{
     try
     {

         string hostName = Dns.GetHostName();

         //gets the IP and port number from text boxes
         Int32 server_port = Int32.Parse(txtboxPortNumber.Text);
         IPAddress localAdd = IPAddress.Parse(txtboxServerIP.Text);

         //listen on the specific IP and port no
         TcpListener listener = new TcpListener(localAdd, server_port);


         Byte[] bytes = new Byte[256];

         byte[] fullarrays = new byte[0];

         listen = true;

         while (listen == true)
         {

             //write to log
             string LogMess = "Listening...";
             WriteToLog(LogMess);

             listener.Start();
             //incoming client connected
             TcpClient client = listener.AcceptTcpClient();

             string LogMessConn = "Connected!";
             // WriteToLog(LogMessConn);

             //gets incoming data through network stream
             NetworkStream nwStream = client.GetStream();


             int off = 0;
             Thread.Sleep(200);
             //loop to receive all the data sent by the client
             while (nwStream.DataAvailable)
             {
                 nwStream.Read(bytes, 0, bytes.Length);

                 Array.Resize(ref fullarrays, fullarrays.Length + bytes.Length);

                 Buffer.BlockCopy(bytes, 0, fullarrays, off, bytes.Length);
                 off = +fullarrays.Length;
             }

             CreateFile(fullarrays);

             string LossMessComplete = "Complete Transfer";
             WriteToLog(LossMessComplete);

             client.Close();
             listener.Stop();
             listen = false;
         }
     }
     catch (Exception)
     {
         /*lblError.Visible = true;
         lblErrorMSG.Visible = true;
         lblErrorMSG.Text = "Please check enterted details";*/
     }
}
private void btnSend_Data_Tapped(object sender, TappedRoutedEventArgs e)
{
     Int32 server_port = Int32.Parse(txtboxPortNumber.Text);

     string ServerIP = txtboxServerIP.Text;



     //puts the file into a bytes array. gets the filetosendpath from the file select button 

     Byte[] textToSend = filetobytes(filetosendpath);

     //creates a TCP client object at the IP and port number
     TcpClient client = new TcpClient(ServerIP, server_port);
     client.SendTimeout = 1600000;
     client.ReceiveTimeout = 1600000;
     NetworkStream nwStream = client.GetStream();



     //byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
     //var bytesToSend = textToSend;

     //send the text
     string LogMess = "Sending: " + textToSend;
     WriteToLog(LogMess);
     load_activitylog();
     nwStream.Write(textToSend, 0, textToSend.Length);
}
LopDev
  • 823
  • 10
  • 26
  • Does this answer your question? [UWP app cannot receive, only send, using sockets](https://stackoverflow.com/questions/50923574/uwp-app-cannot-receive-only-send-using-sockets) – VuVirt Feb 24 '20 at 10:21
  • For the listener use for the IP address : IPAddress localAdd = IPAddress.Any; When a machine has more than on IP address using the IP will get only data from one interface. IP.Any will get from any interface. – jdweng Feb 24 '20 at 11:58
  • @VuVirt I've had a look at that and I don't think it is the same issue. When I've looked a little deeper my program throws an exception on the client side with the error: : '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 192.168.0.146:443' – obiwanconobi Feb 24 '20 at 12:26
  • @jdweng i've given this a try but it didn't seem to make any difference unfortunately! thank you for the advice though, good to know for the future! – obiwanconobi Feb 24 '20 at 12:27
  • Port 443 is using TLS/SSL secure mode. So you must have htts (not http). So you are probably getting a response of 401 instead of 200 OK. – jdweng Feb 24 '20 at 12:59
  • @jdweng i've tried with different ports, even port 80 and it gives the same error! – obiwanconobi Feb 24 '20 at 13:57
  • You have to use what the server expects.. Some server will work with either http or https, some will only work with one. Are you working with same server in UWP and Forms? Best way of debugging is to use a sniffer like wireshark or fiddler and compare the first request in working forms and non working UWP. Then make the headers in UWP request look like the headers in the form. The will work if the server is the same. All bets are off if the server is different. Check the response status to see if you are getting 200 OK or an error response. – jdweng Feb 24 '20 at 14:14
  • @jdweng i'm confused about this because basically the same code worked when it was using Winforms, now it's UWP something is stopping the connection. – obiwanconobi Feb 24 '20 at 14:46
  • It is probably the properties you are using for UWP. What code did you change? The sniffer will also show TCP Network layer and you can compare the TCP message between forms and UWP. – jdweng Feb 24 '20 at 14:52

1 Answers1

0

I've got this to work now. I'm pretty sure it was to do with the "Capabilities" within the Appxmanifest.

I enabled "Private Networks" and "Internet (client and Server)" and this seemed to get it working!

Thanks for the help