基于socket通信,android客户端关闭后再次打开,服务端报出java.io.EOFException

sunny冰青 发布于 2015/03/13 14:15
阅读 1K+
收藏 1

问题详情如题,如果调试需要源码请联系我QQ1450830135

服务器已启动,正在监听1236端口 

[null]上线了!

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at decryptstrangerserver.serverapp.Server.<init>(Server.java:36)
at decryptstrangerserver.main.Main.main(Main.java:10)

以下是问题补充:

@sunny冰青:服务端主要代码 package decryptstrangerserver.serverapp; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import decryptstranger.utils.SystemMessage; import decryptstranger.utils.SystemMessageTypeConstants; import decryptstranger.utils.User; import decryptstrangerserver.database.DatabaseBasicOperation; public class Server { public Server() throws java.io.EOFException { ServerSocket serverSocket = null; Socket socket = null; User user = null; ObjectInputStream ois = null; try { serverSocket = new ServerSocket(1236); System.out.println("服务器已启动,正在监听1236端口 "); while(true) { socket = serverSocket.accept(); ois = new ObjectInputStream(socket.getInputStream()); try { user = (User) ois.readObject(); System.out.println(user.getAccount()); } catch(ClassNotFoundException e) { System.out.println("--------空指针异常---------"); } finally { } SystemMessage sm = new SystemMessage(); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); if(user.getOperation().equals("login")) { //登录 DatabaseBasicOperation dbBasicOperation = new DatabaseBasicOperation(); boolean b = dbBasicOperation.login(user.getAccount(), user.getPassword());//连接数据库验证用户 if(b) { System.out.println("["+user.getAccount()+"]上线了!"); //得到个人信息 String userInfo = dbBasicOperation.getUserInfo(user.getAccount()); //返回一个成功登陆的信息包,并附带个人信息 sm.setType(SystemMessageTypeConstants.SUCCESS); sm.setContent(userInfo); oos.writeObject(sm); ServerConnectClientThread scct = new ServerConnectClientThread(socket);//单开一个线程,让该线程与该客户端保持连接 ServerConnectClientThreadManager.addServerConnectClientThread(user.getAccount(),scct); scct.start();//启动与该客户端通信的线程 } else { sm.setType(SystemMessageTypeConstants.FAIL); oos.writeObject(sm); } } else if(user.getOperation().equals("register")) { DatabaseBasicOperation dbBasicOperation = new DatabaseBasicOperation(); if(dbBasicOperation.register(user)) { //返回一个注册成功的信息包 sm.setType(SystemMessageTypeConstants.SUCCESS); oos.writeObject(sm); } } if(ois!=null) ois.close(); if(socket != null) { if(socket.isConnected()) { // socket.shutdownInput(); // socket.shutdownOutput(); socket.close(); } } } } catch (Exception e) { e.printStackTrace(); } finally { } } } (2015/03/13 17:12)
加载中
0
痞子蔡
痞子蔡
对Java不懂,在C语言中,套接字连接后客户端断开,TCP会发送RST给服务器,服务器要应答,但是此时客户端无法回应,于是造成SIGPIPE信号,服务器崩溃。在C语言中只要处理SIGPIPE就行了,在Java中你找找有没有类似的套接字事件,当事件发生的时候忽视它就应该不会崩溃了。
s
sunny冰青
谢谢问题已经解决,是在捕获异常中释放资源
0
泡泡丹丹
泡泡丹丹

http://lavasoft.blog.51cto.com/62575/235269/

看下这个,希望对你有用。

s
sunny冰青
额,谢谢。问题还没有解决
返回顶部
顶部