Lines Matching +full:days +full:- +full:before +full:- +full:close
1 .. _socket-howto:
14 It's not really a tutorial - you'll still have work to do in getting things
23 the sockets in use. And I'll only talk about STREAM (i.e. TCP) sockets - unless you really
27 work with blocking and non-blocking sockets. But I'll start by talking about
28 blocking sockets. You'll need to know how they work before dealing with
29 non-blocking sockets.
33 distinction between a "client" socket - an endpoint of a conversation, and a
40 -------
45 cross-platform communication, sockets are about the only game in town.
48 like wildfire with the internet. With good reason --- the combination of sockets
61 # now connect to the web server on port 80 - the normal http port
75 # bind the socket to a public host, and a well-known port
92 queue up as many as 5 connect requests (the normal max) before refusing outside
106 There's actually 3 general ways in which this loop could work - dispatching a
108 ``clientsocket``, or restructure this app to use non-blocking sockets, and
116 up - they are using some dynamically allocated port which will be recycled when
121 ---
129 The :mod:`multiprocessing` integrates cross-platform IPC into a higher-level
141 perhaps a signon. But that's a design decision - it's not a rule of sockets.
144 and ``recv``, or you can transform your client socket into a file-like beast and
152 Now we come to the major stumbling block of sockets - ``send`` and ``recv`` operate
185 - coded for clarity, not efficiency
210 chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
217 The sending code here is usable for almost any messaging scheme - in Python you
225 two ``recv``\ s - the first to get (at least) that first character so you can
240 your code will very quickly break unless you use two ``recv`` loops - the first
252 -----------
257 bytes 00 01. Intel and DEC, however, are byte-reversed - that same 1 is 01 00.
258 Socket libraries have calls for converting 16 and 32 bit integers - ``ntohl,
261 nothing, but where the machine is byte-reversed, these swap the bytes around
264 In these days of 32 bit machines, the ascii representation of binary data is
268 fixed-length messages. Decisions, decisions.
274 Strictly speaking, you're supposed to use ``shutdown`` on a socket before you
275 ``close`` it. The ``shutdown`` is an advisory to the socket at the other end.
279 piece of etiquette that normally a ``close`` is the same as ``shutdown();
280 close()``. So in most situations, an explicit ``shutdown`` is not needed.
282 One way to use ``shutdown`` effectively is in an HTTP-like exchange. The client
290 is garbage collected, it will automatically do a ``close`` if it's needed. But
292 doing a ``close``, the socket at the other end may hang indefinitely, thinking
293 you're just being slow. *Please* ``close`` your sockets when you're done.
297 ----------------
300 other side comes down hard (without doing a ``close``). Your socket is likely to
302 before giving up on a connection. If you're using threads, the entire thread is
306 the thread - part of the reason that threads are more efficient than processes
312 Non-blocking Sockets
318 inside-out.
320 In Python, you use ``socket.setblocking(False)`` to make it non-blocking. In C, it's
324 do this after creating the socket, but before using it. (Actually, if you're
331 large, buggy and suck CPU. So let's skip the brain-dead solutions and do it
337 it's close enough to the C version that if you understand ``select`` in Python,
352 thing to do - give it a nice long timeout (say a minute) unless you have good
360 as-close-to-certain-as-we-ever-get-in-this-business that a ``recv`` on that
363 nothing. (Actually, any reasonably healthy socket will return as writable - it
373 determining whether you will block - the socket returns as readable when there's