Java 套接字服务器在读取数据时挂起
Java Socket Server hangs while reading data
我有一个 PHP 文件与 Java 套接字服务器通信,当我发送数据时,我的 java 服务器卡住(挂起、冻结)在 inputLine = in.readLine()
.我调试了一下,发现只有在读取数据的时候才会出现这种情况。
这是我的 java 服务器方法:
public void start_echo_server(int port){
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "STARTING SOCKET LISTENER (echo)"));
int portNumber = port;
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
// accepted the connection
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "ACCEPTED"));
// in stream
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// outstream
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String final_line = sb.toString();
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "IN: " + final_line));
//String final_ret = parser.parse_message(final_line);
//main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "FINAL: " + final_ret));
out.println(final_line);
in.close();
out.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我的 PHP 文件:
<?php
if( isset($_POST['username']) )
{
$username = $_POST['username'];
parse($username);
}else{
echo "Missing parameters!";
exit();
}
function parse($username){
//Must be same with server
$host = "127.0.0.1";
$port = 59090;
// No Timeout
//Create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//Connect to the server
$result = socket_connect($sock, $host, $port) or die("Could not connect toserver\n");
$message = "player_online ". $username;
//Write to server socket
$len = strlen($message);
socket_write($sock, $message, $len) or die("SENDING ERROR ". $message ." \n");
//Read server respond message
$result = socket_read($sock, 1024) or die("RESPONSE ERROR ". $message ." \n");
echo "Reply From Server :".$result;
//Close the socket
socket_close($sock);
}
?>
问题是当我在 PHP 端执行 socket_write
(写入数据)时,但问题出在 java 行 while ((inputLine = in.readLine()) != null) {
。
非常感谢!
尝试将 while 条件更改为您控制的条件,作为概念证明,即在 1 分钟左右的时间内阅读,如果这让 ypu 解开,那么在 in.readline 时不要阅读,但发现其他事情,它发生在我身上在某些 ssh 连接上,然后我们将 while 条件设置为在通道打开时读取...如果您没有根据上述概念证明进行某些操作,将尝试找到该代码并将其添加到此处
已解决!
我正在阅读多行,但只有一行进入,而我在消息后没有包含换行符 (\n
)(这表示上一条消息是已完成的行)。
将 PHP $message = "player_online ". $username;
替换为 $message = "player_online ". $username ."\n";
还得替换Java
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String final_line = sb.toString();
和
String inputLine = in.readLine();
String final_line = inputLine;
我有一个 PHP 文件与 Java 套接字服务器通信,当我发送数据时,我的 java 服务器卡住(挂起、冻结)在 inputLine = in.readLine()
.我调试了一下,发现只有在读取数据的时候才会出现这种情况。
这是我的 java 服务器方法:
public void start_echo_server(int port){
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "STARTING SOCKET LISTENER (echo)"));
int portNumber = port;
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
// accepted the connection
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "ACCEPTED"));
// in stream
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// outstream
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String final_line = sb.toString();
main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "IN: " + final_line));
//String final_ret = parser.parse_message(final_line);
//main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "FINAL: " + final_ret));
out.println(final_line);
in.close();
out.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我的 PHP 文件:
<?php
if( isset($_POST['username']) )
{
$username = $_POST['username'];
parse($username);
}else{
echo "Missing parameters!";
exit();
}
function parse($username){
//Must be same with server
$host = "127.0.0.1";
$port = 59090;
// No Timeout
//Create Socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//Connect to the server
$result = socket_connect($sock, $host, $port) or die("Could not connect toserver\n");
$message = "player_online ". $username;
//Write to server socket
$len = strlen($message);
socket_write($sock, $message, $len) or die("SENDING ERROR ". $message ." \n");
//Read server respond message
$result = socket_read($sock, 1024) or die("RESPONSE ERROR ". $message ." \n");
echo "Reply From Server :".$result;
//Close the socket
socket_close($sock);
}
?>
问题是当我在 PHP 端执行 socket_write
(写入数据)时,但问题出在 java 行 while ((inputLine = in.readLine()) != null) {
。
非常感谢!
尝试将 while 条件更改为您控制的条件,作为概念证明,即在 1 分钟左右的时间内阅读,如果这让 ypu 解开,那么在 in.readline 时不要阅读,但发现其他事情,它发生在我身上在某些 ssh 连接上,然后我们将 while 条件设置为在通道打开时读取...如果您没有根据上述概念证明进行某些操作,将尝试找到该代码并将其添加到此处
已解决!
我正在阅读多行,但只有一行进入,而我在消息后没有包含换行符 (\n
)(这表示上一条消息是已完成的行)。
将 PHP $message = "player_online ". $username;
替换为 $message = "player_online ". $username ."\n";
还得替换Java
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
String final_line = sb.toString();
和
String inputLine = in.readLine();
String final_line = inputLine;