Client/server architecture, i.e. C/s architecture:
1. Hardware C/S Architecture (printer)
2. Software c/S Architecture (Web service)
b/S architecture belongs to C/s architecture
Socket is for the development of C/s architecture, socket is the application layer and TCP/IP protocol Family Communication intermediate software abstraction layer, it is a set of interfaces. He hides the complex TCP/IP protocol family behind the socket interface. Let the socket organize the data to meet the criteria of the specified protocol. So we do not need to understand the TCP/UDP/IP protocol, the socket has been packaged for us, we only go to follow the socket rules to program, write the program naturally follow the TCP/UDP standard.
The IP and port bindings identify an application that is unique to the Internet. The PID of the program is the identification of different processes or threads on the same machine.
At first, sockets are designed to communicate between multiple applications on the same host, which is also known as interprocess communication, or IPC. There are two types of sockets (or become two races), which are file-based and web-based.
Socket family based on file type: Socket family name: Af_unix
Unix everything is file, the file-based socket calls the underlying file system to fetch data, two socket processes run 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
Af_inet6 is used in Ipv6,af_inet is the most widely used one, used for network programming.
ImportSocketphone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.bind (('127.0.0.1', 8000)) Phone.listen (5)Print("wait for the client to send a message: \ n") conn,addr=phone.accept ()#returns a tupleREC_MSG=CONN.RECV (1024)#1024 indicates how much byte information is receivedPrint('Client IP and port:', addr)#Client IP and port informationPrint('the message sent by the client is:', Rec_msg.upper ()) send_msg='I AM SERVER'conn.send (Send_msg.encode ('Utf-8') ) Conn.close ()#Close ConnectionPhone.close ()#Close Socket
Socket Service Side
import Socketphone =socket.socket (socket.af_inet,socket. Sock_stream) Phone.connect (( " 127.0.0.1 ", 8000)) # Establish connection phone.send ( " i AM CLIENT ". Encode (" utf-8 ") # Send Message data=phone.recv (1024 print ( " receives information returned by the server ", data)
Socket Client
Python-socket programming