TCP socket always returns TRUE on connected statement
Hello people i am currently writting myself an app where i need a tcp/ip connection Currently all is working except for one thing
both the socket.connected, StreamReader.canread and StreamWriter.canwrite always return true even if my other app is totaly turned off and disconnected.......
ofcourse this is rather odd and i am wondering if i did something wrong OR that there is a fix to all this :S
Code:
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
ConnectionListener = new TcpListener(localAddr,port);
ConnectionListener.Start();
ConnectionClientSocket = ConnectionListener.AcceptSocket();
if(ConnectionClientSocket.Connected)
{
ConnectionListener.Stop();
ConnectionStream = new NetworkStream(ConnectionClientSocket);
ConnectionReader = new StreamReader(ConnectionStream);
ConnectionWriter = new StreamWriter(ConnectionStream);
//RAISE
Connected();
//END RAISE
while(ConnectionStream.CanWrite && ConnectionStream.CanRead && ForceDisconnect == false)
{
readSocket();
ConnectionWriter.Flush();
System.Windows.Forms.Application.DoEvents();
}
}
doClose();
this is just the "server" side part of the socket readSocket checks for packages from the client party and doClose() makes sure all controlls are closed and set to null again. But the code never even reaches it.
(also the while loop contained ConnectionClientSocket.connected etc but this is the current thing i am trying)
edit:
If there is someone with a Fully 100% working socket for C# which doesn't need any non .net controls i am happy with that too :P
Re: TCP socket always returns TRUE on connected statement
Ok well i have figured it out..... just found something rather usefull
not sure what it all "does" but i get the idea
Code:
if(ConnectionClientSocket.Poll(10,System.Net.Sockets.SelectMode.SelectRead))
{
if(ConnectionClientSocket.Available == 0)
{
ForceDisconnect = true;
break;
}
}
first it polls... if that works i check if something is available if it's 0 then it's not connected which works like a charm :D