Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the code but am getting the error Unable to read data from the transport connection. An existing connection was forcibly closed by the remote host.

IPAddress ipAdress = IPAddress.Parse("x.x.x.x");
            TcpListener tcplistener = new TcpListener(ipAdress, 1000);
            tcplistener.Start();
            Socket socketforClient = tcplistener.AcceptSocket();

  if (socketforClient.Connected)
  {
    Console.WriteLine("Client connected");    
  }
  NetworkStream networkStream = new NetworkStream(socketforClient);
    }            
  System.IO.StreamWriter streamWriter = new   System.IO.StreamWriter(networkStream);
  System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
  string theString = "Sending";
  streamWriter.WriteLine(theString);
  Console.WriteLine(theString);
   streamWriter.Flush();
   theString = streamReader.ReadLine(); // getting the error
    Console.WriteLine(theString);
   streamReader.Close();
    networkStream.Close();
                streamWriter.Close();
            }
            socketforClient.Close();
            Console.WriteLine("Exiting...");
}
Posted
Updated 3-Apr-15 23:41pm
v3
Comments
Afzaal Ahmad Zeeshan 4-Apr-15 5:04am    
Any exception?
Srikanth59 4-Apr-15 5:08am    
the error Unable to read data from the transport connection. An existing connection was forcibly closed by the remote host.
Afzaal Ahmad Zeeshan 4-Apr-15 5:12am    
Connection was closed by remote host. So the client is either terminating the connection, or you've set the time limit for the connection. Check your configuration settings.
BacchusBeale 4-Apr-15 5:48am    
You should be reading after accepting the connection and replying depending on the message/command that was sent to you. Also you only read one line, read to end of stream.
Florian Braun 10-Apr-15 3:09am    
What code runs on the client?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900