无法使用PHP客户端从Java套接字服务器读取响应 [英] Cannot read response from Java socket server using PHP client

查看:175
本文介绍了无法使用PHP客户端从Java套接字服务器读取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java编写的服务器。它是一个简单的,当客户端连接时回显HELLO,然后回显客户端发送的任何回复。代码如下:

I have a server written in Java. Its a simple one that echoes "HELLO" when a client connects and then echoes back any reply the client sends. The code is below:

ServerSocket ss=new ServerSocket(2001);
while(true) {
  Socket s=ss.accept();
  BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
  PrintWriter out=new PrintWriter(s.getOutputStream(),true);
  out.println("HELLO");
  String msg=in.readLine();
  out.println(msg);
}



我有一个PHP脚本连接到服务器:

I have a PHP script that connects to the server:

<?php
  $socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname('tcp'));
  socket_connect($socket,'127.0.0.1',2001);
  $msg=socket_read($socket,10);
  echo socket_write($socket,'I am the client.');
  $msg=socket_read($socket,20);
  echo $msg;
?>

我从服务器收到HELLO消息,服务器也得到I是客户端消息,但PHP客户端没有得到回复。

I am getting the "HELLO" message from the server, and the server is also getting the "I am the client" message, but the PHP client is not getting the reply back. What am I doing wrong here?

推荐答案

in.readLine()

除非 socket_write 在PHP中隐式添加行终止符你需要自己这样做,以便Java端看到你已经写了一整行文本。

Unless socket_write in PHP implicitly adds a line terminator, you'll need to do so yourself, so that the Java side sees that you've written a complete line of text.

这篇关于无法使用PHP客户端从Java套接字服务器读取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆