By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

Timeout on a UDP Socket

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.