2017-11-02 7 views
1

소켓을 사용하여 2 개의 장치를 연결하는 응용 프로그램이 있습니다. 클라이언트에서는 데이터 송수신을 시도합니다. 클라이언트에서 데이터를 보낼 때 서버는 데이터를 수신하고 다른 메시지에 응답 할 수 있습니다. 하지만 서버에서 수신을 잡으려고 InputStream을 열면 알림을 표시합니다 (IOException : java.net.SocketException : Socket is closed). 도와주세요. @@ 클라이언트 코드 :메서드에서 getOutputStream 및 getInputStream을 사용할 수 없습니다.

Socket socket = null; 
    String mes = message; 
    try { 
     socket = new Socket(dstAddress, dstPort); 

     /*Example send data to server*/ 
     ops = socket.getOutputStream(); 
     ps = new PrintStream(ops); 
     ps.print(mes); 
     ps.close(); 
     /*End of example*/ 
     /*Receive data from server*/ 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
       1024); 
     byte[] buffer = new byte[1024]; 

     int bytesRead; 
     InputStream inputStream = socket.getInputStream(); 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      byteArrayOutputStream.write(buffer, 0, bytesRead); 
      response += byteArrayOutputStream.toString("UTF-8"); 
     } 
     /*End of Receive data from server*/ 
     Log.e("Receive From Server:", response); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     response = "UnknownHostException: " + e.toString(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     response = "IOException: " + e.toString(); 
    } finally { 
     if (socket != null) { 
      try { 
       socket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

및 서버

private class SocketServerThread extends Thread { 

    int count = 0; 
    @Override 
    public void run() { 
     try { 
      serverSocket = new ServerSocket(socketServerPORT); 

      while (true) { 
       Socket socket = serverSocket.accept(); 
       count++; 

       /*example get input String*/ 
       try { 
        isr = new InputStreamReader(socket.getInputStream()); 
        br = new BufferedReader(isr); 
        request = br.readLine(); 
        Log.e("Mess-H.a", request); 
       }catch (Exception e){ 
        Log.e("Can't receive data:", e.getMessage()); 
       } 
       message += "#" + count + " from " 
         + socket.getInetAddress() + ":" 
         + socket.getPort() + request + "\n"; 
       /*End of example*/ 
       activity.runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         activity.msg.setText(message); 
        } 
       }); 
       SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
         socket, count); 
       socketServerReplyThread.run(); 

      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

private class SocketServerReplyThread extends Thread { 

    private Socket hostThreadSocket; 
    int cnt; 

    SocketServerReplyThread(Socket socket, int c) { 
     hostThreadSocket = socket; 
     cnt = c; 
    } 

    @Override 
    public void run() { 
     OutputStream outputStream; 
     String msgReply = "Hello from Server, you are #" + cnt; 
     try { 
      outputStream = hostThreadSocket.getOutputStream(); 
      PrintStream printStream = new PrintStream(outputStream); 
      printStream.print(msgReply); 
      printStream.close(); 

      message += "replayed: " + msgReply + "\n"; 

      activity.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        activity.msg.setText(message); 
       } 
      }); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      message += "Something wrong! " + e.toString() + "\n"; 
     } 

     activity.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       activity.msg.setText(message); 
      } 
     }); 
    } 
} 

답변

0

의이 코드는 소켓을 폐쇄하고 그것을 사용하는 것을 계속했다.

ps.close(); 

여기에 있습니다. 풀다.

+0

많은 감사. ^^ –