Lucax
Lucax

Reputation: 407

C# android phone to tcp server, connection forcibly closed by remote host

I'm developing a server that is designed to accept a response sent by an android phone and then process the result, however I keep encountering a persistent issue "An existing connection was forcibly closed by the remote host"

Strangely I have a chat application (server/client) that works perfectly. The only difference between that and this instance is that the android phone connects to a wireless access point and sends the data wirelessly, whereas the chat application is wired.

My code is here:

Server:

    private static void StartServer()
    {
        TcpListener listener = new TcpListener(IPAddress.Parse(SERVER_IP), PORT_NO);
        listener.Start();
        while (true)
        {
            using (TcpClient client = listener.AcceptTcpClient())
            {
                using (NetworkStream stream = client.GetStream())
                {
                    byte[] buffer = new byte[client.ReceiveBufferSize];
                    int bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize);
                    string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                    Console.WriteLine($"Received: {dataReceived}");
                    Thread staThread = new Thread(() => PasteText(dataReceived));
                    staThread.SetApartmentState(ApartmentState.STA);
                    staThread.Start();
                }
            }
        }
    }

Client:

    private void SendToServer(string resultToSend)
    {
        using (TcpClient client = new TcpClient(SERVER_IP, PORT_NO))
        {
            using (NetworkStream stream = client.GetStream())
            {
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(resultToSend);
                stream.Write(bytesToSend, 0, bytesToSend.Length);
            }
        }
    }

There are other questions on the same subject, but my circumstances are a little different and more specialized. The code above is copied directly from a server/client I created a year ago, it was also an android application and it worked without issue. Which is why it was such a surprise that the code no longer works correctly.

It also seems to occasionally work and occasionally not. So by making a minor change (such as adding a delay after the client connects) will let the server work for a bit, but eventually it will stop working. It might not work one day, but the next day it would work perfectly.

I have wireshark but I'm not sure how to interpret the packets. You can see them here, 192.168.0.52 is the ip of the android phone and pc021 is the device the server is running on.

enter image description here

Any help would be appreciated as I've tried all the other solutions posted on here, but they don't seem to relate to my issue other than the error message being the same.

Upvotes: 0

Views: 191

Answers (1)

Lucax
Lucax

Reputation: 407

Found a potential solution, at least I have not experienced any force closes like before. The solution was to enable TLS1.2 on the TCP server by adding this line:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

So far it's been up for 3 days without issue whereas previously it would crash after 10-20 minutes of scanning.

Upvotes: 1

Related Questions