java.io.EOFException

问题描述

笔者在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需要关闭的时候进行流关闭。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值