In real-world development, if you want to create a socket-based application, you need to learn more about how to operate the socket, if you like to understand and skillfully use these methods, you need to first understand the various socket functions in PHP. In the previous section, we described in detail what the socket in PHP is. Here is a description of the socket function in PHP. There are dozens of of socket functions in PHP, here are some of the main socket functions to introduce.
Their syntax format parameters are as follows:
1. socket_create
socket_create (int $domain, int $type, int $protocol)
This function is used to create a socket, which has three parameters, and the return value is a handle (resource).
$DOMAIN Specifies the communication protocol family to use when creating the socket, with an optional value of:
Af_inet: IPV4-based Internet Protocol
AF_INET6: IPV6-based Internet Protocol
Af_unix:unix Local Communication protocol
$TYPE Specifies the type of interaction for the socket communication, and its optional value is:
Sock_stream: Provides a serialized, reliable, full-duplex, connection-based byte stream that supports TCP
SOCK_DGRAM: Provides datagram-free, non-connected, fixed maximum-length, auto-addressing transmission, supports UDP
Sock_seqpacket: Provides serialized, reliable, dual-channel, connection-based datagram transmission
Sock_raw: Provides the original network access protocol to manually build sockets of special protocol types that support ICMP requests (such as ping)
SOCK_RDM: Provides reliable datagram transmission with no guarantee of order
$PROTOCOL specifies which specific transport protocol the socket uses, including ICMP, UDP, TCP, constant SOL_UDP for UDP, and constant sol_tcp for constant TCP.
2. Socket_bind
Socket_bind (Resource $socket, string $address [, int $port = 0])
This function is used to bind the IP address and port to a handle created by Socket_create, with three parameters, which returns a Boolean value.
$socket is a required parameter that represents the handle created by the Socket_create function
$address is a required parameter, which represents the IP address to bind to
The $port is an optional parameter that represents the port number to be bound, which is used to listen for the socket connection, and when the first parameter of the Socket_create function is af_inet, this parameter needs to be specified.
3. Socket_listen
Socket_listen (Resource $socket [, int $backlog = 0])
This function is used to listen for incoming socket connections only if the socket's interaction type is sock_stream or Sock_seqpacket
, it has two parameters and returns a Boolean value.
$socket is a required parameter that represents the handle created by the Socket_create function (and the host is already bound)
$backlog is an optional parameter that represents the maximum number of connections waiting to be processed in the queue (allow backlogs).
4. Socket_set_block
Socket_set_block (Resource $socket)
This function is used to set the socket handle to block mode with only one required parameter, which returns a Boolean value. It can convert a non-blocking mode socket into a blocking mode.
When you perform an operation (receive, send, connect, accept, and so on) in a socket in a blocking mode, the script pauses execution until it receives a signal or it completes the operation.
$socket is a required parameter that represents a valid socket handle (created by socket_create or socket_accept).
Explain the difference between blocking mode and nonblocking mode :
Non-blocking means that a function operation does not block the current thread until the result is immediately available and returns immediately. Blocking means not to come back after the completion of the work, you must get the response from the other side before continuing the next operation. Especially when the user is relatively long, it is necessary to set up non-blocking. In the case of blocking mode, if two clients are connected at the same time, and the server is processing a client request, the request of the other client will be blocked, and the request of the latter client will not be answered until the previous client has finished processing it.
5. Socket_write
Socket_write (Resource $socket, string $buffer [, int $length = 0])
This function is used to write buffered data of a specified size to the socket, with three parameters, which returns the number of bytes written to the data.
$socket is a required parameter that represents a valid socket handle.
$buffer is a required parameter that specifies the string data to write.
$length is an optional parameter that specifies the number of bytes of data to be written in the socket in turn, and silently intercepts the length of bytes to $buffer if its value is greater than the number of bytes $buffer.
6. Socket_read
Socket_read (Resource $socket, int $length [, int $type = Php_binary_read])
This function is used to read data of the specified byte length from the socket, with three parameters that return the read string data.
$socket is a required parameter that represents a valid socket handle.
$length is a required parameter that specifies the length of bytes read.
The $type is an optional parameter, the default value is Php_binary_read, which is to safely read the binary data, and the other optional value is Php_normal_read, which indicates that the read is stopped when \ R or \ n is encountered.
7. Pfsockopen
Pfsockopen (String $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, Float $timeout = Ini_get ("def Ault_socket_timeout ")]]
This function is used to implement a persistent socket connection, that is, a long connection, which returns a handle. It differs from Fsockopen in that the connection established by Pfsockopen is not broken after the script has been executed.
8. Socket_set_option
Socket_set_option (Resource$socket, Int$level, Int$optname, Mixed$optval)
This function is used to set the socket control options, with four parameters, which returns a Boolean value.
$socket is a required parameter that represents a valid socket handle.
$level is a required parameter, specifying the protocol level at which option works, typically taking a constant sol_socket.
$optname is a required parameter that specifies the name of the option to control.
$optval is a required parameter, specifying the value of the option.
9. Socket_last_error
Socket_last_error ([Resource$socket])
This function is used to get the last error code generated by any socket function, and the return value is integer type.
Ten. Socket_strerror
Socket_strerror (int $errno)
The function is used to get the error description represented by the error code, and the return value is a string.
All of the above functions are in PHP for the socket, using these functions, you must open your socket, if you do not open, please edit your php.ini file, remove the following line before the comment:
Extension=php_sockets.dll
If you don't know if your socket is open, you can use the Phpinfo () function to determine if the socket is open.
Here's how these functions are used by creating an example of a server and a client:
Server-side
<?php//Ensure that the client is connected without time-out set_time_limit (0); $ip = ' 127.0.0.1 '; $port = 1935;/* +-------------------------------* @ Socket communication entire process +-------------------------------* @socket_create * @socket_bind * @socket_listen * @socket_accep T * @socket_read * @socket_write * @socket_close +--------------------------------*//*----------------The following actions are Manual-------------------*/if (($sock = Socket_create (af_inet,sock_stream,sol_tcp)) < 0) {echo "Socket_create () failed The reason for this is: ". Socket_strerror ($sock)." \ n ";} if ($ret = Socket_bind ($sock, $ip, $port)) < 0) {echo "Socket_bind () failed due to:". Socket_strerror ($ret). " \ n ";} if ($ret = Socket_listen ($sock, 4)) < 0) {echo "Socket_listen () failed due to:". Socket_strerror ($ret). " \ n ";} $count = 0;do {if ($msgsock = socket_accept ($sock)) < 0) {echo "socket_accept () Failed:reason:". Socket_strerror ($msgsock). "\ n"; Break } else {//sent to client $msg = "Test succeeded! \ n "; Socket_write ($msgsock, $msg, strlen ($MSG)); echo "The test was successful AH \ n"; $buf = Socket_read ($msgsock, 8192); $talkback = "Info received: $BUF \ n"; Echo $talkback; if (+ + $count >= 5) {break; }; }//echo $buf; Socket_close ($msgsock);} while (true); Socket_close ($sock);? >
2. Client
<?phperror_reporting (E_all); set_time_limit (0); echo "
"Related tutorials Recommended"
1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"
2. PHP programming from getting started to mastering the full set of tutorials