为什么客户端input.readObject有时返回“null”,有时工作正常?

时间:2017-03-19 18:40:44

标签: java sockets

我正在尝试创建一个使用tcp java连接的客户端/服务器程序,并使用localhost IP和端口12345在同一台计算机(服务器和客户端)上进行测试。

我使用ObjectInputStream和ObjectOutputStream来接收和发送消息。 有时我的程序完美,它会根据需要发送正确的消息,但有时在程序的随机时间/阶段)我的一个客户“失去”连接,他进入无限循环打印“null”“无效的类型代码:54”,而不做任何代码更改。

我没有错误只是服务器在循环中输出“连接重置”,而客户端“null”“无效的类型代码:54”< / strong>来自 catch(IOException e)

我将向您展示我的代码的基础

服务器:

    public static void main(String[] args) {
    //--------BASIC CODE used for connection stuff below----//
         server = new ServerSocket(12345);
         connection =server.accept();
         Thread thread =new Thread(new ClientThread(connection,GRM...));
         thread.start();
         output = new ObjectOutputStream(connection.getOutputStream());
         output.flush();
         input = new ObjectInputStream(connection.getInputStream());
    }
    --------------//---------thread for each client----------//--------------
    do{
           try{
             message=(String) input.readObject();
             if(message.startsWith("SOMENTHING"))//...some tcode
             if(message.equals("SEND")){
                 String message="hi";
                 output.writeObject("Chat:"+message);output.flush();
                 UserW.writeObject(message);UserW.flush();
                 //UserW is an output from another client stored in the server
             }
             catch(ClassNotFoundException classNotFoundException){
                System.out.print("\nUnknown object");
             }catch (IOException e) {
                System.out.println(e.getMessage());
             }    
     }while(!message.equals("DISCONNECT"));

客户端:

        public static void main(String[] args) {
        connection=new Socket(InetAddress.getByName("127.0.0.1"),12345);
        //...... same logic with streams......//
        }
        //-------thread that receives from server-----//
        do{
          try{
           String message=(String) input.readObject();
           //.....some code....//
          }catch( ClassNotFoundException classNotFoundException){
            System.out.print("\nUnknown object");
          }catch (IOException e) {
            System.out.println(e.getMessage());
          }
        }while(true);

2 个答案:

答案 0 :(得分:0)

  

为什么客户端readObject()有时会返回null

不是。 readObject()未返回null。您在catch块中打印 null,因为您捕获的IOException没有消息,例如可能EOFException,这会导致您关闭套接字并打破了阅读循环。

实际问题是并发问题,在这里:

Thread thread =new Thread(new ClientThread(connection,GRM...));
thread.start();
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());

outputinput变量应该是ClientThread的成员,而不是此类,它们应该在ClientThread.run()中初始化,而不是在其构造函数中初始化,当然也不是这里。

答案 1 :(得分:-1)

所以我用我创建的另一个更简单的服务器/客户端程序进行了一些测试 我想当服务器同时发送两条或更多条消息时,客户端的 input.read()会被混淆&#34;并且不会正确地阅读消息,使服务器执行&#34;连接重置&#34;并且客户提供&#34;无效代码&#34;错误

我对该问题的解决方案(有效)是在服务器站点上创建一个向客户端发送消息的同步方法,但我不喜欢它,因为服务器性能会降低(我认为)。作为第二种解决方案,我在客户网站上考虑制作缓冲区,但我不知道这是否可能。

如果有人有更好的解决方案,我很乐意听到它。

更新:最佳解决方案是使用 BufferedReader 进行输入,使用 PrintWriter 进行输出,而不是使用流。

P.S。非常感谢asettouf,他带领我走向正确的道路。