このクラスを使って送受信をするとエラーが吐かれます
server側でObjectを受信しているのですが、nullを受信してしまいます
socket通信は一つのソケットで送受信両方とも行うのは問題になるのでしょうか?
コンソールではこのような文章が繰り返し表示されます。
server側で発生することもあればclient側で発生することもあります
start readObject java.io.EOFException at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3117) at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1597) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422) at PaffeLibrary_Type_GameBase/Server.Communication.run(Communication.java:57) at java.base/java.lang.Thread.run(Thread.java:830)
以下ソースコードです
ServerCommandクラスはSerializableを実装しています。
BaseSettingManagerはフラグや名前などの管理をしています。
public class Communication implements Runnable{ private boolean close_flg = false; private String issuer_name = ""; private List<ServerCommand> inputs = new ArrayList<>(); private ObjectInputStream in = null; private ObjectOutputStream out = null; private Socket sock = null; public Communication(Socket sock,String name) { this.issuer_name = name; try { if(sock != null) { this.sock = sock; new Thread(new InputStreamHelper()).start(); new Thread(new OutputStreamHelper()).start(); Thread.sleep(10000); new Thread(this).start(); } }catch (InterruptedException e) { e.printStackTrace(); } } @Override public void run() { while(!close_flg) { if(in != null) { try { System.out.println("start readObject"); // ここのreadObjectでエラーが出るjava.io.EOFException ServerCommand input = (ServerCommand)in.readObject(); System.out.println("end readObject"); inputs.add(input); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } public List<ServerCommand> hereInputsCommand(){ List<ServerCommand> return_list = this.inputs; this.inputs = new ArrayList<>(); if(return_list.size() > 0) { return return_list; }else { return null; } } public void sendServerCommand(List<Command> send_commands) { try { ServerCommand send_command = new ServerCommand(issuer_name,null,null); for(Command i : send_commands) { send_command.setSubCommand(i); } this.out.writeObject(send_command); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void close() { try { in.close(); out.close(); sock.close(); this.close_flg = true; BaseSettingManager.changeSettings("communication_flg", false); System.out.println("socket close"); } catch (IOException e) { e.printStackTrace(); } } public void closeAndSendCloseCommand() { try { ServerCommand send_command = new ServerCommand( issuer_name, null, null ); send_command.setSubCommand( new ServerCommand( "close", null, null) ); this.out.writeObject(send_command); System.out.println("send to close_command"); } catch (IOException e) { e.printStackTrace(); } this.close(); } public class InputStreamHelper implements Runnable{ @Override public void run() { try { in = new ObjectInputStream(sock.getInputStream()); } catch (IOException e1) { e1.printStackTrace(); } } } public class OutputStreamHelper implements Runnable{ @Override public void run() { try { out = new ObjectOutputStream(sock.getOutputStream()); } catch (IOException e1) { e1.printStackTrace(); } } } public String getName() { return issuer_name; } public void setName(String name) { this.issuer_name = name; } public boolean getCloseFlg() { return this.close_flg; } }
回答1件
あなたの回答
tips
プレビュー