PHP 中的套接字編程

王林
發布: 2024-08-29 13:14:07
原創
984 人瀏覽過

所有的程式語言都提供了實作伺服器和客戶端通訊的機制。根據這種機制,應用程式使伺服器和客戶端能夠在它們之間交換資料。與其他程式語言類似,PHP也為我們提供了這種機制。套接字編程可以定義為將伺服器和客戶端作為應用程式的程式設計方法,必須在兩者之間建立連接以促進它們之間的通訊。就PHP而言,它也讓我們實作了socket程式設計的概念。在本文中,我們將學習如何使用 PHP 程式語言來實作此套接字程式設計。

廣告 該類別中的熱門課程 程式語言 - 專業化 | 54 課程系列 | 4 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Socket 類別方法

socket類別方法是讓我們實作socket程式設計的特殊函數。為了實現套接字程式設計的功能而必須編寫的程式使用預先定義的套接字函數。這些函數由在套接字程式設計中執行實際作用的語句組成。以下是一些套接字功能。

  • Socket_accept: 這是用於接受套接字連接的非常常見的套接字函數之一。此函數的主要作用是讓連線在請求命中時被接受。
  • Socket_addrinfo_bind: 此函數用於將提供的資訊加入套接字。接受到的信息必須分配給socket以方便其實現。
  • Socket_clear_error: 此函數用於清除套接字上的錯誤。除此之外,這個函數還可以清除最後一個程式碼的錯誤。
  • Socket_close: 顧名思義,此函數用於關閉屬於套接字的資源。
  • Socket_connect: 此方法用於建立套接字連線。在socket程式設計中,程式從建立連線開始,可以使用這個函數來完成。
  • Socket_create: 此方法與套接字的建立有關。使用此方法建立的套接字充當連接的端點。
  • Socket_create_listen:函數用於讓socket開啟接受連線的指定連接埠。顧名思義,它有助於打開套接字進行監聽。
  • Socket_create_pair: 此方法通常用在需要使用套接字程式設計的複雜部分的應用程式中。它有助於創建無法區分的套接字,並將它們儲存在陣列中。
  • Socket_get_option: 此方法用來取得套接字的選項。套接字由必須根據應用程式使用的多個選項組成。透過使用這個方法,我們可以獲得套接字具有的所有選項。
  • Socket_getsockname: 此方法用於查詢所選套接字的本機區域,作為回報,它可能會取得與主機/連接埠或 Unix 檔案系統路徑相關的詳細資訊。無論它得到什麼結果,完全取決於類型。

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 方法將被撤銷,並彈出其中給出的訊息。

Socket Server Example

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.

Conclusion – Socket Programming in PHP

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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!