私は、TCPサーバとして動作するJavaアプリケーションとクライアントとして動作するPHPスクリプトの間の単一のポートに簡単なTCP接続を設定しようとしています。単純なJava TCPサーバとPHPクライアントの問題
以下のコードをそれぞれ掲載しますが、問題は次のとおりです。 Javaサーバーに接続してデータを送信できます。私はそのデータを入手して印刷することができます。私の問題は、PHPサーバーに返信を返そうとする際に発生します。
「echo socket_read($ socket、14、PHP_NORMAL_READ);」の最後の行をコメントアウトすると、データはJavaサーバーに正常に取得されます。その行を追加すると、データはJavaサーバーに到達しません。
私の問題は、私がJavaからデータを送信しているか、サーバーからPHPでデータを取得しようとしているかと何らかの関係があると想定しています。
これは私が本当に困っています、どんな助けも大歓迎です!
のJavaサーバー:
protected ServerSocket socket;
protected final int port = 9005;
protected Socket connection;
protected String command = new String();
protected String responseString = new String();
socket = new ServerSocket(port);
while(true)
{
// open socket
connection = socket.accept();
// get input reader
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
BufferedReader input = new BufferedReader(inputStream);
// get output handler
DataOutputStream response = new DataOutputStream(connection.getOutputStream());
// get input
command = input.readLine();
// process input
Logger.log("Command: " + command);
responseString = command + " MC2 It Works!";
// send response
response.writeBytes(responseString);
response.flush();
response.close();
}
PHPクライアント:
$address = 'example.com'; // obviously not the address I am using
$port = 9005;
$message = "Test";
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port)
if(socket_send($socket, $message, strlen($message), MSG_EOF) != FALSE)
{
echo socket_read($socket, 14, PHP_NORMAL_READ);
}
responseString =コマンド+作る+ "\ rを\ n" は "それは作品MC2を!"。 – clevertension
私は、ソケットがあなたのデータの送信を完了したことを知っている行のEOFを追加する必要があると思います。 – clevertension
はい、間違いなく問題の一部でした。私は質問を提出してから約20分後にそれをすべて理解しました。人生もそうです。 – KayoticSully