所有的程式語言都提供了實作伺服器和客戶端通訊的機制。根據這種機制,應用程式使伺服器和客戶端能夠在它們之間交換資料。與其他程式語言類似,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中文網其他相關文章!