程序停留在ObjectInputStream构造函数中

时间:2016-11-04 07:24:13

标签: java sockets

嘿我试着在过去2-3天内调试这个程序。这是问题,我建立一个客户端服务器架构,所有客户端使用Socket连接在一定的时间间隔后ping服务器(及其信息)。所以在服务器端,当我尝试构造一个ObjectOutputStream时程序卡住了。这是客户代码。

 public void pingUpdate(){
    Thread pingThread = new Thread() {
    public void run() {
        while(true) {
             try { 
                ping_socket = new Socket( "localhost", 11111 );
                ObjectOutputStream ping_objectOutputStream = new ObjectOutputStream( ping_socket.getOutputStream( ) );
                ping_objectOutputStream.flush();

                ping_objectOutputStream.writeObject( user );

                ping_objectOutputStream.close();

                ping_socket.close( );

             }catch (Exception exception) {
               exception.printStackTrace();
            }
           }
   };
    pingThread.start();
}

这是服务器的代码

  public void run() {
while ( true ) {
      try {

            System.out.println("Server Listening" );

            Socket client = null;

            client = serverSock.accept();
            System.out.println("Accepted" );

             InputStream inputStream = client.getInputStream();

             System.out.println("Input stream established" );

             ObjectInputStream ois = new ObjectInputStream( inputStream );

              System.out.println("Object streams established" );

              User user = ( User ) ois.readObject( );

              System.out.println("Object read" );

               ois.close( );
               client.close( );
             }
              catch (Exception e){
                     e.printStackTrace();
                 }
         }
    }

服务器程序打印直到"输入流已建立"而且卡住了。虽然我在客户端刷新了输出流,但我不知道为什么会发生这种情况。谢谢。

1 个答案:

答案 0 :(得分:1)

我无法重现挂客户端。我认为必须如何将对象写入流而不用之后刷新流,因为流只在它们已满或在关闭之前被刷新。请使用finally块来关闭套接字和流。

我重写了您的源代码并发送了一个字符串“Hallo,Server”而不是User对象(在您提供的代码中缺少)。发送后我添加了一点延迟,因为没有连接服务器。

我已经使用JDK 1.8 Update 102测试了Win 8.1上的代码,现在可以正常运行。

客户端:

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client{

   public static void main(String[] args){

      new Client().pingUpdate();
   }

   public void pingUpdate(){

      Thread pingThread = new Thread(){

         @Override
         public void run(){

            while(true){
               Socket ping_socket = null;
               ObjectOutputStream ping_objectOutputStream = null;
               try{
                  ping_socket = new Socket("localhost",
                                           11111);
                  ping_objectOutputStream = new ObjectOutputStream(ping_socket.getOutputStream());

                  ping_objectOutputStream.writeObject("Hallo, Server");
                  ping_objectOutputStream.flush();
               }
               catch(Exception exception){
                  exception.printStackTrace();
               }
               finally{
                  try{
                     if (ping_objectOutputStream != null){
                        ping_objectOutputStream.close();
                     }
                  }
                  catch(IOException e){
                     e.printStackTrace();
                  }
                  try{
                     if (ping_socket != null){
                        ping_socket.close();
                     }
                  }
                  catch(IOException e){
                     e.printStackTrace();
                  }
               }

               // wait some time for the next ping
               try{
                  Thread.sleep(1000);
               }
               catch(InterruptedException e){
                  e.printStackTrace();
               }
            }
         }
      };
      pingThread.start();
   }
}

服务器:

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server{

   public void servePingUpdate(){

      Thread pingThread = new Thread(){

         @Override
         public void run(){

            ServerSocket serverSock = null;
            try{
               serverSock = new ServerSocket(11111);

               while(true){
                  Socket client = null;
                  ObjectInputStream ois = null;

                  try{
                     System.out.println("Server Listening");

                     client = serverSock.accept();
                     System.out.println("Accepted");

                     InputStream inputStream = client.getInputStream();

                     System.out.println("Input stream established");

                     ois = new ObjectInputStream(inputStream);

                     System.out.println("Object streams established");

                     String message = (String) ois.readObject();

                     System.out.println("Object read: " + message);
                  }
                  catch(Exception e){
                     e.printStackTrace();
                  }
                  finally{
                     try{
                        if (ois != null){
                           ois.close();
                        }
                     }
                     catch(IOException e){
                        e.printStackTrace();
                     }
                     try{
                        if (client != null){
                           client.close();
                        }
                     }
                     catch(IOException e){
                        e.printStackTrace();
                     }
                  }
               }
            }
            catch(IOException e1){
               e1.printStackTrace();
            }
            finally{
               try{
                  if (serverSock != null){
                     serverSock.close();
                  }
               }
               catch(IOException e){
                  e.printStackTrace();
               }
            }
         }
      };
      pingThread.start();
   }

   public static void main(String[] args){

      new Server().servePingUpdate();
   }
}

编辑:TCP协议需要一些时间来进行TCP握手,服务器需要一些时间来通过System.out输出线路并关闭套接字。

在客户端没有等待的情况下抛出 java.net.BindException:地址已经在使用:连接 1-2秒后,因为服务器没有可用的端口。 对于低延迟ping,UDP协议更好,请参阅example code for a UDP pinger或保持套接字打开并为每次ping重用它。