2

When using tcpclient for writing to socket as in example below

Server:

tcpListener.Start(PendingClientCount);
tcpListener.BeginAcceptTcpClient(OnAcceptCompleted, null);
////and more code

Client:

var client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Loopback, 50000));
var clientStream = client.GetStream();
if (clientStream.CanWrite)
{
  clientStream.Write(preparedPackage, 0, preparedPackage.Length);
}
clientStream.Close();
client.Close();

It turns out then after closing of server by calling tcpListener.Close() and Dispose() I can write bytes to network stream, and they go to "nowhere". After passing certain time interval (approx. 2 minutes) attempt ti network stream fails (at last).

Is there any possibility to detect that nobody listens to socket on opposite side when trying to write to it?

Gopher
  • 907
  • 6
  • 18
  • 2
    Possible duplicate of [Instantly detect client disconnection from server socket](http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket) – zimdanen May 08 '12 at 13:27
  • Are you calling `Close` on the accepted socket at the server end? – Nick May 08 '12 at 13:28
  • 2Nick: yes, Close() was called – Gopher May 08 '12 at 13:29
  • Not just on the `TcpListener`? I mean the socket which you will have been provided with when you accepted the connection. – Nick May 08 '12 at 13:30
  • Lowest level is TcpClient (result of calling AcceptClient), and it was closed along with TcpListener – Gopher May 08 '12 at 13:35
  • I've tried this http://stackoverflow.com/a/722265/187130 and it works – Gopher May 08 '12 at 13:43

0 Answers0