Home  >  Article  >  Backend Development  >  How to use PHP built-in functions for network programming?

How to use PHP built-in functions for network programming?

WBOY
WBOYOriginal
2024-04-23 08:27:01902browse

This article explores the steps for network programming using PHP's built-in functions, including creating a socket, binding a socket, listening to a socket, accepting client connections, sending and receiving data, and closing the socket.

如何使用 PHP 内置函数进行网络编程?

Using PHP built-in functions: Network Programming

PHP provides a series of built-in functions that allow you to easily perform network programming. This article explores practical examples of using these functions to create and use network sockets.

Create a network socket

socket_create() Function is used to create a network socket. It takes three parameters:

  • $domain - the domain of the socket (e.g. AF_INET for IPv4)
  • $type - The type of socket (e.g. SOCK_STREAM for TCP)
  • $protocol - The protocol to use (for TCP, usually 0)
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$socket) {
    die("socket_create() 失败: " . socket_strerror(socket_last_error()));
}

Bind network socket

socket_bind() Function Will the socket Bind to a specific address and port. You need to provide the following parameters:

  • $socket - the socket to bind
  • $address - the socket to bind to IP address
  • $port - Port to bind
$address = "127.0.0.1";
$port = 8080;
if (!socket_bind($socket, $address, $port)) {
    die("socket_bind() 失败: " . socket_strerror(socket_last_error()));
}

Listening network socket

socket_listen () Function Set the socket to listening state. It requires supplying the following parameters:

  • $socket - The socket to listen on
  • $backlog - The maximum queued number in the queue Number of connections
$backlog = 5;
if (!socket_listen($socket, $backlog)) {
    die("socket_listen() 失败: " . socket_strerror(socket_last_error()));
}

Accept client connections

socket_accept() Function will block until the client connection arrives. It returns a new socket representing the connection.

$client_socket = socket_accept($socket);
if (!$client_socket) {
    die("socket_accept() 失败: " . socket_strerror(socket_last_error()));
}

Sending and receiving data

socket_send() and socket_recv() functions are used to send over sockets and receive data.

Send data:

$data = "Hello, client!";
if (!socket_send($client_socket, $data, strlen($data), 0)) {
    die("socket_send() 失败: " . socket_strerror(socket_last_error()));
}

Receive data:

$buffer = socket_recv($client_socket, 1024, 0);
if (!$buffer) {
    die("socket_recv() 失败: " . socket_strerror(socket_last_error()));
}

Close network socket

In Once you are done with the network socket, you should close it.

Close client socket:

socket_close($client_socket);

Close server socket:

socket_close($socket);

The above is the detailed content of How to use PHP built-in functions for network programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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