I finally tested it today. Socket communication in PHP, first look at the schematic diagram
Here you can see clearly where the socket is in the network model, as well as the image of the port
We are programmed in this order, please look at the figure below
So we write the program, first look at the server side Save as serversocket.php
<?php
set_time_limit (0);
$host = "localhost";
$port =1000;
Create a connection
$socket =socket_create (af_inet,sock_stream,sol_tcp) or Die ("Cannot create socket\n");
Bind the socket to the port
$result =socket_bind ($socket, $host, $port) or Die ("cannot bind port to socket\n");
Start listening on this port
$result =socket_listen ($socket, 4) or die ("could not set up socket listen\n");
Accept the connection, and another socket to process
the communication $msgsock =socket_accept ($socket) or Die ("cannot accept incoming connection\n");
if ($msgsock) {
echo date ("y-m-d h:i:s D a");
}
Read the information sent over by the client
$input =socket_read ($msgsock, 1024) or die ("Cannot read input\n");
$input =trim ($input);
$output =strrev ($input). " The order is reversed. \ n ";
The received information is processed and then returned to the client
socket_write ($msgsock, $output, strlen ($output)) or Die ("Cannot write");
Close the socket connection
socket_close ($msgsock);
Socket_close ($socket);
? >
Then look at the client's code saved as client.php
<?php
set_time_limit (0);
$host = "localhost";
$port =1000;
Create a socket
$socket =socket_create (af_inet,sock_stream,sol_tcp) or Die ("Cannot create socket\n");
$conn =socket_connect ($socket, $host, $port) or Die ("Cannot connect server\n");
if ($conn) {echo "Client connect ok!";}
Socket_write ($socket, "Hello world!") Or Die ("Cannot write data\n");
$buffer =socket_read ($socket, 1024,php_normal_read);
if ($buffer) {
echo "response was:". $buffer. " \ n ";
}
Socket_close ($socket);
? >
Note: 1 Test socket must be determined if you open the socket extension library, if not please go to php.ini to find Extension_dir=php_sockets.dll, put the semicolon to the front Off
2 when the test, in the browser input http://localhost/serverSocket.php and http://localhost/client.php, now the problem is coming, in the end first run Which, because the client wants to respond, so we run the server and run the client, so you can see the results.
Socket Function Reference
Socket_accept () accepts a socket connection
Socket_bind () binds the socket to an IP address and port
Socket_clear_error () Clears the socket error or the last error code
Socket_close () closes a socket resource
Socket_connect () Start a socket connection
Socket_create_listen () opens a socket listener on the specified port
Socket_create_pair () produces a pair of no differential sockets into an array
Socket_create () produces a socket, which is equivalent to generating a socket data structure
Socket_get_option () Get socket option
Socket_getpeername () Gets the IP address of a remote similar host
Socket_getsockname () Gets the IP address of the local socket
Socket_iovec_add () adds a new vector to a scatter/aggregate array
Socket_iovec_alloc () This function creates a IOVEC data structure capable of sending and receiving read and write
Socket_iovec_delete () deletes an assigned Iovec
Socket_iovec_fetch () returns the data for the specified Iovec resource
Socket_iovec_free () frees a Iovec resource
Socket_iovec_set () sets 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 () reads the specified length of data
SOCKET_READV () reads data coming from a scatter/aggregate array
SOCKET_RECV () end data from socket to cache
Socket_recvfrom () accepts data from the specified socket and, if not specified, the default current socket
Socket_recvmsg () received a message from Iovec
Socket_select () multi-channel selection
Socket_send () This function sends data to the connected socket
SOCKET_SENDMSG () Send message to socket
Socket_sendto () sends a message to the socket at the specified address
Socket_set_block () is set to block mode in the socket
Socket_set_nonblock () socket is set to not 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