Basic principles of php socket|server server.php|client client.php
Release: 2016-07-25 08:47:42
Original
1089 people have browsed it
Basic principles and basic usage of php socket: Server: Create a socket=》Bind this socket to the specified IP and port=》Listen to all link requests of this socket=》Respond or return data Client :Create a socket socket=》Connect the socket that needs to be requested=》Send data to the socket=》Read the response data of the socket
(~_~) Please correct any inappropriate understanding~~~
- Server.php
-
-
-
- $host = '192.168.0.10';
- $port = 88888;
-
- if($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)){
- echo " socket create success.n";
- }
-
- if($bind = socket_bind($socket, $host, $port)){
- echo "socket bind success.n";
- }
-
- if($listen = socket_listen( $socket)){
- echo "socket listening...n";
- }
-
- if($connect = socket_accept($socket)){
- echo "socket accept a connect success.n";
- }
-
- // ================================================== =//
- socket_write($connect, 'I am from Server.php at DateTime:'.date('Y-m-d H:i:s')."n");
- //========= ===========Send to client==================================== ===============//
-
- //============================== =====================//
- $read = socket_read($connect, 1024);
- echo $read;
- //======= =====Accept messages from the client============================///
-
-
- //while($connect = socket_accept($socket)){
- // $read = socket_read($connect, 1024);
- // echo $read;
- //}
- //Loop accept
-
Copy code
- Client.php
-
-
-
- $host = '192.168.0.10';
- $port = 88888;
-
- if($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)){
- echo " socket create success.n";
- }
- socket_connect($socket, $host, $port);
-
- //======================== ===============================//
- socket_write($socket, 'I am from client.php at DateTime:' .date('Y-m-d H:i:s')."n");
- //==================Send a request to the server====== ==========================================//
-
- //== ================================================== ===//
- $read = socket_read($socket, 1024);
- echo $read;
- //================Accept the return data from the server====== ===================//
-
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31