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 7 5144
The first write does not fail because it does not wait for the TCP frames to
be acknowledged before returning.
<da**********@ukgateway.net> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com... 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
Hi, Any idea why the first write doesn't fail?
Probably cause the TCP stack has no idea yet that the remote connection is
dead, it just sent the first package and is waiting for the ACK, maybe if
your buffer is small and you send a big chunk of data in your first line you
get the error right there.
BTW, I don't see what is the big issue if you are getting the exception
anyway
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I see your point, although it's not much help if I only want to make
one call to Write. I only made two in the code to demonstrate the
issue.
BTW, I tried setting the SendBuffer size to 4 bytes, and sending though
a 1000 byte buffer on the Write calls - the first call still did not
fail.
David
I see your point, although it's not much help if I only want to make
one call to Write. I only made two in the code to demonstrate the
issue.
BTW, I tried setting the SendBuffer size to 4 bytes, and sending though
a 1000 byte buffer on the Write calls - the first call still did not
fail.
David
<da**********@ukgateway.net> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com... I see your point, although it's not much help if I only want to make one call to Write. I only made two in the code to demonstrate the issue.
You would get the error when you closed the socket.
If you really want positive acknowledgement that some data has been received
then you have to do it yourself.
BTW, I tried setting the SendBuffer size to 4 bytes, and sending though a 1000 byte buffer on the Write calls - the first call still did not fail.
It probably ignored you. Apart from anything else the minimum IP datagram
size that must be supported by any system is 576 bytes so I see no way that
it wouold accept less than this (minus header size). Try checking to see
what size it actually used.
Hello, da**********@ukgateway.net!
dt> BTW, I tried setting the SendBuffer size to 4 bytes, and sending though
dt> a 1000 byte buffer on the Write calls - the first call still did not
dt> fail.
This can be due to 'nagling' ( SocketOptionName.NoDelay ).
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
if you are looking for in Version 1.1, you have to do through Native API and
reflection.. Check www.pinovke.net, they have samples of what calls to use..
VJ
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:O8**************@tk2msftngp13.phx.gbl... Hello, da**********@ukgateway.net!
dt> BTW, I tried setting the SendBuffer size to 4 bytes, and sending though dt> a 1000 byte buffer on the Write calls - the first call still did not dt> fail.
This can be due to 'nagling' ( SocketOptionName.NoDelay ).
-- Regards, Vadym Stetsyak www: http://vadmyst.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Srinivas R. Loka |
last post by:
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...
|
by: Danny Tuppeny |
last post by:
Hi all,
I'm trying to send a null character as a string delimiter through a
TcpClient (code below). It's to connect to this poker bot room:
http://games.cs.ualberta.ca/webgames/poker/bots.html...
|
by: hamid_2020 |
last post by:
I wrote a class to connect to a server using tcpclient.
I need to connect to the server and the connection must be open.Then i need to send request to the server again and again.But the problem is...
|
by: TDC |
last post by:
I'm working on tying in a library that uses a
TcpClient/NetworkStream/BeginRead. When that lib object is started, it
connects and immediately does a BeginRead that stays pending until the
socket...
|
by: Charles |
last post by:
Hi,
Is there a way to find out how many bytes NetworkStream Read/Write actually
read/wrote when there is an exception (i.e. socket read/write timeout). Or,
can I assume that if there is an...
|
by: Ryan Liu |
last post by:
Hi, I have some basic question about NetworkStream, can someone explain to
me? Thanks a lot in advance!
TcpClient has a GetStream() method return a NetworkStream for read and
write.
I...
|
by: Ryan Liu |
last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte
of data can be read?
In a Client/Server application, what does it mean at the end of stream/no
more data available? ...
|
by: sternr |
last post by:
Hey,
I'm using a TcpClient to create HTTP requests to my web-server (I know
of HttpWebRequest, it is mandatory for me to use TcpClient.).
Here's my code:
TcpClient tcp = new...
|
by: mledbetter |
last post by:
I've written a simple web stress tester application using TCPClient.
My code was working fine against our web server until our server admin changed IIS security setting by removing anonymous access...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |