java readUTF 中的 EOFException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17972172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EOFException in readUTF
提问by raja shekar
I am getting below EOFException
while using readUTF()
method, please let me know how could i overcome with this problem and also please suggest how readUTF()
transfers socket information over the other networks
我EOFException
在使用readUTF()
方法时遇到了问题,请让我知道我如何克服这个问题,并请建议如何readUTF()
通过其他网络传输套接字信息
import java.io.*;
import java.net.*;
public class GreetingServer {
public static void main(String args[]) {
String servername =args[0];
int port = Integer.parseInt(args[1]);
try {
System.out.println("Server Name "+ servername +"Port"+port);
Socket client = new Socket(servername,port);
System.out.println("Just connected to"+ client.getRemoteSocketAddress());
OutputStream outs = client.getOutputStream();
DataOutputStream dout = new DataOutputStream(outs);
dout.writeUTF("Hello From"+client.getRemoteSocketAddress());
InputStream in = client.getInputStream();
DataInputStream din = new DataInputStream(in);
System.out.println("Server Says"+ din.readUTF());
client.close();
}
catch (EOFException f) {
f.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
回答by user207421
You have reached the end of the stream. There is no more data to read.
您已到达流的末尾。没有更多的数据要读取。
Possibly your server isn't using writeUTF(),
or you are out of sync with it. If the server is writing lines you should be using BufferedReader.readLine().
可能您的服务器没有使用,writeUTF(),
或者您与它不同步。如果服务器正在编写您应该使用的行BufferedReader.readLine().
回答by Robadob
The docsfor readUtf()
state;
该文档的readUtf()
状态;
First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . This integer value is called the UTF length and specifies the number of additional bytes to be read. These bytes are then converted to characters by considering them in groups. The length of each group is computed from the value of the first byte of the group. The byte following a group, if any, is the first byte of the next group.
首先,读取两个字节并使用 readUnsignedShort 方法完全相同的方式构造一个无符号的 16 位整数。此整数值称为 UTF 长度,指定要读取的附加字节数。然后通过将它们分组考虑将这些字节转换为字符。每个组的长度是根据组的第一个字节的值计算的。组后面的字节(如果有)是下一组的第一个字节。
This suggests to me that what your trying to read with readUtf() isn't UTF, as the EOFException
occurs when the End of File (EOF) is read unexpectedly.
这向我表明,您尝试使用 readUtf() 读取的内容不是 UTF,因为在意外读取文件结尾 (EOF)EOFException
时会发生这种情况。
Check that you are reading the right types in the same order that your server is sending them etc. You should be following a decided protocol, rather than blindly reading.
检查您阅读的类型是否与服务器发送它们的顺序相同,等等。您应该遵循确定的协议,而不是盲目阅读。