PHP/Java 套接字通信不工作
PHP/Java socket communication not working
我已经被以下问题困扰了两天:我写了一个 java 套接字服务器,它可以接收数据并将数据发送到托管在 'localhost:64005' 的套接字。我可以通过 php 连接到它并发送或接收消息。但是我无法发送然后收到答复。我已将问题追溯到我编写的 php 脚本。
<?php
class socketCommunication{
protected $PK;
protected $ip = '127.0.0.1';
protected $port = 64005;
protected $socket;
public $result;
function __construct($key){
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$this->result = socket_connect($this->socket, $this->ip, $this->port) or die("Could not connect to server\n");
$this->PK = $key;
$this->sendSocket();
}
function getResponse(){
//$input = socket_read($this->socket, 1024) or die("Could not read");
$bytes = socket_recv($this->socket, $buf, 2048, MSG_WAITALL);
return $buf;
}
function sendSocket(){
$len = strlen($this->PK);
socket_send ($this->socket, $this->PK, $len, 0);
}
}
?>
<?php
//include("/mysql/RandomQuery.php");
include("/java/socketCommunication.php");
$object2 = new socketCommunication(100001);
echo $object2->getResponse();
?>
java套接字服务器:
package Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.Semaphore;
import Database.Reader;
public class Receive implements Runnable {
Semaphore semaphore;
private Socket connection;
String result = "default";
public Receive(Socket conn, Semaphore sem){
this.connection = conn;
this.semaphore = sem;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(connection.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
String userInput;
while ((userInput = in.readLine()) != null) {
System.out.println(userInput);
Reader reader = new Reader(Integer.parseInt(userInput));
this.result = reader.getResult();
System.out.println(result);
out.println(result);
break;
}
connection.close();
in.close();
System.out.println("input is closed");
semaphore.release();
} catch (IOException | InterruptedException e) {e.printStackTrace();}
}
}
只要我在 php 中同时调用 sendSocket 和 getResponse 方法,页面就会无限加载。但是,如果我单独调用 sendSocket 或 getResponse(在更改 java 套接字后它不会等待输入)方法,它们就可以正常工作。
我做错了什么?
亲切的问候。
参见:http://php.net/manual/en/function.socket-recv.php
MSG_WAITALL 标志将阻塞,直到接收到缓冲区的完整长度。您指定为 2048 字节的数据。
我已经被以下问题困扰了两天:我写了一个 java 套接字服务器,它可以接收数据并将数据发送到托管在 'localhost:64005' 的套接字。我可以通过 php 连接到它并发送或接收消息。但是我无法发送然后收到答复。我已将问题追溯到我编写的 php 脚本。
<?php
class socketCommunication{
protected $PK;
protected $ip = '127.0.0.1';
protected $port = 64005;
protected $socket;
public $result;
function __construct($key){
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$this->result = socket_connect($this->socket, $this->ip, $this->port) or die("Could not connect to server\n");
$this->PK = $key;
$this->sendSocket();
}
function getResponse(){
//$input = socket_read($this->socket, 1024) or die("Could not read");
$bytes = socket_recv($this->socket, $buf, 2048, MSG_WAITALL);
return $buf;
}
function sendSocket(){
$len = strlen($this->PK);
socket_send ($this->socket, $this->PK, $len, 0);
}
}
?>
<?php
//include("/mysql/RandomQuery.php");
include("/java/socketCommunication.php");
$object2 = new socketCommunication(100001);
echo $object2->getResponse();
?>
java套接字服务器:
package Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.Semaphore;
import Database.Reader;
public class Receive implements Runnable {
Semaphore semaphore;
private Socket connection;
String result = "default";
public Receive(Socket conn, Semaphore sem){
this.connection = conn;
this.semaphore = sem;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(connection.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
String userInput;
while ((userInput = in.readLine()) != null) {
System.out.println(userInput);
Reader reader = new Reader(Integer.parseInt(userInput));
this.result = reader.getResult();
System.out.println(result);
out.println(result);
break;
}
connection.close();
in.close();
System.out.println("input is closed");
semaphore.release();
} catch (IOException | InterruptedException e) {e.printStackTrace();}
}
}
只要我在 php 中同时调用 sendSocket 和 getResponse 方法,页面就会无限加载。但是,如果我单独调用 sendSocket 或 getResponse(在更改 java 套接字后它不会等待输入)方法,它们就可以正常工作。
我做错了什么?
亲切的问候。
参见:http://php.net/manual/en/function.socket-recv.php
MSG_WAITALL 标志将阻塞,直到接收到缓冲区的完整长度。您指定为 2048 字节的数据。