473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timeout on Stream Reads

I am implementing a protocol which transmits messages. The messages are
most naturally transferred using the a Stream so the protocol can
communicate over serial-ports, network links, ....

If the other party never answers I must timeout -- not block, and this
gives me a problem, since the Stream concept in .NET does not reflect
timeouts. If a timeout occurs I will still need to use the same Stream
for more messages, for example re-sends.

For implementing timeouts i have explored some solutions:

- Using async IO: this allows me to use WaitHandle.Wait One with a
timeout. But when A timeout occurs cancelling the pending async-io
operations (and the reulting synchronization ) is a problem.

- Using Socket.Select/Socket.Poll: Far from all Streams are based on
Socket, and I can't retrieve the Socket anyway since it's a protected
property in NetworkStream.

I've been googling a bit on stuff like .NET, C#, timeout, stream, read,
but it doesn't turn up much usefull info.

What could I do here? Basically i just wish to have an option to
wait-with-timeout ("select" or "poll" in unix/sockets terminology) for a
guarantee that Read will not block indefinatly if invoked.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #1
3 6464
Helge Jensen <he**********@s log.dk> wrote:
I am implementing a protocol which transmits messages. The messages are
most naturally transferred using the a Stream so the protocol can
communicate over serial-ports, network links, ....

If the other party never answers I must timeout -- not block, and this
gives me a problem, since the Stream concept in .NET does not reflect
timeouts. If a timeout occurs I will still need to use the same Stream
for more messages, for example re-sends.

For implementing timeouts i have explored some solutions:

- Using async IO: this allows me to use WaitHandle.Wait One with a
timeout. But when A timeout occurs cancelling the pending async-io
operations (and the reulting synchronization ) is a problem.

- Using Socket.Select/Socket.Poll: Far from all Streams are based on
Socket, and I can't retrieve the Socket anyway since it's a protected
property in NetworkStream.

I've been googling a bit on stuff like .NET, C#, timeout, stream, read,
but it doesn't turn up much usefull info.

What could I do here? Basically i just wish to have an option to
wait-with-timeout ("select" or "poll" in unix/sockets terminology) for a
guarantee that Read will not block indefinatly if invoked.


Have you tried setting a timer and closing the stream from another
thread?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2


Jon Skeet [C# MVP] wrote:
Have you tried setting a timer and closing the stream from another
thread?


Thanks for your suggestion, that thought also crossed my mind as a way
to interrupt the async IO. Unfortunatly, closing the stream is not an
option. Usually I will have to retry by retransmitting the same message
down the same stream once more a few times.

Specifically, the stream does not guarantee transmission integrity to
the endpoint. Often it is a reliable network-connection, or a direct
serial connection, to a radio-network bridge which provides rather
unreliable transmission of data to the end destination. Closing the
connection to the bridge is not a good way to retry communication in the
radio network.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3


Helge Jensen wrote:
For implementing timeouts i have explored some solutions:

- Using async IO: this allows me to use WaitHandle.Wait One with a
timeout. But when A timeout occurs cancelling the pending async-io
operations (and the reulting synchronization ) is a problem.


I have implemented a solution based on this approach. it uses buffering
to avoid cancelling the pending async-io operation. It's not altogether
too pretty and only works on Streams that implement BeginRead
asynchoneously, but it seems to be my only option.

The code (including a test) is available from SVN at
http://slog.dk/svn/home/jensen/sourc...tstream/trunk/ if anybody
feels like taking it to the cleaners :)

On unix system, pretty much every IO-thingy have a "file-descriptor"
which is accessible by some kind of means and passable to select/poll. I
was hoping that the corresponding Socket abstraction in .NET was exposed
in Stream, or that Stream would expose a publicly available WaitHandle
that i could Wait-All/Any on. I guess I'll have to live without any of
that proven functionality ;)

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4

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

Similar topics

11
3890
by: Amey Samant | last post by:
hi all i was trying to read an integer from keyboard with DataInputStream can someone explain the behaviour of the following code ???? <code> DataInputStream dis=new DataInputStream(System.in); DataOutputStream dout = new DataOutputStream(System.out); a=dis.readInt(); System.out.println("Using system.out a = "+a);
5
16675
by: Jim | last post by:
Hi, I am trying to figure out a way to implement a timeout along with a read() call on an open file. It only has to work on linux, for now I am trying: ret = select.select( ,,, timeout ) if ret == # do read
0
1460
by: Garnet2 | last post by:
I am trying to get the following code to work : HttpWebRequest request1 = (HttpWebRequest) WebRequest.Create(@"http://localhost/TacUssd/8.tac"); request1.Method = "POST"; request1.Credentials = System.Net.CredentialCache.DefaultCredentials; request1.ContentType = "application/x-www-form-urlencoded"; //Stream stream = request1.GetRequestStream(); //Stream sr = File.Open(@"D:\My Documents\Visual Studio Projects\ussd\test.xml",...
1
3179
by: Joey Chömpff | last post by:
Hello, When I'm uploading an file to a JRun WebServer (third party) with a Windows Forms application, l always get an TimeOut while uploading, all other request who doesn't request an post are going ok? Why? I've tried almost everything. I also tried the KB888528 solution but this also doesn't work. What can the KB887563 do for me? Machine( tried on Windows 2003 Server & WinXp)
7
3156
by: simonrigby_uk | last post by:
Hi all, Sorry if this is the incorrect group but I couldn't see anything directly relevant. Can someone confirm for me what happens when two network streams are sent to an application at the same time. My scenario is a small server application that listens on a particular TCP port. I am sending streams of data to it via a client app. Inside
5
1663
by: Terry Olsen | last post by:
Is there a good way to find a pattern of bytes/chars in a stream? I've got a serial port connected to a tcp port. I need to be able to catch a unique character string in the stream so that I can perform certain functions. For example, I have a telnet client connected to an Apple II through the serial port. The user at the telnet terminal is using the BBS running on the Apple II just like the good ole days of dialup BBS's. I need to be...
3
11778
by: carmelo8 | last post by:
Hi there, I want to timeout Read method of System.Stream class after 30 secs. of no activity given a certain opened connection. That is, because some incoming connections to my application just don't give any signal and my Read method keeps waiting from the socket for some data to be read eternally. I have seen that in framework 2.0 System.Stream.Read has a new property that will probably match with what I need called 'ReadTimeout'....
3
6774
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk on the stream. What am I doing wrong? There is more code than this, but this is the problem code.
10
6183
by: Zytan | last post by:
I have a TcpClient. I set the read/write timeouts at 1 minute (in milliseconds). I get a NetworkStream from it and confirm the timeouts still exist. I do a NetworkStream.Write() and then a NetworkStream.Read(). Sometimes it sits and waits -- on the Write() or the Read() -- for 15 minutes before I get fed up and close the app..... I am not connecting to a TcpListener. I am connecting to a Socket with ProtocolType.Tcp, which is...
0
10357
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
10162
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...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
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.