我已经坚持了一段时间了。我正在尝试使用 PHP 将数据发送到 Java 服务器。当我在 Bukkit 中加载插件时,当我调用此函数时它会停止加载:
public void SocketListen()
{
String clientSentence;
String capitalizedSentence;
try
{
ServerSocket welcomeSocket = new ServerSocket(25566);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
catch(IOException e)
{
getLogger().severe(e.toString());
}
}
这告诉我我做错了什么。我的PHP是:
<?php
$host = "localhost";
$port = 25566;
$data = 'test\n';
if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error());
else
{
echo "Attempting to connect to '$host' on port '$port'...<br>";
if ( ($result = socket_connect($socket, $host, $port)) === FALSE )
echo "socket_connect() failed. Reason: ($result) " . socket_strerror(socket_last_error($socket));
else {
echo "Sending data...<br>";
socket_write($socket, $data, strlen($data));
echo "OK<br>";
echo "Reading response:<br>";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
}
socket_close($socket);
}
?>
我究竟做错了什么?
基本上,我只需要 PHP 服务器发送“Hello”然后 Java 客户端用 getLogger().info(data);
另外,我需要在客户端或服务器上使用 PHP 端口转发 25566 吗?它们都托管在我的本地网络上,具有相同的外部 IP。