TcpClient - An existing connection was forcibly closed by the remote host

rodit

The Info

I have been developing a web http server in c# and decided to add a remote console feature. The console can be used from any location and uses a TcpListener (web server) and a TcpClient (remote console) to send commands and functions through.

The Code

This is what my server looks like:

TcpListener consoleListener = new TcpListener(consolePort);
consoleListener.Start();
byte[] bytes = new Byte[256];
string data = null;
while (true)
{
    TcpClient client = consoleListener.AcceptTcpClient();
    data = null;
    byte[] msg = { 0 };
    int i;
    while ((i = client.GetStream().Read(bytes, 0, bytes.Length)) != 0)
    {
        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        if (data == "shutdown")
        {
            //Server shutdown logic.
        }
        //Other commands here...
        else
        {
            msg = Encoding.ASCII.GetBytes("Invalid command. Type 'help' or '?' to get a list of commands.");
        }
        client.GetStream().Write(msg, 0, msg.Length); //sends return message to console
    }
    client.Close(); //closes connection between client and server after EVERY command. Connection is reopened when a new command is sent.
}

Note - The server is run on a separate thread to both the webserver and main console application thread.

This is my client:

public static string Communicate(string text)
{
    try
    {
        TcpClient client = new TcpClient(ip, port); //initializes tcpclient (ip and port are correct)

        byte[] data = System.Text.Encoding.ASCII.GetBytes(text); //converts text to bytes for stream writing

        NetworkStream stream = client.GetStream();

        stream.Write(data, 0, data.Length);

        Console.WriteLine("Sent data: " + text);

        data = new Byte[256];

        string responseData = String.Empty; //initializes responsData string

        Int32 bytes = stream.Read(data, 0, data.Length);
        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
        client.Close();
        return responseData; //returns what server writes
    }
    catch (Exception ex)
    {
        return "An error occured\n" + ex.ToString();
    }
}

The Problem

I can send one command to the server with a successful return. However, when I try and send another command, the server throws the error below:

System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at ---.Server.ConsoleListener() in X:\Users\---\Documents\Visual Studio 2013\Projects\---\---\Program.cs:line x

I know it is not firewall or administrator elevation problems as I can send one command through successfully. It is only on the second command sent that it throws this error.

Here is a screenshot describing the problem: The remote console and server communication and error reporting.

EDIT: By doing a little research, I found that the problem is most likely a result of a small error in my for loop. However, I do not know any way of fixing this as I do not know the exact problem :). Please help me identify it so I can fix it.

Thanks again

Stilgar

It seems that your client closes the connection after one message.

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
client.Close();
return responseData; //returns what server writes

If you want to persist the connection you should have a loop on the client similar to the one you have on the server. If you want to establish a new connection every time you should close the stream gracefully on the server and not have a loop like this. You will still need to loop in case the message is longer or you need to specify max length for the command.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TotoiseHG: An existing connection was forcibly closed by the remote host

From Dev

Hmm An existing connection was forcibly closed by the remote host

From Dev

Existing connection was forcibly closed by the remote host

From Dev

An existing connection was forcibly closed by the remote host [BigChainDB]

From Dev

Error: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

Error: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

Connection was forcibly closed by the remote host

From Java

C# HttpClient An existing connection was forcibly closed by the remote host

From Dev

SVN error running context: An existing connection was forcibly closed by the remote host

From Dev

.NET UDPClient: Error: An existing connection was forcibly closed by the remote host

From Dev

android I/O Error: An existing connection was forcibly closed by the remote host

From Dev

"An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

From Dev

Cannot Checkout, "An existing connection was forcibly closed by the remote host"

From Dev

How to return IEnumerable in WCF ? An existing connection was forcibly closed by the remote host

From Dev

"An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

From Dev

"An existing connection was forcibly closed by the remote host" when listening for incoming data

From Dev

.NET UDPClient: Error: An existing connection was forcibly closed by the remote host

From Dev

An existing connection was forcibly closed by the remote host in authorize.net

From Dev

Connection was forcibly closed by the remote host on receive

From Dev

"Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."

From Dev

Can't run app on my phone for testing: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host in C#

From Dev

Windows azure - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

From Dev

urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

From Dev

Windows azure - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

From Dev

Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

From Dev

Python 3 , Windows 7 :ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

From Dev

How to catch this Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

From Dev

How to properly close a client proxy (An existing connection was forcibly closed by the remote host)?

Related Related

  1. 1

    TotoiseHG: An existing connection was forcibly closed by the remote host

  2. 2

    Hmm An existing connection was forcibly closed by the remote host

  3. 3

    Existing connection was forcibly closed by the remote host

  4. 4

    An existing connection was forcibly closed by the remote host [BigChainDB]

  5. 5

    Error: Adb connection Error:An existing connection was forcibly closed by the remote host

  6. 6

    Error: Adb connection Error:An existing connection was forcibly closed by the remote host

  7. 7

    Connection was forcibly closed by the remote host

  8. 8

    C# HttpClient An existing connection was forcibly closed by the remote host

  9. 9

    SVN error running context: An existing connection was forcibly closed by the remote host

  10. 10

    .NET UDPClient: Error: An existing connection was forcibly closed by the remote host

  11. 11

    android I/O Error: An existing connection was forcibly closed by the remote host

  12. 12

    "An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

  13. 13

    Cannot Checkout, "An existing connection was forcibly closed by the remote host"

  14. 14

    How to return IEnumerable in WCF ? An existing connection was forcibly closed by the remote host

  15. 15

    "An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

  16. 16

    "An existing connection was forcibly closed by the remote host" when listening for incoming data

  17. 17

    .NET UDPClient: Error: An existing connection was forcibly closed by the remote host

  18. 18

    An existing connection was forcibly closed by the remote host in authorize.net

  19. 19

    Connection was forcibly closed by the remote host on receive

  20. 20

    "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."

  21. 21

    Can't run app on my phone for testing: Adb connection Error:An existing connection was forcibly closed by the remote host

  22. 22

    Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host in C#

  23. 23

    Windows azure - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

  24. 24

    urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

  25. 25

    Windows azure - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

  26. 26

    Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

  27. 27

    Python 3 , Windows 7 :ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

  28. 28

    How to catch this Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

  29. 29

    How to properly close a client proxy (An existing connection was forcibly closed by the remote host)?

HotTag

Archive