Java ServerSocket OutputStream 不刷新
Java ServerSocket OutputStream not flushing
我的 ServerSocket (java.net.ServerSocket) 仅在我关闭 OutputStream 时刷新数据,但如果我关闭 OutputStream,它会关闭连接。这是我的代码
服务器:
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
os.write("Hello World".getBytes());
os.flush();
// os.close();
客户:
Socket client = new Socket("localhost", 7788);
byte[] bytes = client.getInputStream().readAllBytes();
String message = new String(bytes);
System.out.println(message);
如果我关闭 OutputStream 一切正常,但我需要为我的项目多次发送数据。有解决办法吗?
问题不在于 OutputStream
(服务器)上的 flush()
,而是 InputStream
(客户端)上的 readAllBytes()
。来自 docs(强调我的):
public byte[] readAllBytes() throws IOException
Reads all remaining bytes from the input stream. This method blocks
until all remaining bytes have been read and end of stream is
detected, or an exception is thrown.
end of stream
仅在 close()
上“发送”,这就是为什么您只能在 close()
.
之后才能阅读(打印)邮件的原因
交换 one-line 文本数据的一个非常简单的解决方案是发送由 '\n'(换行)字符分隔的消息,并在客户端从套接字 line-by-line 读取,例如:
Server.java
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
for (int i = 0; i < 5; i++) {
os.write(String.format("message %d\n", i).getBytes());
os.flush();
Thread.sleep(2000);
}
os.close();
}
}
Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket client = new Socket("localhost", 7788);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = null;
while ((message = reader.readLine()) != null)
System.out.println(message);
reader.close();
}
}
$ javac Server.java Client.java
$ java Server
$ java Client
message 0
message 1
message 2
message 3
message 4
$
如果你运行上面的代码,你会看到消息被一个接一个地打印(接收),延迟 2 秒,readLine()
returns读取 \n
(新行)字符。
如果这对您的用例不可行,我认为您将需要一个更结构化的协议。
我的 ServerSocket (java.net.ServerSocket) 仅在我关闭 OutputStream 时刷新数据,但如果我关闭 OutputStream,它会关闭连接。这是我的代码
服务器:
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
os.write("Hello World".getBytes());
os.flush();
// os.close();
客户:
Socket client = new Socket("localhost", 7788);
byte[] bytes = client.getInputStream().readAllBytes();
String message = new String(bytes);
System.out.println(message);
如果我关闭 OutputStream 一切正常,但我需要为我的项目多次发送数据。有解决办法吗?
问题不在于 OutputStream
(服务器)上的 flush()
,而是 InputStream
(客户端)上的 readAllBytes()
。来自 docs(强调我的):
public byte[] readAllBytes() throws IOException
Reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and end of stream is detected, or an exception is thrown.
end of stream
仅在 close()
上“发送”,这就是为什么您只能在 close()
.
交换 one-line 文本数据的一个非常简单的解决方案是发送由 '\n'(换行)字符分隔的消息,并在客户端从套接字 line-by-line 读取,例如:
Server.java
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
for (int i = 0; i < 5; i++) {
os.write(String.format("message %d\n", i).getBytes());
os.flush();
Thread.sleep(2000);
}
os.close();
}
}
Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket client = new Socket("localhost", 7788);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = null;
while ((message = reader.readLine()) != null)
System.out.println(message);
reader.close();
}
}
$ javac Server.java Client.java
$ java Server
$ java Client
message 0
message 1
message 2
message 3
message 4
$
如果你运行上面的代码,你会看到消息被一个接一个地打印(接收),延迟 2 秒,readLine()
returns读取 \n
(新行)字符。
如果这对您的用例不可行,我认为您将需要一个更结构化的协议。