PHP socket programming is more difficult to understand, but we just understand the socket of the relationship between several functions, as well as the role they play, then it should not be difficult to understand, in my opinion, socket programming, in fact, is to establish a Network service client and server , which is the same as the MySQL client and server, you just have to understand what the MySQL client and server are all about, and you should be able to understand what I'm going to say.
About the socket programming involved in the network protocol, what TCP Ah, UDP ah, what socket three times handshake, and so on, these network protocols online have a very detailed explanation, here do not say, only a socket to set up the process diagram of the socket to let you see:
How does the socket establish a connection? As mentioned above, the process of establishing a connection is essentially the same as the connection between the client and the server of MySQL. And it is different from MySQL, MySQL's server and client have been for us to edit, we just need to apply on the line. But, at the critical moment, the socket has nothing to offer us, and the only thing that comes to us is: dozens of socket functions.
Reference socket according to official document: http://php.net/manual/zh/book.sockets.php
The 8 main functions of PHP Socket programming are:
- socket_create-Create a socket (communication node) role: Create a socket socket, plainly speaking, is a network data stream. Return value: A socket, or FALSE, parameter error issue e_warning warning Socket_create creates and returns a socket, also known as a communication node. A typical network connection consists of 2 sockets, one running on the client and the other running on the server side.
- socket_bind-binding names to sockets
- socket_connect-opening a socket connection returns TRUE or False
- socket_listen-listens on a socket, the return value is TRUE or False
- socket_accept-receives the resource information for the socket, successfully returns the information resource for the socket, and fails to false
- socket_read-function: Reads the resource information of the socket, returns the value: successfully converted a socket resource to a string information, failed to false
- socket_send-sending data
- socket_write-function: Writes data to the return value in the socket: the byte length of the successfully returned string, failed to False
- socket_close-Closing a socket resource
PHP Socket Programming 9 main functions of the use of the test case