473,791 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TcpClient / NetworkStream not failing on write?

Hi

The code below demostrates an issue I'm having with with NetworkStream:

using System;
using System.Net.Sock ets;

namespace TCPCTest
{
class Class1
{
static void Main(string[] args)
{
TcpClient tcpc = new TcpClient();
tcpc.Connect("l ocalhost", 15015);

byte[] bytearray = new byte[4];
bytearray[0] = (byte)0;
bytearray[1] = (byte)1;
bytearray[2] = (byte)2;
bytearray[3] = (byte)3;

Console.WriteLi ne("Sleeping for 10s...");
// While the thread is sleeping, kill the receiving process
System.Threadin g.Thread.Sleep( 10000);

NetworkStream ns = tcpc.GetStream( );
try
{
Console.WriteLi ne("Write 1");
ns.Write(bytear ray, 0, 4);
Console.WriteLi ne("Write 1 OK");
Console.WriteLi ne("Write 2");
ns.Write(bytear ray, 0, 4);
Console.WriteLi ne("Write 2 OK");
}
catch (System.Excepti on e)
{
Console.WriteLi ne("Exception") ;
Console.WriteLi ne(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.W rite. 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

Mar 21 '06 #1
7 5188
The first write does not fail because it does not wait for the TCP frames to
be acknowledged before returning.

<da**********@u kgateway.net> wrote in message
news:11******** **************@ t31g2000cwb.goo glegroups.com.. .
Hi

The code below demostrates an issue I'm having with with NetworkStream:

using System;
using System.Net.Sock ets;

namespace TCPCTest
{
class Class1
{
static void Main(string[] args)
{
TcpClient tcpc = new TcpClient();
tcpc.Connect("l ocalhost", 15015);

byte[] bytearray = new byte[4];
bytearray[0] = (byte)0;
bytearray[1] = (byte)1;
bytearray[2] = (byte)2;
bytearray[3] = (byte)3;

Console.WriteLi ne("Sleeping for 10s...");
// While the thread is sleeping, kill the receiving process
System.Threadin g.Thread.Sleep( 10000);

NetworkStream ns = tcpc.GetStream( );
try
{
Console.WriteLi ne("Write 1");
ns.Write(bytear ray, 0, 4);
Console.WriteLi ne("Write 1 OK");
Console.WriteLi ne("Write 2");
ns.Write(bytear ray, 0, 4);
Console.WriteLi ne("Write 2 OK");
}
catch (System.Excepti on e)
{
Console.WriteLi ne("Exception") ;
Console.WriteLi ne(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.W rite. 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

Mar 21 '06 #2
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
Mar 21 '06 #3
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

Mar 21 '06 #4
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

Mar 21 '06 #5

<da**********@u kgateway.net> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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.
Mar 22 '06 #6
Hello, da**********@uk gateway.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' ( SocketOptionNam e.NoDelay ).


--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 24 '06 #7
vj
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.ne t> wrote in message
news:O8******** ******@tk2msftn gp13.phx.gbl...
Hello, da**********@uk gateway.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' ( SocketOptionNam e.NoDelay ).
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Mar 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2406
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 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...
3
4689
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 My code is as below, but I never get a response. I'm assuming my transmission is ending at the null character (the debug statement certainly only outputs up until the null character). Any ideas?
1
3359
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 that i can communicate with the servet just for one time,the first time i connect to the server,send data and receive response,but when i want to receive data for another time,there won't be any response.some body help me please,whats the problem???...
2
1590
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 is closed some time later. (As is common, as data is received it is placed in a buffer and then rolls back into another BeginRead. For the purposes of troubleshooting I've not initiated any action that would cause data to come in, so as to...
1
2426
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 exception that no bytes are read/written. Thanks, Charles.
6
7282
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 remember there are 2 streams in Java, one for read, one for write. If I just use synchronous Read() and Write() method:
3
2689
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? Client could send data once few seconds of minutes. Is there an "end" at all? In a C/S application, if server side call BeginginRead() again in EndRead() to create a endless loop to get message from client, is this a better approach than "one...
0
1370
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 TcpClient(SERVER_IP, SERVER_PORT); tcp.NoDelay = true; string data = "GET http://" + SERVER_IP + SERVER_PORT + "/getData?id=1
1
3266
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 and adding Integrated windows authentication. Now I get 401 Unauthorized errors. Here is the code that was working: // Make non-secure HTTP Client connections. private string MakeTcpClientRequest() { String _url =...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.