473,378 Members | 1,577 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,378 software developers and data experts.

Timeout on a UDP Socket

w
I'm having problems with this program hanging on the revc(buf). I was
trying to figure out a way to allow the socket to timeout. I'm have
tried making the socket non-blocking but had on luck. Being very new
to Python, could there be a better way to accomplish this task. The
program works very well, but on occassion is will not receive the data
from the server and hang.

Thank,
# Client program

import os.path

from socket import *
from time import *

# Set the socket parameters
#host = "localhost"
host = "10.0.0.20"
port = 21567
buf = 1024
addr = (host,port)

# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

# Send messages
while(1):
if not os.path.exists( "user1.inp" ) :
sleep( 2 )
else:
print "opening....."
print "reading....."
in_file = open("user1.inp", "r")

data = in_file.readline()
#print in_line
in_file.close()

if(UDPSock.sendto(data,addr)):
print "Sending message '",data,"'.....<done>"
if os.path.exists( "user1.inp" ) :
os.remove( "user1.inp" )

print "Waiting for Response..."

# Receive messages
data = UDPSock.recv(buf)
print "\nReceived message '", data,"'"

#output result message from server
f=open( "user1.out", "w" )
f.write( data )
f.close()
# Close socket
UDPSock.close()
Jul 18 '05 #1
3 9860

w> I'm having problems with this program hanging on the revc(buf). I
w> was trying to figure out a way to allow the socket to timeout.

Since 2.3 you can set socket timeouts. It ought to work for UDP sockets
though I've never tried it with anything other than TCP sockets. Check the
socket module libref page.

Skip

Jul 18 '05 #2
In article <a1**************************@posting.google.com >, w wrote:
I'm having problems with this program hanging on the revc(buf). I was
trying to figure out a way to allow the socket to timeout. I'm have
tried making the socket non-blocking but had on luck. Being very new
to Python, could there be a better way to accomplish this task. The
program works very well, but on occassion is will not receive the data
from the server and hang.

You could use a sigalarm (assuming your OS supports that).

For an example from the python docs:
http://www.python.org/doc/current/lib/node304.html

I've also added some example code to your client example demonstrating this (if
you'll excuse the intrusion into your code :) )

-jbs
# Client program

import os.path,signal

from socket import *
from time import *

# Set the socket parameters
#host = "localhost"
host = "10.0.0.20"
port = 21567
buf = 1024
addr = (host,port)
def handler(signum,frame):
"""This is a handler function called when a SIGALRM is received,
it simply raises a string exception"""

raise "SocketTimeOut"
# Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

# Send messages
while(1):
if not os.path.exists( "user1.inp" ) :
sleep( 2 )
else:
print "opening....."
print "reading....."
in_file = open("user1.inp", "r")

data = in_file.readline()
#print in_line
in_file.close()

if(UDPSock.sendto(data,addr)):
print "Sending message '",data,"'.....<done>"
if os.path.exists( "user1.inp" ) :
os.remove( "user1.inp" )

print "Waiting for Response..."

# Receive messages
try:
# setup signal handler
signal.signal(signal.SIGALRM,handler)
# 10 seconds is the limit
signal.alarm(10)
data = UDPSock.recv(buf)
# shutdown the alarm
signal.alarm(0)
print "\nReceived message '", data,"'"
#output result message from server
f=open( "user1.out", "w" )
f.write( data )
f.close()
except "SocketTimeOut":
f=open("user1.out","w")
f.write("Timed out on receive from "+host+", got no data\n")
f.close()

# Close socket
UDPSock.close()
Jul 18 '05 #3
> UDPSock = socket(AF_INET,SOCK_DGRAM)
# Receive messages
data = UDPSock.recv(buf)


I believe in order to receive from a UDP socket, it must be bound to an
address and port so that the server can actually send it somewhere.

- Josiah
Jul 18 '05 #4

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

Similar topics

17
by: Achim Domma | last post by:
Hi, I'm using Python 2.3s timeout sockets and have code like this to read a page from web: request = ... self.page = urllib2.urlopen(request) and later:
10
by: Jacek Popławski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
1
by: Mark Smith | last post by:
Hi , Is it possible in .Net to define a async timer callback. What I am wanting to do is declare a time-out condition that gets executed should the something timeout. The reason I want to do...
5
by: supercooper | last post by:
I am downloading images using the script below. Sometimes it will go for 10 mins, sometimes 2 hours before timing out with the following error: Traceback (most recent call last): File...
5
by: John Nagle | last post by:
I thought I had all the timeout problems with urllib worked around, but no. socket.setdefaulttimeout is useful, but not always effective. I'm setting that to 15 seconds. If the host end won't...
2
by: Robin Becker | last post by:
While messing about with some deliberate socket timeout code I got an unexpected timeout after 20 seconds when my code was doing socket.setdefaulttimeout(120). Closer inspection revealed that...
4
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am wondering if I am using TCPClient class in C#, how to setup timeout value? Timeout I mean, when connects to server for the 1st time, and during <timeoutinterval, if no...
2
by: carl.rosenberger | last post by:
Hi, I am trying to get the following behaviour for my Socket connection: (1) Attempt a blocked read for a defined amount of time. (2) If a timeout occurs, because no data has been sent to the...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
3
by: oberon | last post by:
Hi, I hope it's ok for me to place an IronPython question in this forum(?). I am running IronPython 1.1.2 and Python 2.5. OS is Windows XP SP3. Currently I'm developing a lightweight test...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
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$) { } ...
0
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...
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...

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.