PHP sends and receives data from the socket server. PHP sends and receives data from the socket server. this example describes how PHP sends and receives data from the socket server. Share it with you for your reference. The details are as follows: In PHP, the method for sending and receiving data from PHP to the socket server is as follows:
This example describes how PHP sends and receives data from a socket server. 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:
/* Socket send and receive data @ host (string) socket server IP @ post (int) Port @ str (string) returned data @ back 1 | 0 socket returns true if data exists | 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.
In this example, PHP sends and receives data from the socket server. Share it with you for your reference. The details are as follows: In PHP, you need...