所有的编程语言都提供了实现服务器和客户端通信的机制。根据这种机制,应用程序使服务器和客户端能够在它们之间交换数据。与其他编程语言类似,PHP也为我们提供了这种机制。套接字编程可以定义为将服务器和客户端作为应用程序的编程方法,必须在两者之间建立连接以促进它们之间的通信。就PHP而言,它也让我们实现了socket编程的概念。在本文中,我们将学习如何使用 PHP 编程语言来实现此套接字编程。
广告 该类别中的热门课程 编程语言 - 专业化 | 54 课程系列 | 4 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
socket类方法是让我们实现socket编程的特殊函数。为了实现套接字编程的功能而必须编写的程序使用预定义的套接字函数。这些函数由在套接字编程中执行实际作用的语句组成。以下是一些套接字功能。
本节将看到用于实现客户端套接字编程的代码。下面提到的示例将包含用于创建套接字连接的帖子和主机详细信息。连接建立后,它会交换一些消息并期待服务器的响应。
<?php $port_number = 1230; $IPadress_host = "127.0.0.1"; $hello_msg= "This is server"; echo "Hitting the server :".$hello_msg; $socket_creation = socket_create(AF_INET, SOCK_STREAM, 0) or die("Unable to create connection with socket\n"); $server_connect = socket_connect($socket_creation, $IPadress_host , $port_number) or die("Unable to create connection with server\n"); socket_write($socket_creation, $hello_msg, strlen($hello_msg)) or die("Unable to send data to the server\n"); $server_connect = socket_read ($socket_creation, 1024) or die("Unable to read response from the server\n"); echo "Message from the server :".$server_connect; socket_close($socket_creation); ?>
在上面的示例中,程序尝试连接的端口号是 1230。主机的 IP 地址将是本地主机的 IP。如果有人愿意与远程服务器交互,他们可以提及服务器的 IP 地址。然后消息将被发送到服务器,并显示在响应页面上。稍后将处理套接字创建。在这个程序中,有一个适当的机制来使用 die 方法来处理错误。如果出现任何问题,在这种情况下,die 方法将被撤销,并弹出其中给出的消息。
The example detailed in this section will be having the PHP codes that will be leveraged to implement the socket programming at the server-side. The details of the IP and the port number used in the last example will remain the same in this example as well. This example’s main difference will make the core difference that separates it from the client-side socket programming language. Lets process to understand the PHP code for server-side socket programming.
<?php $port_number = 1230; $IPadress_host = "127.0.0.1"; set_time_limit(0); $socket_creation = socket_create(AF_INET, SOCK_STREAM, 0) or die("Unable to create socket\n");$socket_outcome = socket_bind($socket_creation, $IPadress_host , $port_number ) or die("Unable to bind to socket\n"); $socket_outcome = socket_listen($socket_creation, 3) or die("Unable to set up socket listener\n"); $socketAccept = socket_accept($socket_creation) or die("Unable to accept incoming connection\n"); $data = socket_read($socketAccept, 1024) or die("Unable to read input\n"); $data = trim($data); echo "Client Message : ".$data; $outcome = strrev($data) . "\n"; socket_write($socketAccept, $outcome, strlen ($outcome)) or die("Unable to write output\n"); socket_close($socketAccept); socket_close($socket_creation); ?>
In the above example, the program has been developed to work in the localhost. The IP address mentioned here belongs to the localhost, and the port number can run the TCP and UDP service on that. The initial step is always the creation of the socket, as it is something that will be used throughout the program. Later the socket has been bonded with the specified values, which will help in functioning. The methods used in this program have a predefined meaning that can be used for a specific purpose. Once everything goes well, the program will work accordingly and will close the socket connection eventually.
The socket programming language is used to let the application work on the server and the client model. This approach of programming lets us establish the connection between the server and the client so that the exchange of the data could be facilitated. To make the socket programming easy and convenient, PHP has provided predefined methods where all the methods have some unique tasks assigned to them.
以上是PHP 中的套接字编程的详细内容。更多信息请关注PHP中文网其他相关文章!