Technology (Read Only) > Programming

C# Exception: "Unable to write data to the transport connection:"

(1/1)

Rocket50:
Full Exception:

Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.


static void Connection(string add, int port, string input)
    {   
             if (input == "connect")
             {
             
                   
       
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(input);     
                TcpClient client = new TcpClient();
                client.Connect(add,port); //Initial Connection works
                if (client.Connected == true)
                {
                    Console.WriteLine("\nConnected to {0} via port: {1}!", add, port);
               
                    NetworkStream network = client.GetStream();
                    network.Write(data, 0, data.Length);

                connecttest:

                    Console.Write("\nEnter a message: ");
                    message = Console.ReadLine();
               
                    if (message != "")
                    {
                        data = System.Text.Encoding.ASCII.GetBytes(message);
                        network.Write(data, 0, data.Length); //Error happens here

                    }
                    if (client.Connected == false)
                    {
                        Console.WriteLine("Client Forcefully Disconnected :<");
                        Main();
                    }
                    else
                    {
                        Console.WriteLine("\nSent!"); //Successful second sending of data, but third causes error
                    }
                    if (message == "disconnect")
                    {
                        client.Close();
                        if (client.Connected == false)
                        {
                            Console.WriteLine("\nClient Disconnected\n");

                        }
                        Main();
                    }
                    else
                    {
                        goto connecttest;
                    }
                   

Stuff I tried:

Turning off router firewalls
Turning off Anti Virus
Turning off windows Firewall
Tried on another computer

any help would be really appreciated... Except google. I've tried that too...

Foofoo.

coolzeldad:
Just looked at this, try verifying when you lose the connection - or at least a close approximation.

Monitor when the value of client.Connected changes... after you write data to the stream and when you jump to your connecttest label (which i do not recommend the use of), etc.

Rocket50:
I created a simpler version of this solely to test it, and what it does is it establishes, and sends "a" across the stream immediately. Then I test the connection as of the instance "a" was sent, (design choice by Microsoft to only allow .Connected's value to change as of the last instance something was sent), and then I send a user input string, which is sent. However the third one does not.

Foofoojack:
try goog- *User was banned for this post*




« Last Edit: September 15, 2012, 10:30:56 AM by yamada »

Navigation

[0] Message Index

Go to full version