1

ObjectInputStream 및 ObjectOutputStream을 사용하여 Socket을 통해 데이터를 보내려고합니다. 나는 인터넷에서 보았지만, 몇 가지 설명을하더라도, 나는 내 문제를 해결할 수 없다.ObjectInputStream 내 소켓을 차단하십시오.

여기 내 생성자의 연속입니다 :

public class Server { 
    //Entry point of the application 
    public static void main(String[] args) { 
     Integer port; 
     if(args.length<=0) 
      port=new Integer("3000"); // si pas d'argument : port 3000 par d�faut 
     else 
      port = new Integer(args[0]); // sinon il s'agit du num�ro de port pass� en argument 

     ClientListener server = new ClientListener(port); // instance de la classe principale 
    } 
} 

public ClientListener(Integer port) { // THIS IS THE MAIN THREAD 
     try 
     { 
      server = new ServerSocket(port.intValue()); // ouverture d'un socket serveur sur port 
      new IOCommande(this); // RUN IN ANOTHER THREAD 
      while (true) // attente en boucle de connexion (bloquant sur ss.accept) 
      { 
      new ClientSocketStream(server.accept(),this); // un client se connecte, un nouveau thread client est lanc� 
      } 
     } 
     catch (Exception e) { 
      //server.close(); 
     } 
    } 

public ClientSocketStream(Socket incomingClient, ClientListener ref) { // THIS IS NOT A THREAD 
     this.client = incomingClient; 
     this.server = ref; 

     //Ajout des IO et generation d'un UID 
     try{ 
      out = new ClientOutputWriter(client.getOutputStream()); 
      in = new ClientInputReader(((MessageReader)this),client.getInputStream()); 

      // ajoute du client dans la liste 
      id = server.addClient(this); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }finally {// En cas de probl�me (que le client s'est d�connect�) 
      try { 
       server.removeClient(id); 
       client.close(); 
      }catch(IOException e) { 

      } 
     } 
    } 

public ClientInputReader(MessageReader client, InputStream in) throws IOException { //THIS IS A THREAD 
     this.client = client; 
     try { 
      this.in = new ObjectInputStream(in); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     this.runtime = new Thread(this); 
     this.runtime.start(); 
    } 

public ClientOutputWriter(OutputStream out) throws IOException { //THIS IS NOT A THREAD 
     this.out = new ObjectOutputStream(out); 
    } 

소켓이 완전히 작동하는지 생각하십시오, 그래서 내가 텔넷을 사용하여 연결할 수 있어요.

그래서 ClientInputReader 클래스에서 ObjectInputStream을 인스턴스화 할 때 막혔습니다. 생성자가 차단 된 것처럼 보입니다. 올바른 생성자를 사용할 수 없습니다.

솔루션 :

대신 텔넷을 사용하여 테스트의 또 다른 실행 가능한 프로젝트를 만들어보십시오. 하는 것을 잊지 마십시오 ObjectInputStream와 전에 ObjectOutputStream를 선언하고 다음과 같이 뭔가를 할 : 당신의 클라이언트가 ObjectOutputStream을 구성되지

public class TestClient { 
    //Entry point of the application 
    public static void main(String[] args) { 
     try { 
      Socket socket = new Socket("127.0.0.1", 3000); 
      ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); 
      ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); 
      out.writeObject(new Message("test")); 
      Message msg = (Message)in.readObject(); 
      socket.close(); 
     }catch(Exception e){} 
    } 
} 

답변

1

, 그래서 당신의 ObjectInputStream 생성자는 이유로 차단하는 것은 자바 독에 설명했다.

+0

나는 텔넷 연결을 통해 테스트 할 수 있었기 때문에 나는이 문제를 이해하지 못했다. 따라서 다른 클래스를 생성하면 잘 작동합니다. 감사 ! – Lollipop