首先,这是一道作业题.话虽这么说,我被卡住了.在套接字上搜索 java 属性会导致很多不相关的事情.
First of all, this is a homework problem. That being said, I'm stuck. Googling for java Properties over Sockets results in a lot of irrelevant things.
我正在尝试通过套接字传输 Properties 对象.API 说它可以用 Stream 或 Writer/Reader 来完成,但我无法让它工作.我可以手动完成,也就是说,如果我逐行读取文件并通过 PrintWriter 传递它.
I'm trying to transfer a Properties object over a socket. The API says it can be done with a Stream or a Writer/Reader, but I can't get it to work. I can do it manually, that is, if I read the file line by line and pass it through a PrintWriter.
在客户端,我大致了解:
On the client side I've got roughly:
socket = new Socket(host, port); outStream = socket.getOutputStream(); out = new PrintWriter(outStream, true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); reader = new BufferedReader(new FileReader(file)); ... props.load(reader); props.store(out, null);在服务器端,接收位看起来像:
On the server side the receiving bits look like:
out = new PrintWriter(sock.getOutputStream(), true); inStream = sock.getInputStream(); in = new BufferedReader( new InputStreamReader(inStream)); ... props.load(in); // hangs // doesn't get to code here...在这种情况下,它挂在 props.load(in) 上.我没有执行 props.load(in),而是逐行读取它以确保 props.store(out, null) 正常工作,并且数据看起来像是在传输.
In this case it hangs at the props.load(in). Instead of doing props.load(in), I read it in line by line to make sure props.store(out, null) was working, and the data looks like its being transferred.
是否有关于加载/存储的问题我不明白,或者是 Stream/Writer/Reader 的问题?
Is there something about load/store I don't understand, or is it an issue with the Stream/Writer/Reader?
推荐答案我认为这将回答这个问题以及 如何识别 Java 套接字中的 EOF? 和 我可以向 InputStream 发送什么来表示已达到 EOF?
I think this will answer this question as well as How do I recognize EOF in Java Sockets? and What can I send to an InputStream to signify EOF has been reached?
我遇到了类似的问题;我的困境是我有一个客户端/服务器请求-响应协议,其中一个请求包括使用 clientProps.store() 从客户端发送的流.服务器端相应的 serverProps.load() 永远不会返回,因为它需要查看文件结尾"——这在 Java 中意味着客户端必须关闭它的流;导致套接字连接关闭.不想要的结果是,我不仅不能为无限期的请求-响应交换保持套接字打开,我什至不能保持它打开以便服务器发送其回复.
I had a similar problem; my dilemma was that I had a client/server request-response protocol where one of the requests included a stream sent from the client side using clientProps.store(). The corresponding serverProps.load() on the server side never returns because it needs to see the "end-of-file" - which in Java means the client has to close it's stream; resulting in the socket connection closing. The unwanted result was that, not only could I not keep the socket open for indefinite request-response exchanges, I couldn't even keep it open for the server to send its reply.
我讨厌 Java 让我这样做,更因为 Properties.load() 的文档说:
I hated Java for making me do that, even more because the documentation for Properties.load() says:
此方法返回后,指定的流保持打开状态.
The specified stream remains open after this method returns.
如果它通过看到流关闭来检测文件结尾,那永远不会发生!!无论如何,现在,我仍然喜欢 Java,因为它允许我使用此解决方案(如果您对流式传输的数据有任何特殊编码或本地化,则可能没有用):
That could never happen if it's detecting end-of-file by seeing the stream close!! Anyway, now, I still love Java because it allowed me to use this solution (might not be useful if you have any special encoding or localization of the data you are streaming):
我在客户端使用了这个:
I used this on the client side:
PrintWriter toServer; Properties clientProps = new Properties(); // ... code to populate the properties and to // construct toServer from the socket ... clientProps.store(toServer, null); toServer.write('\u001A'); // this is an old-school ASCII end-of-file toServer.flush();在服务器端,我扩展了 Reader 以检测 1A 并返回 -1(以便 serverProps.load() 以正常方式了解文件结尾(通过查看 -1 从对 read()) 的调用返回,但在下面,流和套接字保持打开状态.
On the server side I extended Reader to detect the 1A and return -1 (so that the serverProps.load() learns about the end-of-file in the normal way (by seeing -1 returned from a call to read()), but below that, the stream and the socket stay open.
BufferedReader fromClient; Properties serverProps = new Properties(); // ... code to construct fromClient from the socket ... serverProps.load (new PropReader (fromClient)); ///// private static class PropReader extends Reader { BufferedReader src; boolean eof=false; private PropReader(BufferedReader fromClient) { super(); src=fromClient; } @Override public int read(char[] cbuf, int off, int len) throws IOException { int inCount; if (!eof) { inCount = src.read(cbuf, off, len); if (inCount > 0) { // we read a buffer... look at the end for the EOF that the client used to mark the end of file if (cbuf[off+inCount-1] == '\u001A') { --inCount; // don't send eof with the data eof = true; // next time... we'll return -1 } } } else { inCount = -1; } return inCount; } @Override public void close() throws IOException { src.close(); }更多推荐
套接字上的 Java 流属性
发布评论