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 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 protocol.
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)
Workflow for Sockets
Socket () module function usage
Service-Side socket functions
S.bind () binding (host, port number) to socket
S.listen () Start TCP Listener
S.accept () passively accepts a TCP client connection, (blocking) waits for a connection to arrive
Client socket functions
S.connect () active initialization of TCP server connections
Extended version of the S.CONNECT_EX () connect () function, which returns an error code instead of throwing an exception when an error occurs
Socket functions for public use
S.RECV () receiving 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 () sends the full TCP data (essentially a cyclic call Send,sendall the data is not lost when the amount of data to be sent is greater than the remaining space in the buffer, and the call to send is sent until it is finished)
S.recvfrom () receiving 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 parameters of the specified socket
S.setsockopt () sets the parameters of the specified socket
S.close () close socket
TCP-based socket usage
TCP is link-based, you must start the server, and then start the client to link the server
Service side
# Create a server socket ss.bind () # bind address to Socket Ss.listen () # Listen link inf_loop: # server infinite loop # Accept Client Links Comm_loop: # communication Loop # dialog (receive and send) cs.close () # Close the client socket ss.close () # Close the server socket (optional)
Client
CS = socket () # Create client Socket cs.connect () # try to connect to server comm_loop: # Communication Cycle Cs.send ()/cs.recv () # Dialog (send/Receive)Cs.close () # close client sockets
the handling of sticky packs.
TCP protocol, also known as the Stream protocol, unlike UDP, UDP protocol is a datagram protocol, the header information will be added to each packet, so there will be no sticky packet phenomenon.
There are two situations where there is a sticky packet phenomenon in TCP:
Send the end of the buffer full to send information, send data time is short, packets of smaller packets, will be sent out together.
The receiver does not receive the buffer in time packets, resulting in multiple packets received (the client sent a piece of data, the server only received a small portion of the service end of the next time to collect the last data from the buffer, resulting in sticky packets)
Methods for handling sticky packets
Python socket programming