[Tutor] More Q's on Socket Programming

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Aug 4 05:10:45 CEST 2005

On Wed, 3 Aug 2005, Joseph Quigley wrote:

> decided to make a bot. But then I got an idea to make a chatting
> program. It worked nicely on our home network but it had a small
> problem.... you can't type anything and send it to the person you are
> chatting with untill they reply!!

Hi Joseph,

Can you show us the code?  We might be able to identify the issue here.
I suspect, though, that it has to do with the asynchronous nature of
sockets, and how you might be doing something like:

    next_line = raw_input(">> ")

which prevents you from checking the socket for input.

Basically, your program is trying to do two things at once: read input
from the user at the console, and read input from the socket.  There are
two main approaches that people have used to do two things at once:
threads, and event-driven programming.  We can talk about either in more
detail if you want.

> I also have one computer as the server and another as the client.... can
> I have a dedicated server program that handles two clients instead of
> having a direct connect? If you have any examples please send them too
> me.. it would be very much appreciated!!

Yes, check Section 5 in the Socket HOWTO on "Non-blocking Sockets":


They mention 'select', which is at the heart of doing an event-driven
socket program.  The idea is to pass control off to 'select()', and it'll
tell you whenever something's ready for processing, be it text from the
user or text from your network socket.

Here are a few thread tutorials:


Finally, if you're really getting into socket programming, you may want to
consider looking into the Twisted framework:


because the Twisted folks have done a lot of framework-building to
simplify socket programming issues.  On the downside, this means learning
Twisted.  But on the upside, this isn't as bad as it sounds.  *grin*

Good luck!

More information about the Tutor mailing list