Socket communication principle is not said here, its use is more extensive, we can use the socket to do an API interface out, you can use the socket to achieve the communication between the two programs, we have to study how to implement socket communication in PHP.
Because the socket service side of the code to listen to the port, waiting to receive requests, so PHP in the socket service need to run the php file inside cmd.
If you want the PHP file to run in cmd, you need to set the following:
1. Add environment variables, named Php_home, the value of the PHP file installation directory under the. exe file address, such as D:\wamp\bin\php\php5.5.12\php.exe
2. Modify the value of the system variable path
Add PHP installed directories in the value of path: such as D:\wamp\bin\php\php5.5.12;
OK, here we have configured the environment variable, next we open cmd, want to run PHP files inside, such as aaa.php file, then we write this sentence:
PHP d:\wamp\www\aaa.php
Then press ENTER, OK, our php file in cmd run, output a sentence: Hello
This way the PHP file can be successfully run in cmd, then we look at how PHP to implement socket communication.
1.php made socket service end
The main function is to set the socket communication IP address and port number, listening port, there is a client connection, receive the connection request to receive data, processing and return data.
The code is as follows:
Ensure that the client is not timed out set_time_limit (0);
Set IP and port number $address = "127.0.0.1"; $port = 2048;
When debugging, you can change the port to test the program! /** * Create a socket * af_inet= is IPv4 if IPv6, then the parameter is Af_inet6 * Sock_stream is the TCP type of the socket, if UDP is the use of sock_dgram/$sock = Socke T_create (Af_inet, Sock_stream, sol_tcp) or Die ("socket_create () the reason for the failure is:". Socket_strerror (Socket_last_error ()).
"/n"); The reason for the blocking mode Socket_set_block ($sock) or Die ("Socket_set_block ()" Failure is: ". Socket_strerror (Socket_last_error ()).
"/n"); Binding to the socket port $result = Socket_bind ($sock, $address, $port) or Die ("Socket_bind ()) failed because of:". Socket_strerror (Socket_last_error ()).
"/n"); Start listening $result = Socket_listen ($sock, 4) or Die ("Socket_listen ()) failed because of:". Socket_strerror (Socket_last_error ()).
"/n"); echo "ok\nbinding the socket on $address: $port ...
";
echo "Ok\nnow ready to accept connections.\nlistening on the socket ... \ n"; Do {//Never stop the daemon//it receives the connection request and invokes a child connection socket to handle information between the client and server $msgsock = Socket_accept ($sock) or Die ("Socket_accep T () Failed:reason: ". Socket_strerror (Socket_last_error ()).
"/n");
Read client data echo "read client \ n"; The Socket_read function reads the client data until it encounters a \n,\t or a character.
The PHP script regards this character as the input terminator.
$buf = Socket_read ($msgsock, 8192);
echo "Received msg: $buf \ n";
The data transfer writes the return result to the client $msg = "Welcome \ n"; Socket_write ($msgsock, $msg, strlen ($msg)) or Die ("Socket_write () Failed:reason:". Socket_strerror (Socket_last_error ()). "
/n ");
Once the output is returned to the client, the parent/child socket should terminate the Socket_close ($msgsock) by Socket_close ($msgsock) function;
} while (true); Socket_close ($sock);
2. client file for socket service
The client is still set to access the server's IP address and port number (that is, the IP and ports in the previous step), and then create a socket connection, send data to the server, and receive the returned data.
Set_time_limit (0);
$host = "127.0.0.1";
$port = 2048;
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp) or die ("could not create socket\n");//Create a socket
$ Connection = Socket_connect ($socket, $host, $port) or die ("could not connet server\n"); Connection
Socket_write ($socket, "hello socket") or Die ("Write failed\n");//data transfer sends a message to the server while
($buff = @socket_rea D ($socket, 1024, php_normal_read)) {
echo ("Response was:"). $buff. "\ n");
}
Socket_close ($socket);
3. Run the service-side code inside CMD
Running successfully, already on the listening port ...
4. Run our client page inside the Web page to interact with the server data
Run up, the browser shows:
CMD inside the service side display:
This is a simple socket communication test, as to what the socket received data, how to process data, return what type of data, also need to use PHP to do logic.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.