473,671 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timeout on Stream.Read

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.R ead has a new property
that will probably match with what I need called 'ReadTimeout'. But i
was wondering how to do that in framework 1.1 that do not has this one.

I have tried to do that with System.Timers.T imer class throwing an
event after 30secs. that create a thread to do some action but i don't
actually need a new thread i just want to timeout an operation (Read)
and after a certain time unblock it and be able to fire some exception
or just handle the "non response from the established connection"
scenario.

If anyone can help me with that or know how to solve it I'll be really
glad. Thanks in advanced.

Carmelo

Oct 27 '06 #1
3 11749
<ca******@gmail .comwrote:
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.R ead has a new property
that will probably match with what I need called 'ReadTimeout'. But i
was wondering how to do that in framework 1.1 that do not has this one.

I have tried to do that with System.Timers.T imer class throwing an
event after 30secs. that create a thread to do some action but i don't
actually need a new thread i just want to timeout an operation (Read)
and after a certain time unblock it and be able to fire some exception
or just handle the "non response from the established connection"
scenario.

If anyone can help me with that or know how to solve it I'll be really
glad. Thanks in advanced.
If you close the stream from a different thread, I believe the Read
call will throw an exception.

--
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
Oct 27 '06 #2
I have tried and it ain't seems to work... Is like the Read() locks the
stream so i can't close it through another thread. Here I post the code
I'm currently using:

public class1
{
myTimer.Elapsed += new
System.Timers.E lapsedEventHand ler(OnTimedEven t);
myTimer.Interva l = 3000;
myTimer.Start() ;

try
{
count = ins.Read();
}
catch(System.Ob jectDisposedExc eption)
{
throw new IOException();
}
myTimer.Stop();
...

private static void OnTimedEvent(ob ject source,
System.Timers.E lapsedEventArgs e)
{
myTimer.ins.Clo se();
myTimer.Dispose ();
}
}
public class MyTimer : System.Timers.T imer
{
public Stream ins;
public MyTimer(Stream ins)
{
this.ins = ins;
}
}

thanks,
Carmelo
If you close the stream from a different thread, I believe the Read
call will throw an exception.
Oct 30 '06 #3
Lao Tse <ca******@gmail .comwrote:
I have tried and it ain't seems to work... Is like the Read() locks the
stream so i can't close it through another thread. Here I post the code
I'm currently using:
What kind of timers are you using? If they're Windows Forms timers, it
would be trying to use the same thread (the UI thread) for both event
handlers - that certainly wouldn't work. You'll need to be in a
different thread.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
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
Oct 31 '06 #4

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

Similar topics

0
1448
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",...
3
15090
by: Michael Fields | last post by:
I have a problem with the WebClient and uploading a file to a web server. The stream object returned from the WebClient.OpenWrite menthod throws a timeout exception when using this method on computers w/ low bandwith ( < = 256 kbs). The exception occurs when I call the Stream.Close method. Here is the code: WebClient client = new WebClient(); Stream postStream = client.OpenWrite(uri,"PUT"); postStream.Write(buffer, 0, buffer.Length);...
1
3175
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)
3
6461
by: Helge Jensen | last post by:
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....
1
2778
by: davvel | last post by:
It has been 6 days of re-writing the same code in different ways to try and avoid a getResponse Timeout which haunted me for much too long now. I am trying to do a very simple thing which is prooving the most difficult in my career. Scrape a screen which has several session variables within it, and send this page to an e-mail. I have two pages in my application webform1.aspx and webform2.aspx, in webform1.aspx I set several session...
3
4535
by: Al G | last post by:
Does anyone know what a -1 means as a Network stream read timeout? Console.WriteLine("Timeout= {0}", stream.ReadTimeout) returns a -1 Thanks
5
1607
by: poe | last post by:
Hello, I currently have a routine that copies the contents of a web request into a string. I would like, however, to set the period after which the reader will throw an exception. I don't want my users to wait 300 seconds, instead I would rather have an exception thrown and the user notified of the service essentially being down. Here is what I've come up with, but it doesn't seem to timeout correctly. Any suggestions? Assume any...
10
6164
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...
2
4369
by: Piotrekk | last post by:
Hi I am trying to upload a 700 mb file using webrequest/response model. At 3x% I am getting the same error: "Unable to write data to the transport connection: an established connection was aborted by the software in your host machine" CLIENT side script is:
0
8472
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
8819
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
8596
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
7428
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
6222
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...
0
5690
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
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1801
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.