Hi how to kill the recv or recvfrom in socket connection, then there is no data and the functions recv, recvfrom is in waiting mode.?for example:

sock=socket.socket(...)
sock.bind((host,port))
while True:
  packet,address=socket.recvfrom()
  print packet

if there is no data on socket it is waiting, so how to kill it if it doesn't get any data for about 5 or less seconds?thanks in advance

Recommended Answers

All 3 Replies

Why don't you set the socket's timeout to 5 and then catch the timeout error ?

sock=socket.socket(...)
sock.bind((host,port))
sock.settimeout(5)
try:
    while True:
      packet,address=socket.recvfrom()
      print packet
except socket.timeout:
    print "socket timed out"
finally:
    socket.close()

why except gives the syntax error?

why except gives the syntax error?

Which syntax error ? can you post the traceback ?
By the way you probably mean sock.recvfrom() instead of socket.recvfrom().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.