In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "php socket establishment process is how", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "php socket establishment socket process is how" it!
How does socket establish a connection? As mentioned above, the process of establishing a connection is essentially the same as the connection between the client and server of mysql. Unlike mysql, the server and client of mysql have been edited for us, and all we have to do is apply it. But when the crunch comes, socket doesn't give us anything, the only thing it gives us is: dozens of socket functions.
The implication is that socket programming requires us to create our own server and client, that is to say, ``socket programming. this means that we need to build a server and client application similar to mysql.
Speaking of which, I would like to ask, do you think this socket is a headache? It neither sets up a server, nor sets up a client for us to apply, so we have to apply socket functions and create our own network protocol socket application. Isn't that a headache for you? There's nothing you can do about headaches. If you need your own app, you still have to deal with socket. Ha ha, this is just beside the point, do not say much, let's get to the point.
Before you are fooled by socket programming, let me show you a few key functions of socket and explain their respective functions. Otherwise, if people who have no basic knowledge of socket programming see it, I'm afraid you will skip this article decisively and have a phobia of socket since then. Ha ha, said more.
The key function of socket 1:
Socket_create ($net parameter 1) parameter 2) parameter 3)
Purpose: create a socket socket, which, to put it bluntly, is a network data stream.
Return value: a socket, or false, with the wrong parameter issuing an E_WARNING warning
Php's online manual makes it even clearer:
Socket_create creates and returns a socket, also known as a communication node. A typical network connection consists of two sockets, one running on the client side and the other on the server side.
The above sentence is copied from the php online manual. See, does this mean exactly the same as the client and server I mentioned over and over again? He he.
Parameter 1 is: network protocol
What are the network protocols? Its options are as follows:
AF_INET: IPv4 network protocol. This protocol is available for both TCP and UDP. It's usually used, you know.
AF_INET6: IPv6 network protocol. This protocol is available for both TCP and UDP.
AF_UNIX: local communication protocol. IPC (interprocess communication) with high performance and low cost.
Parameter 2: socket stream, options are:
SOCK_STREAMSOCK_DGRAMSOCK_SEQPACKETSOCK_RAWSOCK_RDM .
Only the first two are explained here:
SOCK_STREAMTCP protocol sockets.
SOCK_DGRAMUDP protocol sockets.
Parameter 3:protocol protocol. Options are:
SOL_TCP: TCP protocol.
SOL_UDP: UDP protocol.
As you can see here, the second parameter of the socket_create function is associated with the third parameter.
For example, if your first parameter applies the IPv4 protocol: AF_INET, then the second parameter applies the TCP socket: SOCK_STREAM
Then the third parameter must use SOL_TCP, which should be easy to understand.
TCP protocol sockets, of course, can only use the TCP protocol, right? If you use UDP sockets, then I won't say how to choose the third parameter, hehe, you know.
Key function 2:
Socket_connect ($socket parameter 1 _
Function: connect a socket, and the return value is true or false
The function returns the value of the parameter 1:socket_create
Parameter 2:ip address
Parameter 3: Port number
Key function 3:
Socket_bind ($socket parameter 1 _
Function: bind a socket. The returned value is true or false
The function returns the value of the parameter 1:socket_create
Parameter 2:ip address
Parameter 3: Port number
Key function 4:
Socket_listen ($socket parameter 1)
Function: listens to a socket and the return value is true or false
The function returns the value of the parameter 1:socket_create
Parameter 2: maximum number of monitoring sockets
Key function 5:
Socket_accept ($socket)
Function: receive the resource information of the socket and return the information resource of the socket successfully. The failure is false.
Parameter: the function return value of socket_create
Key function 6:
Socket_read ($socket parameter 1 is the exact length parameter 2)
Function: read the resource information of the socket
Return value: successfully converts the resource of the socket into string information, and fails to false
The function returns the value of the parameter 1:socket_create or socket_accept
Parameter 2: the length of the string read
Key function 7:
Socket_write ($socket parameter 1, description msg parameter 2, description strlen parameter 3)
Function: write data to the socket
Return value: the byte length of the string was returned successfully. The failure is false.
The function returns the value of the parameter 1:socket_create or socket_accept
Parameter 2: string
Parameter 3: the length of the string
Key function 8:
Socket_close ($socket)
Function: turn off sockets
Return value: true for success and false for failure
Parameter: the function return value of socket_create or socket_accept
These eight functions are the core functions of socket. Here are two more important functions.
Socket_last_error ($socket). The parameter is the return value of socket_create, which is used to get the last error code of the socket and return the value socket code.
Socket_strerror ($code). The parameter is the return value of the socket_last_error function, and the string information of code is obtained. The return value is the error message of the socket.
These two functions are still very important in socket programming. When writing socket programming, I think you still have to use them, especially for beginners, which can be used as debugging.
The following is the code, pay attention, please read my comments carefully, comments are very important, important things to shout three times, hehe.
Server script, D:\ vhost\ test\ socket\ server_socket.php
`
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.