ホームページ  >  記事  >  バックエンド開発  >  PHP でのソケット プログラミング

PHP でのソケット プログラミング

王林
王林オリジナル
2024-08-29 13:14:07991ブラウズ

すべてのプログラミング言語は、サーバーとクライアントの通信を実装するメカニズムを提供します。このメカニズムに従って、アプリケーションはサーバーとクライアントの間でデータを交換できるようにします。他のプログラミング言語と同様に、PHP もこのメカニズムを提供します。ソケット プログラミングは、サーバーとクライアントをアプリケーションとして持ち、両者間の通信を容易にするために両者の間に接続を確立する必要があるプログラミング アプローチとして定義できます。 PHP に関しては、ソケット プログラミングの概念も実装できます。この記事では、PHP プログラミング言語を使用してこのソケット プログラミングを実装する方法を学びます。

広告 このカテゴリーの人気コース プログラミング言語 - 専門分野 | 54 コース シリーズ | 4 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

ソケットクラスのメソッド

ソケット クラスのメソッドは、ソケット プログラミングを実装できる特別な関数です。ソケット プログラミングの機能を実現するために作成する必要があるプログラムは、事前定義されたソケット関数を使用します。これらの関数は、ソケット プログラミングで実際の役割を実行するステートメントで構成されます。以下はソケット関数の一部です。

  • Socket_accept: これは、ソケット接続を受け入れるために使用される非常に一般的なソケット関数の 1 つです。この関数の主な役割は、リクエストがヒットするたびに接続を受け入れられるようにすることです。
  • Socket_addrinfo_bind: この関数は、提供された情報をソケットに追加するために使用されます。受け入れられた情報は、実装を容易にするためにソケットに割り当てる必要があります。
  • Socket_clear_error: この関数は、ソケット上のエラーをクリアするために使用されます。それに加えて、この関数は最後のコードのエラーもクリアします。
  • Socket_close: 名前が示すように、この関数はソケットに属するリソースを閉じるために使用されます。
  • Socket_connect: このメソッドは、ソケット接続を作成するために使用されます。ソケットプログラミングでは、プログラムは接続の確立から始まります。これはこの関数を使用して実行できます。
  • Socket_create: このメソッドはソケットの作成に関係します。このメソッドを使用して作成されたソケットは、接続のエンドポイントとして機能します。
  • Socket_create_listen: この関数は、接続を受け入れる指定されたポートを開くソケットを取得するために使用されます。名前が示すように、リッスンするためにソケットを開くのに役立ちます。
  • Socket_create_pair: このメソッドは通常、ソケット プログラミングの複雑な部分を使用する必要があるアプリケーションで使用されます。これは、区別できないソケットの作成に役立ち、それらは配列に保存されます。
  • Socket_get_option: このメソッドは、ソケットのオプションを取得するために使用されます。ソケットは、アプリケーションに応じて使用する必要があるいくつかのオプションで構成されています。このメソッドを使用すると、ソケットが持つすべてのオプションを取得できます。
  • Socket_getsockname: このメソッドは、選択したソケットのローカル領域をクエリするために使用され、その結果、ホスト/ポートまたは Unix ファイルシステム パスに関連する詳細を取得できます。どのような結果が得られるかは、タイプに完全に依存します。

ソケットクライアントの例

このセクションでは、クライアント側のソケット プログラミングを実装するために使用されるコードについて説明します。以下で説明する例には、ソケット接続の作成に使用されるポストとホストの詳細が含まれています。接続が確立されると、いくつかのメッセージを交換し、サーバーからの応答を期待します。

<?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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。