| 1 | \documentclass{howto}
|
|---|
| 2 |
|
|---|
| 3 | \title{Socket Programming HOWTO}
|
|---|
| 4 |
|
|---|
| 5 | \release{0.00}
|
|---|
| 6 |
|
|---|
| 7 | \author{Gordon McMillan}
|
|---|
| 8 | \authoraddress{\email{[email protected]}}
|
|---|
| 9 |
|
|---|
| 10 | \begin{document}
|
|---|
| 11 | \maketitle
|
|---|
| 12 |
|
|---|
| 13 | \begin{abstract}
|
|---|
| 14 | \noindent
|
|---|
| 15 | Sockets are used nearly everywhere, but are one of the most severely
|
|---|
| 16 | misunderstood technologies around. This is a 10,000 foot overview of
|
|---|
| 17 | sockets. It's not really a tutorial - you'll still have work to do in
|
|---|
| 18 | getting things operational. It doesn't cover the fine points (and there
|
|---|
| 19 | are a lot of them), but I hope it will give you enough background to
|
|---|
| 20 | begin using them decently.
|
|---|
| 21 |
|
|---|
| 22 | This document is available from the Python HOWTO page at
|
|---|
| 23 | \url{http://www.python.org/doc/howto}.
|
|---|
| 24 |
|
|---|
| 25 | \end{abstract}
|
|---|
| 26 |
|
|---|
| 27 | \tableofcontents
|
|---|
| 28 |
|
|---|
| 29 | \section{Sockets}
|
|---|
| 30 |
|
|---|
| 31 | Sockets are used nearly everywhere, but are one of the most severely
|
|---|
| 32 | misunderstood technologies around. This is a 10,000 foot overview of
|
|---|
| 33 | sockets. It's not really a tutorial - you'll still have work to do in
|
|---|
| 34 | getting things working. It doesn't cover the fine points (and there
|
|---|
| 35 | are a lot of them), but I hope it will give you enough background to
|
|---|
| 36 | begin using them decently.
|
|---|
| 37 |
|
|---|
| 38 | I'm only going to talk about INET sockets, but they account for at
|
|---|
| 39 | least 99\% of the sockets in use. And I'll only talk about STREAM
|
|---|
| 40 | sockets - unless you really know what you're doing (in which case this
|
|---|
| 41 | HOWTO isn't for you!), you'll get better behavior and performance from
|
|---|
| 42 | a STREAM socket than anything else. I will try to clear up the mystery
|
|---|
| 43 | of what a socket is, as well as some hints on how to work with
|
|---|
| 44 | blocking and non-blocking sockets. But I'll start by talking about
|
|---|
| 45 | blocking sockets. You'll need to know how they work before dealing
|
|---|
| 46 | with non-blocking sockets.
|
|---|
| 47 |
|
|---|
| 48 | Part of the trouble with understanding these things is that "socket"
|
|---|
| 49 | can mean a number of subtly different things, depending on context. So
|
|---|
| 50 | first, let's make a distinction between a "client" socket - an
|
|---|
| 51 | endpoint of a conversation, and a "server" socket, which is more like
|
|---|
| 52 | a switchboard operator. The client application (your browser, for
|
|---|
| 53 | example) uses "client" sockets exclusively; the web server it's
|
|---|
| 54 | talking to uses both "server" sockets and "client" sockets.
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | \subsection{History}
|
|---|
| 58 |
|
|---|
| 59 | Of the various forms of IPC (\emph{Inter Process Communication}),
|
|---|
| 60 | sockets are by far the most popular. On any given platform, there are
|
|---|
| 61 | likely to be other forms of IPC that are faster, but for
|
|---|
| 62 | cross-platform communication, sockets are about the only game in town.
|
|---|
| 63 |
|
|---|
| 64 | They were invented in Berkeley as part of the BSD flavor of Unix. They
|
|---|
| 65 | spread like wildfire with the Internet. With good reason --- the
|
|---|
| 66 | combination of sockets with INET makes talking to arbitrary machines
|
|---|
| 67 | around the world unbelievably easy (at least compared to other
|
|---|
|
|---|