D
david.topham
Hi
The code below demostrates an issue I'm having with with NetworkStream:
using System;
using System.Net.Sockets;
namespace TCPCTest
{
class Class1
{
static void Main(string[] args)
{
TcpClient tcpc = new TcpClient();
tcpc.Connect("localhost", 15015);
byte[] bytearray = new byte[4];
bytearray[0] = (byte)0;
bytearray[1] = (byte)1;
bytearray[2] = (byte)2;
bytearray[3] = (byte)3;
Console.WriteLine("Sleeping for 10s...");
// While the thread is sleeping, kill the receiving process
System.Threading.Thread.Sleep(10000);
NetworkStream ns = tcpc.GetStream();
try
{
Console.WriteLine("Write 1");
ns.Write(bytearray, 0, 4);
Console.WriteLine("Write 1 OK");
Console.WriteLine("Write 2");
ns.Write(bytearray, 0, 4);
Console.WriteLine("Write 2 OK");
}
catch (System.Exception e)
{
Console.WriteLine("Exception");
Console.WriteLine(e);
}
ns.Close();
tcpc.Close();
}
}
}
This isn't production code, but it demonstrates the problem I'm having
with in my app. The code connects to a remote process listening for
connections on local port 15015, although it could be any port on any
address. It then sleeps for 10s - use this time to kill the remote app,
pull out the network cable, or whatever it takes to break the
connection.
The code then makes two calls to NetworkStream.Write. An exception
should be thrown because the write is not possible. But it's always the
second Write that throws the IOException, never the first. As far as
you can tell, the first write is successful. I've tried setting NoDelay
on the TCPClient to no effect. I've also tried changing the SendBuffer
size. Adding calls to Flush makes no difference, as Flush on
NetworkStream does nothing (see the docs).
Any idea why the first write doesn't fail?
Thanks
David Topham
The code below demostrates an issue I'm having with with NetworkStream:
using System;
using System.Net.Sockets;
namespace TCPCTest
{
class Class1
{
static void Main(string[] args)
{
TcpClient tcpc = new TcpClient();
tcpc.Connect("localhost", 15015);
byte[] bytearray = new byte[4];
bytearray[0] = (byte)0;
bytearray[1] = (byte)1;
bytearray[2] = (byte)2;
bytearray[3] = (byte)3;
Console.WriteLine("Sleeping for 10s...");
// While the thread is sleeping, kill the receiving process
System.Threading.Thread.Sleep(10000);
NetworkStream ns = tcpc.GetStream();
try
{
Console.WriteLine("Write 1");
ns.Write(bytearray, 0, 4);
Console.WriteLine("Write 1 OK");
Console.WriteLine("Write 2");
ns.Write(bytearray, 0, 4);
Console.WriteLine("Write 2 OK");
}
catch (System.Exception e)
{
Console.WriteLine("Exception");
Console.WriteLine(e);
}
ns.Close();
tcpc.Close();
}
}
}
This isn't production code, but it demonstrates the problem I'm having
with in my app. The code connects to a remote process listening for
connections on local port 15015, although it could be any port on any
address. It then sleeps for 10s - use this time to kill the remote app,
pull out the network cable, or whatever it takes to break the
connection.
The code then makes two calls to NetworkStream.Write. An exception
should be thrown because the write is not possible. But it's always the
second Write that throws the IOException, never the first. As far as
you can tell, the first write is successful. I've tried setting NoDelay
on the TCPClient to no effect. I've also tried changing the SendBuffer
size. Adding calls to Flush makes no difference, as Flush on
NetworkStream does nothing (see the docs).
Any idea why the first write doesn't fail?
Thanks
David Topham