2012-05-14 8 views
1

필자가 작성한 게임에서 멀티 플레이어를 구현하려고하는데, 성공적으로 연결하려면 모든 것이 필요합니다. 클라이언트에 의해 throw 된 EOFException이 있고 객체 (ArrayList)가 성공적으로 수신되지 않습니다.안드로이드 프로그래밍에서 소켓과 객체 입출력 스트림 사용하기

서버 스레드에 대한 코드 :

class ServerThread implements Runnable 
{ 
    ServerSocket server = null; 
    Socket controlSocket = null; 
    ObjectOutputStream outStream = null; 
    ObjectInputStream inStream = null; 
    @Override 
    public void run() { 
     setupConnection(); 
     while(true){ 
      sendObject(out.getStuff());  
     } 


    } 
    void setupConnection(){ 
     Log.e("OUTPUTSHOOTER","init-connect"); 
     try { 
      server = new ServerSocket(SERVERPORT); 
      Log.e("OUTPUTSHOOTER","server initiated port: "+SERVERPORT); 
     controlSocket = server.accept(); 
     Log.e("OUTPUTSHOOTER","connected"); 
     inStream = new ObjectInputStream(controlSocket.getInputStream()); 
     outStream = new ObjectOutputStream(controlSocket.getOutputStream()); 
     } catch (StreamCorruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Log.e("OUTPUTSHOOTER",server+" "+controlSocket+" "+inStream+" "+outStream); 
     } 

    public Object recieveObject(){ 
     Object o = null; 
     try { 
      o = inStream.readObject(); 
     } catch (OptionalDataException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return o; 
    } 
    public void sendObject(Object o) 
    { 
      try { 
       outStream.writeObject(o); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    } 
} 

그리고 클라이언트의 코드 :

class ClientThread implements Runnable 
{ 
    Socket controlSocket = null; 
    ObjectOutputStream outStream = null; 
    ObjectInputStream inStream = null; 
    @Override 
    public void run() { 
     setupConnection(); 
     while(true){ 
      Log.e("OUTPUTSHOOTER","recieving"); 
      Object in = recieveObject(); 
      if(in!= null && in instanceof ArrayList) 
      { 
       Log.e("OUTPUTSHOOTER","loading"); 
       out.load((ArrayList<UniverseObject>)in); 
      }  
     } 


    } 

    void setupConnection(){ 
     Log.e("OUTPUTSHOOTER","ip: "+SERVERIP); 
     while(controlSocket == null) { 
      try { 
       controlSocket = new Socket(SERVERIP,SERVERPORT); 
       Log.e("OUTPUTSHOOTER","socket connected"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
     try { 
      Log.e("OUTPUTSHOOTER","attempting streams"); 
      outStream = new ObjectOutputStream(controlSocket.getOutputStream()); 
      Log.e("OUTPUTSHOOTER","output working"); 
      inStream = new ObjectInputStream(controlSocket.getInputStream()); 
      Log.e("OUTPUTSHOOTER","streams connected"); 

     } catch (StreamCorruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 
    } 
    public Object recieveObject(){ 
     Object o = null; 
     try { 
      o = inStream.readObject(); 
     } catch (OptionalDataException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return o; 
    } 
    public void sendObject(Object o) 
    { 
      try { 
       outStream.writeObject(o); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    } 
} 

이 무엇을 의미합니까? 그리고 아마도 더 중요한 것은 어떻게 해결할 수 있을까요? 미리 감사드립니다.

+0

네트워크 인터페이스 (wi-fi/mobile)를 통해 직렬화 된 객체를 보내려고하십니까? Android 기기간에 또는 서버에서 Android 기기로 연결 하시겠습니까? –

+0

wifi를 통해 두 Android 기기간에 ArrayList를 보내려고합니다. IP 주소와 소켓은 연결 자체가 성공 했으므로 정확합니다. 데이터 전송이 아닙니다. – user1394672

답변

1

출력 스트림을 닫지 않습니다. 그 소켓이 성공했다하더라도, Problem serializing and deserializing ArrayList

+0

사실, 그는'EOFException'을 얻을 수있는 유일한 방법은 스트림이나 소켓 중 하나를 닫는 것입니다. – EJP

0

서버가 제대로 입력 및 출력 스트림을의 시작되지 않았습니다 밝혀 :

이 SO 항목을 참조하십시오. Dunno why,하지만 출력 스트림을 먼저 시작한 다음 입력 (?)을 시작한 경우에만 작동합니다. 정말 이상한 버그가 있지만, 적어도 의사 소통은 작동하는 것 같습니다. 나는 그것에 대해 게시하기 전에 그들에게 더 많은 것을 보일 것입니다. 고마워요!

관련 문제