This article mainly introduces the methods for sending and receiving data from PHP to the socket server, analyzes the methods for sending and receiving data from the socket, and introduces common socket functions, for more information about how PHP sends and receives data from a socket server, see the following example. Share it with you for your reference. The details are as follows:
To send data to other programs in PHP, you need to use the socket function of php for an instance. let's look at an example with the code as follows:
The code is as follows:
/* Send and receive data through socket
@ Host (string) socket server IP address
@ Post (int) Port
@ Str (string) the data to be sent
@ Back 1 | 0 indicates whether data is returned from the socket.
Returns true | false | server data
*/
Function sendSocketMsg ($ host, $ port, $ str, $ back = 0 ){
$ Socket = socket_create (AF_INET, SOCK_STREAM, 0 );
If ($ socket <0) return false;
$ Result = @ socket_connect ($ socket, $ host, $ port );
If ($ result = false) return false;
Socket_write ($ socket, $ str, strlen ($ str ));
If ($ back! = 0 ){
$ Input = socket_read ($ socket, 1024 );
Socket_close ($ socket );
Return $ input;
} Else {
Socket_close ($ socket );
Return true;
}
}
The second parameter of socker_read is used to specify the number of bytes to read. you can use it to limit the size of data obtained from the client.
Sock function introduction
Function name |
Description |
Socket_accept () |
Accept a Socket connection |
Socket_bind () |
Bind the socket to an IP address and port |
Socket_clear_error () |
Clear socket errors or the final error code |
Socket_close () |
Disables a socket resource. |
Socket_connect () |
Start a socket connection |
Socket_create_listen () |
Open a socket listener on the specified port |
Socket_create_pair () |
Generate a pair of identical sockets into an array |
Socket_create () |
Generate a socket, which is equivalent to the data structure of a socket. |
Socket_get_option () |
Obtain socket options |
Socket_getpeername () |
Obtain the IP address of a remote host. |
Socket_getsockname () |
Obtain the IP address of the local socket |
Socket_iovec_add () |
Add a new vector to a scattered/aggregated array |
Socket_iovec_alloc () |
This function creates an iovec data structure that can send and receive read/write data. |
Socket_iovec_delete () |
Delete an allocated iovec |
Socket_iovec_fetch () |
Returns the data of the specified iovec resource. |
Socket_iovec_free () |
Releases an iovec resource. |
Socket_iovec_set () |
Set the new value of iovec data |
Socket_last_error () |
Get the final error code of the current socket |
Socket_listen () |
Listen to all connections from the specified socket |
Socket_read () |
Read data of the specified length |
Socket_readv () |
Read data from scattered/aggregated arrays |
Socket_recv () |
End data from socket to cache |
Socket_recvfrom () |
Accept data from the specified socket. if not specified, the current socket is used by default. |
Socket_recvmsg () |
Receive messages from iovec |
Socket_select () |
Multiple choice |
Socket_send () |
This function sends data to the connected socket |
Socket_sendmsg () |
Send messages to socket |
Socket_sendto () |
Send a message to the socket of the specified address |
Socket_set_block () |
Set block mode in socket |
Socket_set_nonblock () |
Set to non-block mode in socket |
Socket_set_option () |
Set socket options |
Socket_shutdown () |
This function allows you to close the read, write, or specified socket |
Socket_strerror () |
Returns a detailed error of the specified error number. |
Socket_write () |
Write data to socket cache |
Socket_writev () |
Write data to scattered/aggregated arrays |
Note:The socket_read function reads the shell data until n, t, or character is met. the PHP script regards these characters as the input Terminator.
I hope this article will help you with php programming.