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

问题详情如题,如果调试需要源码请联系我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冰青
发帖于4年前 2回/1K+阅

以下是问题补充:

  • @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 { } } } (4年前)
顶部