473,398 Members | 2,113 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,398 software developers and data experts.

Socket programming design problem

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
Dec 22 '05 #1
2 1116
David <nu**@example.net> writes:
[...]
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

Dec 22 '05 #2
On Thu, 22 Dec 2005 22:12:09 +0000,
David <nu**@example.net> wrote:
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?)


Try the select module:

http://www.python.org/doc/current/li...le-select.html

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Dec 23 '05 #3

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

Similar topics

2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
6
by: ahlongxp | last post by:
socket.makefile() may lose data when "connection reset by peer". and socket.recv() will never lose the data. change the "1" to "0" in the client code to see the difference. confirmed on both...
3
by: A. W. Dunstan | last post by:
I'm creating a socket as follows: m_networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_networkSocket.LingerState = new LingerOption(true, 1);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.