This article describes the method of PHP sending and receiving data to the socket server. Share it for your reference. The details are as follows:
In PHP to send data to other programs, you need to use the socket function of php to use as an example. Let’s take a brief look at an example, and the code is as follows:
@host(string) socket server IP
@post(int) port
@str(string) Data to be sent
@back 1|0 Is there any data returned on the socket side
Return 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 read, which can be used to limit the size of data fetched from the client.
Introduction to sock function
Function name | describe |
socket_accept() | Accept a Socket connection |
socket_bind() | Bind socket to an IP address and port |
socket_clear_error() | Clear socket error or last error code |
socket_close() | Close 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 sockets with no difference into an array |
socket_create() | Generating a socket is equivalent to generating a socket's data structure |
socket_get_option() | Get socket options |
socket_getpeername() | Get the IP address of a remote host-like host |
socket_getsockname() | Get 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, receive, read and write |
socket_iovec_delete() | Delete an already assigned iovec |
socket_iovec_fetch() | Returns the data of the specified iovec resource |
socket_iovec_free() | Free an iovec resource |
socket_iovec_set() | Set the new data value of iovec |
socket_last_error() | Get the last error code of the current socket |
socket_listen() | Listen to all connections from the specified socket |
socket_read() | Read data of a specified length |
socket_readv() | Read data from a scattered/aggregated array |
socket_recv() | End data from socket to cache |
socket_recvfrom() | Accept data from the specified socket, if not specified, the default current socket is |
socket_recvmsg() | Accept messages from iovec |
socket_select() | Multiple choice |
socket_send() | This function sends data to the connected socket |
socket_sendmsg() | Send a message to the socket |
socket_sendto() | Send a message to the socket at the specified address |
socket_set_block() | Set as 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 read, write, or specify socket |
socket_strerror() | Returns the detailed error of the specified error number |
socket_write() | Write data to socket cache |
socket_writev() | Write data to scatter/aggregation array |
Notice:The socket_read function will read the shell client data until it encounters n, t or characters. PHP scripts regard these characters as input ending characters.
I hope this article will be helpful to everyone's PHP programming.