java socks EOFException异常解决

异常如下

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
    at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:2836)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2892)
    at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1075)

调用如下方法报的异常

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String version = ois.readUTF();//此句异常

分析问题

由于读取字符串读不完整,就会出现此问题,所以可以写服务端那边将ObjectOutputStream写后后调用如下语句即可

oos.writeUTF("version");
oos.flush();

即把字符串缓存完全刷过去。

你可能感兴趣的:(java)