This article illustrates how PHP sends and receives data from the socket server. Share to everyone for your reference. Specifically as follows:
In PHP to send data to other programs, you need to use the PHP socket function to the example, we look at a simple example, the code is as follows:
Copy Code code as follows:
/*socket send and receive data
@host (string) socket server IP
@post (int) port
@str (string) the data to send
@back 1|0 socket end has data return
Returns true|false| server-side 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 specifies the number of bytes to read, which you can use to limit the size of the data being fetched from the client.
Introduction to Sock functions
| The name of the function |
Describe |
| Socket_accept () |
Accept a socket connection |
| Socket_bind () |
Bind the socket to an IP address and port |
| Socket_clear_error () |
Clears the socket error or the 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 () |
Produces a pair of no different sockets into an array |
| Socket_create () |
Produces a socket that is equivalent to generating a socket's data structure |
| Socket_get_option () |
Get socket options |
| Socket_getpeername () |
To obtain an IP address for a remote-like host |
| Socket_getsockname () |
Get the IP address of the local socket |
| Socket_iovec_add () |
Add a new vector to a scatter/aggregate array |
| Socket_iovec_alloc () |
This function creates a IOVEC data structure that can send and receive read and write |
| Socket_iovec_delete () |
Delete an already allocated Iovec |
| Socket_iovec_fetch () |
Returns the data for the specified Iovec resource |
| Socket_iovec_free () |
Releasing a Iovec resource |
| Socket_iovec_set () |
Set the Iovec data new value |
| Socket_last_error () |
Gets the last error code for the current socket |
| Socket_listen () |
Listens for all connections by the specified socket |
| Socket_read () |
Read data of the specified length |
| SOCKET_READV () |
Read data coming from a scatter/aggregate array |
| SOCKET_RECV () |
End data to cache from socket |
| Socket_recvfrom () |
Accept data from the specified socket, or default current socket if not specified |
| Socket_recvmsg () |
Take the message from the Iovec. |
| Socket_select () |
Multi-channel selection |
| Socket_send () |
This function sends data to the connected socket |
| Socket_sendmsg () |
Send Message to Socket |
| Socket_sendto () |
A socket that sends a message to a specified address |
| Socket_set_block () |
Set to block mode in socket |
| Socket_set_nonblock () |
Socket is set to non block mode |
| Socket_set_option () |
Set socket options |
| Socket_shutdown () |
This function allows you to close a read, write, or specified socket |
| Socket_strerror () |
Returns a detailed error for the specified error number |
| Socket_write () |
Write data to the socket cache |
| Socket_writev () |
Write data to a scatter/aggregate array |
Note:the Socket_read function reads the shell-side data until it meets n,t or characters, and the PHP script takes these characters as input terminator.
I hope this article will help you with your PHP program design.