我正在尝试在 PHP 套接字和 JAVA 套接字之间传输简单的消息。php 套接字成功发送数据并等待 Java 服务器响应。但另一方面,Java 服务器的套接字仍在等待来自 PHP 的消息。
这是Java代码:
ServerSocket s = new ServerSocket(4280);
Socket sock = s.accept();
System.out.println("Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
System.out.println("Reading");
String str = br.readLine();
System.out.println("Writing");
bw.write(str);
输出:
连接的
阅读
这是PHP代码:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "localhost", 4280);
socket_write($socket, "Hello");
echo socket_read($socket, 10);
socket_write($socket, "Lelo");
echo socket_read($socket, 10);
输出:
浏览器:等待本地主机