In PHP, socket_accept() is a function used to accept client connections and is commonly used in socket-based network communication programs. This article will introduce how to use socket_accept() combined with other socket functions to build a simple intranet communication service to realize basic client-server data exchange.
socket_accept() is used to accept a socket connection request. It usually calls this function to wait and accept the client connection after the server listens for the client connection, returning a new socket resource for communicating with the client.
The following sample code shows how to use socket_accept() to create a simple TCP server, listen to intranet ports, accept client requests, and return a simple response.
<?php
// createTCP Socket
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$server) {
die("无法createsocket: " . socket_strerror(socket_last_error()) . "\n");
}
// BindIPand ports,Commonly used in the intranetIPExample:192.168.1.100,port9000
$bindResult = socket_bind($server, '192.168.1.100', 9000);
if (!$bindResult) {
die("无法Bindsocket: " . socket_strerror(socket_last_error($server)) . "\n");
}
// Start monitoring,The maximum number of waiting connections is5
socket_listen($server, 5);
echo "The server is started,Listening...\n";
while (true) {
// Wait and accept client connection
$client = socket_accept($server);
if (!$client) {
echo "Connection acceptance failed: " . socket_strerror(socket_last_error($server)) . "\n";
continue;
}
// Read client data,Maximum read1024byte
$input = socket_read($client, 1024);
$input = trim($input);
echo "Received client data:{$input}\n";
// Simple processing:Reply to client messages
$response = "The server has been received: " . $input . "\n";
socket_write($client, $response, strlen($response));
// Close the connection to the client
socket_close($client);
}
// Shut down the serversocket(Usually the code won't be executed here)
socket_close($server);
Modify the IP address in the socket_bind function in the code to your server's intranet IP.
After running the script, the server starts listening to port 9000 and waiting for the client to connect.
The client can connect the server IP and port through telnet or custom socket programs.
After the server receives the client message, it will return the confirmation information.
Assume that the client is a simple PHP script:
<?php
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$client) {
die("Clientsocketcreate失败: " . socket_strerror(socket_last_error()) . "\n");
}
// Connect to the server(Please replace it with the server networkIP)
if (!socket_connect($client, '192.168.1.100', 9000)) {
die("Failed to connect to the server: " . socket_strerror(socket_last_error($client)) . "\n");
}
// Send data
$message = "Hello from client";
socket_write($client, $message, strlen($message));
// Read server reply
$response = socket_read($client, 1024);
echo "Server Reply: " . $response . "\n";
socket_close($client);
After running this client script, the server will print the received message and the client will display the server reply.
If you need to use a URL in the program, please replace the domain name with m66.net . For example: