What is a socket?
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In the design mode, thesocket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified association On.
So, we do not need to understand the TCP/UDP protocol, the socket has been packaged for us, we only need to follow the socket rules to program, write the program is naturally follow the TCP/UDP standard.
The history and classification of sockets
socket family based on file type
Socket family name: Af_unix
Unix all files, file-based sockets are called by the underlying file system to fetch data, two sockets process running on the same machine, you can access the same file system to complete the communication indirectly
socket family based on network type
Socket family name: af_inet
(There are also af_inet6 used for IPv6 and some other address families, but they are either used only on a platform, or have been discarded, or are rarely used, or are not implemented at all, and Af_inet is the most widely used one in all address families, Python supports a variety of address families, but since we only care about network programming, most of the time I use af_inet only)
Socket workflow
Socket Service Side
Start with the server side. The server-side initializes the Socket, then binds to the port (BIND), listens to the port (listen), calls the accept block, waits for the client to connect. At this point if a client initializes a Socket and then connects to the server (connect), the client-server connection is established if the connection is successful. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, closes the connection, and ends the interaction at once .
Socket () module function usage
ImportSocket2 Socket.socket (socket_family,socket_type,protocal=0)3socket_family can be Af_unix or af_inet. Socket_type can be sock_stream or sock_dgram. Protocol is generally not filled, the default value is 0. 4 5 Get tcp/IP Sockets6 Tcpsock =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 8 Get udp/IP Sockets9 Udpsock =Socket.socket (socket.af_inet, socket. SOCK_DGRAM)10 11 Because there are too many properties in the socket module. We made an exceptional use of it here.'From module Import *'Statement. Use'From socket Import *', we took all the attributes from the socket module into our namespace, which greatly shortened our code. 12 for example Tcpsock = socket (af_inet, SOCK_STREAM)
Socket functions
Service-side socket function S.bind () binding (host, port number) to Socket S.listen () starts TCP listener s.accept () passively accepts TCP client connections, (blocked) The arrival of the waiting connection client socket function S.connect () actively initializes an extended version of the TCP server Connection S.CONNECT_EX () connect () function, returning an error code when an error occurs, Instead of throwing an exception to the public-use socket function s.recv () receives TCP Data s.send () sends TCP data (send data is lost when the amount of data to be sent is greater than the remaining space in the cache) S.sendall () Send complete TCP data (essentially circular call Send,sendall when the amount of data to be sent is greater than the remaining space in the cache, the data is not lost, the loop calls send until the end of the Send) S.recvfrom () receive UDP data s.sendto () Send UDP data S.getpeername () the address of the remote that is connected to the current socket S.getsockname () address of the current socket s.getsockopt () Returns the parameter of the specified socket s.setsockopt () sets the parameter of the specified socket S.close () closes the socket for the lock socket Method s.setblocking () Sets the blocking and non-blocking mode for sockets S.settimeout () to set the timeout time for a blocked socket operation S.gettimeout () Gets the timeout time for a blocked socket operation the function of a socket for a file S.fileno () The socket's file descriptor S.makefile () creates a file associated with the socket
Python Network programming socket