NetworkStream Write Not Failing

  • Thread starter Thread starter Srinivas R. Loka
  • Start date Start date
S

Srinivas R. Loka

I have a TCP server to which a number of mobile devices connect. If a device
disconnects(mostly no clean close as they usually lose cell coverage or
shutdown), and the server then tries to send data using the
Networkstream.Write method, its NOT throwing an exception. I am using the
TCPClient class.
I am able to reproduce this by shutting down my test device(turning off
power) and then sending some data. The Write just finishes as if the data
was sent.
Is there some timeout that has to occur before the Write fails ?

Thanks

Srinivas
 
Are you using a NetworkStream to write? If so, just check the CanWrite property

TcpClient tcp = new TcpClient("www.google.com", 80)
NetworkStream ns = new NetworkStream(tcp, true)
if (ns.CanWrite

ns.Write(myBytes, 0, 256)
 
Thanks for the reply.
Yes I am using the NetworkStream. Even the CanWrite returns true. Here is
part of the code that writes

if (Cmd != "")
{
try
{
byte[] OTA = Encoding.ASCII.GetBytes(Cmd);
try
{
if(_NetworkStream.DataAvailable)
{
//JUst to check for connection
}
}
catch (SocketException) //remote closed
{
this.CloseSocket();
return;
}
if(_NetworkStream.CanWrite)
{
_NetworkStream.Write(OTA,0,(int)OTA.Length);
}
}
catch(Exception eOTA)
{
Controller.EMailErr("OTA Send Error",eOTA.Message.ToString());
}
}


The mobile unit is a third party device and I just power it down to simulate
a lost TCP connection.
I even used DataAvailable as it is supposed to throw an exception if remote
disconnects. Even after I shutdown the remote device, the CanWrite returns
true and the Write does not thrown an exception.
The mobile unit connects using a wireless cellular network(ATT/cingular) -
would that have something to do with it ?

Thanks again
 
Back
Top