1.Socket Overview
Socket (Socket) is an abstraction layer, the application can send or receive data through it, it can be the same as file open, close, and write operations. Sockets allow an application I / O into the network, and communicate with other applications in the network. Network socket is a combination of IP addresses and ports .
Sockets can be seen as a two endpoint applications in communication networks, each communication connection. A piece of information communication, wherein a network application to be transmitted in a Socket host writes it in the Socket Socket transmits this information through a transmission medium to the network interface card in another host, so that this information can be conveyed to other programs. Thus, the data transmission between the two is accomplished through the application socket. Workflow as shown below:
Socket (): create socket.
Directly on the code:
Client:
. 1 Import Socket 2 Import SYS . 3 . 4 # Create a Socket . 5 S = socket.socket (socket.AF_INET, socket.SOCK_STREAM) . 6 # to establish a connection . 7 s.connect (( ' 127.0.0.1 ' , 3000 )) . 8 the while True: 9 # sending data: 10 the try : . 11 data = INPUT ( " client: " ) 12 is s.send (data.encode ()) 13 is buf = s.recv (1024 ) .decode () 14 if buf != 'exit': 15 print("服务端: " + buf) 16 except: 17 print("Dialogue Over") 18 s.close() 19 sys.exit(0)
Server:
. 1 Import Socket 2 . 3 S = socket.socket (socket.AF_INET, socket.SOCK_STREAM) . 4 s.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR,. 1 ) . 5 s.bind (( ' 127.0.0.1 ' , 3000)) # listening port 6 s.listen (1) # call to listen () method starts listening port, the incoming parameter specifies the maximum number of connections waiting 7 our sock, addr = s.accept () 8 buf = sock.recv (1024 ) .decode () . 9 the while True: 10 IF buf =! ' Exit ' : . 11 Print("客户端: " + buf) 12 data = input("服务端: ") 13 sock.send(data.encode()) 14 if data == 'exit': 15 break 16 buf = sock.recv(1024).decode()
Both programs have so few lines, corresponding to the above workflow description:
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM):
Create a socket for communication of IPV4 (socket.AF_INET) and use the streaming socket for TCP (socket.SOCK_STREAM)
s.bind(('127.0.0.1', 3000)):
The address (host name (127.0.0.1), port number (3000) of) bound to the socket
s.connect(('127.0.0.1', 3000))
Connected to the socket end of the service designated
s.listen(1):
Call to listen () method starts listening port, the incoming parameter specifies the maximum number of connections waiting, here 1
sock, addr = s.accept() :
And returned to accept the TCP connection (conn, address), where conn is a new socket object, it can be used to receive and transmit data. address is the address of the connecting client.
sock.send(data.encode()):
TCP data transmission, the transmission data parameters to the socket connection.
buf = sock.recv(1024).decode():
Accept TCP socket data. Data is returned as a string, the maximum amount of data to be received is designated 1024.
s.close():
Closes the socket.
Program results are as follows:
Client:
Server:
Note that first start Server restart Client, otherwise it will error
Correspondence between 3.python function and Linux Socket API's
Correspondence relation as follows:
python function | Linux Socket API |
socket.socket | int socket(int domain, int type, int protocol); |
bind | int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
connect | int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
listen | int listen(int sockfd, int backlog); |
accept | int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
send | ssize_t send(int sockfd, const void *buf, size_t len, int flags); |
recv | ssize_t recv(int sockfd, void *buf, size_t len, int flags); |
close | int close(int socketfd) |
可以通过strace python3 server.py命令跟踪这个程序所使用的系统调用
在输出的结果中有这么一段:
这个程序使用的系统调用对应上面的表格中的Linux Socket API