source: trunk/essentials/dev-lang/python/Doc/howto/sockets.tex@ 3298

Last change on this file since 3298 was 3225, checked in by bird, 19 years ago

Python 2.5

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