Timeout on a UDP Socket

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • w

    Timeout on a UDP Socket

    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.readlin e()
    #print in_line
    in_file.close()

    if(UDPSock.send to(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(bu f)
    print "\nReceived message '", data,"'"

    #output result message from server
    f=open( "user1.out" , "w" )
    f.write( data )
    f.close()


    # Close socket
    UDPSock.close()
  • Skip Montanaro

    #2
    Re: Timeout on a UDP Socket


    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

    Comment

    • Josh Smith

      #3
      Re: Timeout on a UDP Socket

      In article <a1ff320f.04032 02029.55bc8d5f@ posting.google. com>, w wrote:[color=blue]
      > 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.[/color]


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

      For an example from the python docs:
      The official home of the Python Programming Language


      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 "SocketTime Out"


      # 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.readlin e()
      #print in_line
      in_file.close()

      if(UDPSock.send to(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(s ignal.SIGALRM,h andler)
      # 10 seconds is the limit
      signal.alarm(10 )
      data = UDPSock.recv(bu f)
      # 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.o ut","w")
      f.write("Timed out on receive from "+host+", got no data\n")
      f.close()



      # Close socket
      UDPSock.close()


      Comment

      • Josiah Carlson

        #4
        Re: Timeout on a UDP Socket

        > UDPSock = socket(AF_INET, SOCK_DGRAM)[color=blue]
        > # Receive messages
        > data = UDPSock.recv(bu f)[/color]

        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

        Comment

        Working...