1. Half Open socket
Use the shutdown () function to make the socket bidirectional data transfer into one-way data. Shutdown () requires a separate parameter that indicates how to close the socket. The specific: 0 means prohibition of future reading; 1
to prohibit future writing; 2 means to prohibit future reading and writing.
2. Timeouts Control timeout
Call the socket's settimeout () function to pass the parameter to it, indicating the time-out setting. When accessing a socket, if nothing happens after the set time of the parameter, a Socket.timeout exception is generated.
For example: When the program runs, it waits for data to pass in. On the other terminal, use Telnet to connect to port 12345. After the connection is successful, the connection from: * * * is displayed, if the terminal is not entered in 5 seconds, the system
The system will prompt the connection timeout to exit.
The code is as follows:
The code is as follows:
#-*-coding:cp936-*-
# #tcp响应服务器
Import Socket,traceback
Host= "
port=12345
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
S.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
S.bind ((Host,port))
S.listen (1)
While 1:
Try
Clientsock,clientaddr=s.accept ()
Except Keyboardinterrupt:
Raise
Except
Traceback.print_exc ()
Continue
Clientsock.settimeout (5)
Try
Print "Connection from:", Clientsock.getpeername ()
While 1:
DATA=CLIENTSOCK.RECV (4096)
If not Len (data):
Break
Clientsock.sendall (data)
Clientsock.sendall ("\ni get it!\n")
# # T=raw_input (' Input the word: ')
# # Clientsock.sendall (t)
Except (Keyboardinterrupt,systemexit):
Raise
Except Socket.timeout:
print ' Connection timed out '
Pass
Except
Traceback.print_exc ()
Try
Clientsock.close ()
Except Keyboardinterrupt:
Raise
Except
Traceback.print_exc ()
3. Understanding Network byte Order
Different platforms have different binary data encoding methods, in order to solve this always, a standard binary data representation called network byte order. Before sending a binary integer, the integer is first
Convert to network byte order. When the receiver receives the data, the network byte order is converted to a cost-of-representation method before it is used.
Python's struct module provides support for converting data between Python and binary data.
The main two basic formats are:
H: Applies to 16-bit integers
I: Applies to 32-bit integers
An exclamation point indicates that the struct module is encoded and decoded using network byte order. Other formats are shown in the following table:
Character |
Byte Order |
Size and alignment |
@ |
Native |
Native enough 4 bytes |
= |
Native |
Standard by original number of bytes |
< |
Little-endian |
Standard by original number of bytes |
> |
Big-endian |
Standard by original number of bytes |
! |
Network (= Big-endian) |
Standard by original number of bytes |
Common statements:
Struct.pack (Fmt,v1,v2,...) Convert v1,v2 by parameter format. Parameter FMT is a format character
String, here is the main! I. V1,v2,... Represents the Python value to convert.
Struct.unpack (fmt,string) is just the opposite of pack.
For example:
>>> Import struct
>>> a=20
>>> Str=struct.pack ("! I ", a)
>>> print repr (str)
' \x00\x00\x00\x14 '
>>> print Struct.unpack ("! I ", str)
(20,)