0

I'm running a server on a linux kernel, which starts listening to a port and then crashes (this is not subject here). When I then try to establish a connection from some client using the following code, it looks as if everything is working: The TcpClient gets connected immediately and the send operation succeeds. However, I obviously will never get a response.

TcpClient tcpClient = new TcpClient();
tcpClient.Connect(new IPEndPoint(new IPAddress(new byte[] { 192,168,0,1 }), 123));
if (tcpClient.Connected)
{
    Console.WriteLine("Connected");
    client.NoDelay = true;
    NetworkStream stream = tcpClient.GetStream();
    stream.Write(new byte[] { 0 }, 0, 1);
    stream.Flush();
    Console.WriteLine("Sent");
}

How can I determine if the connection is actually open? When the client now tries to do send a command and waits for the response, it will block until the timeout.

The actual code is part of a wrapper around TcpClient which should handle the connection, send messages and receive the responses. It has no idea about the underlying protocol (so send anything and wait for a response seems not useful...)

LionAM
  • 1,271
  • 10
  • 28
  • 1
    "the send operation succeeds" - you're not actually sending any data, and you haven't disabled send delays. I *suspect* that if you did both of those things, the send would fail. – Jon Skeet Jan 24 '23 at 15:32
  • I have updated the application to send a single byte and to set NoDelay - the result is still the same. – LionAM Jan 24 '23 at 15:44
  • I wonder if this is a duplicate of [.Net Socket doesn't respond to remote disconnect?](https://stackoverflow.com/questions/9683074/net-socket-doesnt-respond-to-remote-disconnect) – Wyck Jan 24 '23 at 15:52
  • @Wyck I don't think so because in the question, the client was actually connected. When manually closing it, it won't reconnect and IsConnected will be false. However, in my case, the IsConnected property will be true again. – LionAM Jan 24 '23 at 16:04
  • I assume you got actually connected too, and then the server crashed after that, am I misunderstanding? – Wyck Jan 24 '23 at 16:06
  • @Wyck The server already crashed before I try to connect... It seems to me that there is something still listening on the linux side, but checking with the "ps" command, the server is already gone. – LionAM Jan 24 '23 at 16:09

0 Answers0