473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

socket read timeout

hg
Hi,

I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)

Tanks,

hg

Mar 27 '07 #1
18 3378
hg napisał(a):
I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)
Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?

--
Jarek Zgoda

"We read Knuth so you don't have to."
Mar 27 '07 #2
Jarek Zgoda wrote:
hg napisał(a):
>I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)

Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?
Note that since 2.4, I believe, sockets in the standard library allow
you to specify a timeout parameter.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 27 '07 #3
>I am looking for the most efficient / cleanest way to implement a
socket read with timeout (Windows mainly but would be great if the
same code worked under *nix)
JarekDid you see http://www.timo-tasi.org/python/timeoutsocket.py ?

Also socket objects have timeout attributes now.

Skip

Mar 27 '07 #4
hg
Steve Holden wrote:
Jarek Zgoda wrote:
>hg napisa?(a):
>>I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)

Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?
Note that since 2.4, I believe, sockets in the standard library allow
you to specify a timeout parameter.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
Hi and thanks to all.

Do you mean use select ?

Thanks,

hg

Mar 27 '07 #5
hg wrote:
Do you mean use select ?
No, socket's timeout:
>>import socket
s = socket.socket()
s.settimeout(5)
...
Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Mar 27 '07 #6
hg
Facundo Batista wrote:
hg wrote:
>Do you mean use select ?

No, socket's timeout:
>>>import socket
s = socket.socket()
s.settimeout(5)
...

Regards,

--
. Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

My issue with that is the effect on write: I only want a timeout on read ...
but anyway ...

Thanks,

hg
Mar 28 '07 #7
hg wrote:
Facundo Batista wrote:
>hg wrote:
>>Do you mean use select ?
No, socket's timeout:
>>>>import socket
s = socket.socket()
s.settimeout(5)
...
Regards,

--
. Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


My issue with that is the effect on write: I only want a timeout on read ...
but anyway ...
If you want selective timeout behaviour then yes, you should use select.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 28 '07 #8

hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you want
to read.

Skip
Mar 28 '07 #9
hg
sk**@pobox.com wrote:
>
hgMy issue with that is the effect on write: I only want a timeout
on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you
want to read.

Skip

Not bad .. thanks

Mar 28 '07 #10
<skip@p....x.comwrote:

>
hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you want
to read.
Are sockets full duplex?

I know Ethernet isn't.

- Hendrik
Mar 29 '07 #11
>So set a long timeout when you want to write and short timeout when you
>want to read.

Are sockets full duplex?

I know Ethernet isn't.
Both are full duplex.

--
damjan
Mar 29 '07 #12
Hendrik van Rooyen wrote:
<skip@p....x.comwrote:

> hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you want
to read.

Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
I know Ethernet isn't.
Don't know much, then, do you? ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 1 '07 #13
"Steve Holden" <s..e@hol....eb.com>

Hendrik van Rooyen wrote:
<skip@p....x.comwrote:

hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you
want
to read.
Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
This seems to bear out the scenario I have described elsewhere in this
thread - I think its caused by the file handlers, but I don't _know_ it.
>
I know Ethernet isn't.
Don't know much, then, do you? ;-)
No not really - I easily get confused by such things as collisions...

: - )

- Hendrik

Apr 1 '07 #14
Steve Holden wrote:
Hendrik van Rooyen wrote:
>Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?
--
--Bryan
Apr 1 '07 #15
"Bryan Olson" <fa*********@nowhere.orgwrote:

Steve Holden wrote:
Hendrik van Rooyen wrote:
Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.

Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?
Elsewhere in this thread I wrote about my experience with a serial port,
where I can show that the "file handler" only does the write once the
blocking read completes - and the point at issue is if sockets are the same.

We regularly get questions about "my stuff does not come out" on
the group, and I wondered whether this effect is the underlying cause.

But I don't know about the sockets case, which is why I asked.

You raise an interesting point about a different process - my serial
experience is using threads. I have never tried mixing processes
on a serial port. Haven't a clue if its possible, or if the behaviour
will be different.

- Hendrik

Apr 2 '07 #16
Hendrik van Rooyen wrote:
"Steve Holden" <s..e@hol....eb.com>

>Hendrik van Rooyen wrote:
>> <skip@p....x.comwrote:
hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you
want
>>>to read.

Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.

This seems to bear out the scenario I have described elsewhere in this
thread - I think its caused by the file handlers, but I don't _know_ it.
>>I know Ethernet isn't.
Don't know much, then, do you? ;-)

No not really - I easily get confused by such things as collisions...

: - )
Right, but collisions are *so* twentieth-century, aren't they. With a
properly-implemented switched infrastructure Ethernet interfaces can
transmit and receive at the same time.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 2 '07 #17
Bryan Olson wrote:
Steve Holden wrote:
>Hendrik van Rooyen wrote:
>>Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.

Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?

Nothing, I guess. I just didn't consider threaded code ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 2 '07 #18
"Steve Holden" <s..e@h...b.comwrote:
>
Right, but collisions are *so* twentieth-century, aren't they. With a
properly-implemented switched infrastructure Ethernet interfaces can
transmit and receive at the same time.
This is true, while "A" and "B" are not simultaneously trying to address
"C" - Then you need something like store and forward, on the fly...

: - ) better known as "routing"...

Some (most?) of the little switches I have seen are too dumb even to
allow "A" to talk to "B" while "C" is talking to "D" - they just broadcast
the first "talker"'s message to all the "listeners" - little better than
active hubs, destroying the end point's hardware capability to talk and
listen at the same time.

I think the keywords here are "properly implemented" - its actually not a
trivial problem, as the switch has to know or learn who is where, and set
up paths accordingly, in real time. This is hard to do without store and
forward.

- Hendrik
Apr 3 '07 #19

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

Similar topics

2
by: Adam | last post by:
Hi, I'm using the GNU Common C++ library to read data from a TCPStream. In the code below, why does the read not throw an exception if the socket times out? (If I check the error code after the...
2
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
0
by: Tomasz Naumowicz | last post by:
Hello Everybody, I have the following problem with Socket: Socket.Available reports data in the buffer (e.g. 1849 bytes) but I can't read it because Socket.Read throws an exception The...
2
by: Lisa Pearlson | last post by:
Hi, I am wanting to write an internet 'server' program. For ease, I want to use PHP-CLI with XINET super server. XINET communicates with the 'server' program via STDIN/STDOUT. I'm not sure...
3
by: Mike Schilling | last post by:
How does one put a timeout on an attempt to connect a socket? There's no timeout parameter to Socket.Connect(), and while there are socket options for send and receive timeouts, there is none for...
1
by: Mani | last post by:
Hi, This is the second time i am posting this question, since i didnt get any reponse from my previous post. When i tried to execute the script in browser, i am getting the following...
0
by: PAzevedo | last post by:
Hi there. I've read a few posts on this group about the select timeout not working when set to -1, when according to the documentation it should. I found no solutions, just workarounds. ...
3
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
0
by: fborot | last post by:
hello I am trying to use the socket receive timeout property but I find it not accurate, there is an offset of 500 ms. example, I set it to 500 ms, send the bytes, set the socket to receive and if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.