私はすでにソケットのチュートリアルを行ってきましたが、それは何を得ることができませんでした。私はソケットが何をしているのか、なぜそれが使われているのかを知りたい。これは私が参照したコードです。PHPのソケットとは何ですか?そして、どのような状態で私はソケット接続のために行く必要がありますか?
client.php
<?php
$host = "localhost";
$port = 1024;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
?>
server.php
<?php
// set some variables
$host = "localhost";
$port = 1024;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
ここで、サーバーコードとクライアントコードを入力する場所がわかりませんでした。通常、私たちはユーザ入力を取得しながらサーバコードを書いています。私はこれについて非常に混乱しています。誰でも助けてくれますか?事前に感謝します
私はそれがありますので、オフトピックとして、この質問を閉じるために投票しています。 –
それがある場合は、それがどこにあるかお勧めできますか?それは大きな助けになるでしょう。 – Sam
ありがとう@RyanVincent 2台のコンピュータがどのように通信するかを知るにはとても便利な記事です。しかし、彼らはソケットについて何も言わなかった。だから私を助けることができますか?あるいは、ソケットについて知っておくべきあらゆる記事と実装方法を教えてください。私は既にそこにチャットアプリケーションを開発しました。メッセージをあるユーザからデータベースに保存し、それを他のユーザに表示します。ページはajaxを使ってリフレッシュされます。だから私はソケットプログラミングを介してチャットを実行するためにinorderをしなければならない。あなたはその流れを説明できますか? – Sam