php Javasocket侦听器问题?
我已经在这上面呆了一段时间了。我试图用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()将其输出。信息(数据)
此外,我是否需要在客户端或服务器上使用PHP向前移植25566?它们都以相同的外部IP托管在我的本地网络上
# 1 楼答案
这可能是因为循环从未停止:
您可以使用我为java服务器和客户端制作的类:
聊天室
聊天服务器线程
聊天接受者