英文:
Java ServerSocket OutputStream not flushing
问题
Server:
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
os.write("Hello World".getBytes());
os.flush();
// os.close();
Client:
Socket client = new Socket("localhost", 7788);
byte[] bytes = client.getInputStream().readAllBytes();
String message = new String(bytes);
System.out.println(message);
如果我关闭OutputStream,一切都正常工作,但是我需要在我的项目中多次发送数据。这个问题有解决方法吗?
英文:
My ServerSocket (java.net.ServerSocket) only flushes the data if I close the OutputStream, but if I close the OutputStream, it closes the connection. Here is my code
Server:
ServerSocket server = new ServerSocket(7788);
Socket client = server.accept();
OutputStream os = client.getOutputStream();
os.write("Hello World".getBytes());
os.flush();
// os.close();
Client:
Socket client = new Socket("localhost", 7788);
byte[] bytes = client.getInputStream().readAllBytes();
String message = new String(bytes);
System.out.println(message);
If I close the OutputStream everything works fine, but I need to send data multiple times for my project. Is there a solution for this?
答案1
得分: 1
问题不在于OutputStream
(服务器)上的flush()
,而在于InputStream
(客户端)上的readAllBytes()
。根据文档(我加了重点):
> public byte[] readAllBytes() throws IOException
>
> 从输入流中读取所有剩余的字节。此方法会阻塞,直到读取所有剩余字节并检测到流结束,或者抛出异常。
只有在close()
时才会"发送"“流结束”,这就是为什么只有在close()
之后才能读取(打印)消息。
交换单行文本数据的一个非常简单的解决方案是通过'\n'
(换行)字符分隔消息,在客户端部分按行从套接字读取,例如:
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()
会在读取到\n
(换行)字符后立即返回。
如果这对您的用例不可行,我认为您将需要一个更结构化的协议。
英文:
the problem is not the flush()
on the OutputStream
(server), but rather the readAllBytes()
on the InputStream
(client). from the docs (emphasis mine):
> 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
is "sent" only on close()
, that is why you are able to read (print) the message only after close()
.
a very simple solution to exchange one-line textual data would be to send messages separated by '\n' (new line) character and on the client part, read from the socket line-by-line, for example:
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
$
if you run the above code, you will see that messages gets printed (received) one after the other with a 2 seconds delay and readLine()
returns as soon as it read a \n
(new line) character.
if this is not feasible for your use case, I think you will need a more structured protocol.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论