问题描述
笔者在Socket文件上传中,在文件写出完成之后,发现服务端抛出如下异常
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at ServerTest$Client.run(ServerTest.java:49)
at java.lang.Thread.run(Thread.java:744)
发现文件能正常上传,但是在文件上传完成后,抛出异常。
实验
笔者又进行了另外一个实验,在服务端用通过循环读取客户端的信息,客户端发送数据。
//服务端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest{
public static void main(String[] args) {
new Thread(new Server()).start();;
}
private static class Server implements Runnable{
private ServerSocket ss = null;
@Override
public void run() {
try {
ss = new ServerSocket(6677);
while(!Thread.currentThread().isInterrupted()){
Socket s = ss.accept();
Thread t = new Thread(new Client(s));
t.setDaemon(true);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class Client implements Runnable{
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public Client(Socket s){
this.s = s;
try {
this.dis = new DataInputStream(s.getInputStream());
this.dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while(!Thread.currentThread().isInterrupted()){
String str = dis.readUTF();
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(dos!=null)dos.close();
if(dis!=null)dis.close();
if(s!=null)s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//客户端
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) {
Socket s = null;
DataOutputStream dos = null;
try {
s = new Socket("127.0.0.1",6677);
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("hello");
dos.flush();
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
bos.close();
/* try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(dos!= null){
dos.close();
}
if(s != null){
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
发现依然出现问题描述中的异常。
解决方案
客户端在发送完文件后不可直接调用流的close(),无论是DataOoutputStream还是ObjectOutputStream亦或是BufferedOutputStream,还是InputStream都不可直接调用close()方法,当Socket.InputStream亦或是Socket.OutputStream调用close()方法都会使得Socket失效,从而服务端抛出异常。
另一方面在套接的其他流调用了close()方法中会调用Socket的流的close()方法。
所以当使用完输入和输出流后不应该及时关闭,可在客户端关闭时或需要在Socket需要关闭的时候进行流关闭。