PHP sends and receives data from the socket server. To send data to other programs in PHP, you need to use the socket function of php for instance, the following is a simple example of an instance code: Copy the code * socket sends and receives data @ host (stri needs to use the socket function of PHP to send data to other programs in php, let's take a look at an instance.
| 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
Socket functions
Function name description
Socket_accept () accepts a Socket connection
Socket_bind () binds the socket to an IP address and port.
Socket_clear_error () clears socket errors or the final error code
Socket_close () closes a socket resource
Socket_connect () starts a socket connection
Socket_create_listen () opens a socket listener on the specified port.
Socket_create_pair () generates a pair of identical sockets into an array.
Socket_create () generates a socket, which is equivalent to the data structure of a socket.
Socket_get_option () obtain the socket option
Socket_getpeername () obtains the IP address of a remote host similar to that of a remote host.
Socket_getsockname () gets 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 () deletes an allocated iovec
Socket_iovec_fetch () returns the data of the specified iovec resource.
Socket_iovec_free () releases an iovec resource.
Socket_iovec_set () sets the new value of iovec data
Socket_last_error () obtains the final error code of the current socket.
Socket_listen () listens to all connections from the specified socket
Socket_read () reads data of the specified length
Socket_readv () reads data from scattered/aggregate arrays
Socket_recv () ends data from the socket to the cache
Socket_recvfrom () accepts data from the specified socket. If no value is specified, the current socket is used by default.
Socket_recvmsg () receives messages from iovec
Socket_select () multi-path selection
Socket_send () this function sends data to the connected socket
Socket_sendmsg () sends a message to the socket
Socket_sendto () sends a message to the socket of the specified address
Socket_set_block () is set to block mode in socket
Set the socket in socket_set_nonblock () to non-block mode.
Socket_set_option () sets socket options
The socket_shutdown () function allows you to close the read, write, or specified socket
Socket_strerror () returns a detailed error of the specified error number
Socket_write () writes data to the socket cache
Socket_writev () writes data to a distributed/aggregate array
Note: The socket_read function keeps reading shell data until it meets n, t, or characters. the PHP script regards these characters as input Terminator.
The pipeline code is as follows/* socket sends and receives data @ host (stri...