SocketWhat is it?
Socket is the application layer andThe intermediate software abstraction layer for TCP/IP communication. It is a group of interfaces. In the design mode,Socket is actually a facade mode, which makes the complexThe TCP/IP protocol family is hidden inAfter the socket interface, for users, a set of simple interfaces is all, letSocket organizes data to conform to the specified protocol.
How to Use SOCKET?
Start with the server. Initialize the server firstSocket, and then bind with the port(BIND) to listen to the port(Listen), callAccept blocking, waiting for client connection. At this time, if a client initializesSocket, and then connect to the server(CONNECT). If the connection is successful, the connection between the client and the server is established. The client sends a data request. The server receives the request and processes the request. Then, the response data is sent to the client. The client reads the data and closes the connection. The interaction ends.
Create a socket object:
Socket.Socket(Family = af_inet,Type = sock_stream)
The value of family can be af_unix (UNIX domain used for communication between processes on the same machine) or af_inet (for TCP and UDP of IPv4 protocol)
The type parameter can be sock_stream (stream socket), sock_dgram (Data PACKET socket), or sock_raw (raw socket ).
Main attributes and methods of the socket object:
Socket.Bind(Address)
Generally, address is a dual-element tuples (host, Port), host name or IP address + port number.
Socket.Listen(Backlog)
Number of socket listeners, which defines the maximum queue. The maximum value depends on the main system (usually 5)
Socket. Accept ()
When the accept method is called, the socket enters the 'waiting' (or blocking) state. When a customer requests a connection, the method establishes a connection and returns it to the server. The accept method returns a tuples containing two elements, such as (connection, address ). The first element (connection) is a new socket object through which the Server communicates with the customer. The second element (Address) is the customer's Internet address.
Socket. Connect (Address)
Connect to the remote socket defined by address
Socket.Send(Bytes[,Flags])
Send information to remote socket
Socket. Recv (Bufsize[,Flags])
The server obtains data information through socket,BufsizeDefines the maximum value of one-time receipt.
TCP
Server. py
Import sockets = socket. socket (socket. af_inet, socket. sock_stream) s. BIND ("", 8081) s. listen (1) While true: connection, address = S. accept () Data = connection. recv (1024) print ('Received: ', data)
Client. py
Import socketfrom Pip. backwardcompat import raw_inputs = socket. socket (socket. af_inet, socket. sock_stream) Port = 8081 host = "localhost" s. connect (host, Port) while true: MSG = raw_input () s. send (bytes (MSG, 'utf-8') s. close ()
UDP
Server code
Import sockets = socket. socket (socket. af_inet, socket. sock_dgram) s. BIND ("", 8081) while true: data, host = S. recvfrom (1024) print ('stored ed: ', Data, 'from', host)
Client code
Import socketfrom Pip. backwardcompat import raw_inputs = socket. socket (socket. af_inet, socket. sock_dgram) Port = 8081 host = "localhost" While true: MSG = raw_input () print (MSG) s. sendto (bytes (MSG, encoding = "UTF-8"), (host, Port ))
Socket module Learning