Socket programming design problem

D

David

After programming with Python for a few hours, I've come up with some code:
http://p.shurl.net/3n. However, now I've realised there's a bit of a
problem with the original design.

I have a number of questions, if anybody could answer I'd be grateful.

a) Big problem, I can't see how to receive from more than one socket at
once. I need to do this so that data from the TCP connection can be sent
out on the UDP one and vice versa. Do I need a thread for each, or is
there some other way I can listen on two sockets at once (maybe
non-blocking ones?)

b) If the script dies prematurely, e.g. I mistyped something and Python
throws an exception, it is caught by the generic handler at the bottom.
However, the socket does not seem to be cleaned up properly, as if I try to
run it again immediately I get an error that it is still in use. lsof
doesn't show it in use though, which seems weird. Is there any particular
reason for this?

Thanks,

David
 
J

John J. Lee

David said:
a) Big problem, I can't see how to receive from more than one socket at
once. I need to do this so that data from the TCP connection can be sent
out on the UDP one and vice versa. Do I need a thread for each, or is
there some other way I can listen on two sockets at once (maybe
non-blocking ones?)

1. threads, yes
2. http://docs.python.org/lib/module-select.html (if on non-Windows system)
3. http://docs.python.org/lib/module-asyncore.html (ditto)
4. http://twistedmatrix.com/


Interesting but maybe not production-ready code:

1. http://kamaelia.sourceforge.net/Home
2. http://poshmodule.sourceforge.net/

b) If the script dies prematurely, e.g. I mistyped something and Python
throws an exception, it is caught by the generic handler at the bottom.
However, the socket does not seem to be cleaned up properly, as if I try to
run it again immediately I get an error that it is still in use. lsof
doesn't show it in use though, which seems weird. Is there any particular
reason for this?

Google for SO_REUSEADDR



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top