2012-11-04 82 views
0

我已經使用Socket框架在我的手機和我的電腦之間建立了連接。只要我只需要發送一個命令,它就可以正常工作。然後我必須重新啓動我的服務器才能接收新的命令。你能幫我弄清楚爲什麼?我希望能夠發送多個命令。ServerSocket停止接收命令,java/android

我從GUI主類調用類方法。

我的服務器代碼:

public class Server { 

    private static PrintWriter out; 
    private static BufferedReader in; 
    private static ServerSocket serverSocket = null; 
    private static Socket clientSocket = null; 
    private static String inputLine, outputLine; 

    public static void main() throws IOException { 


     try { 
      serverSocket = new ServerSocket(4444); 
      System.out.println(serverSocket.getInetAddress().toString()); 
     } catch (IOException e) { 
      System.err.println("Could not listen on port: 4444."); 
      System.exit(1); 
     } 


     try { 
      clientSocket = serverSocket.accept(); 
     } catch (IOException e) { 
      System.err.println("Accept failed."); 
      System.exit(1); 
     } 

     out = new PrintWriter(clientSocket.getOutputStream(), true); 
     in = new BufferedReader(
       new InputStreamReader(
       clientSocket.getInputStream())); 
     Protocol kkp = new Protocol(); 

     outputLine = kkp.processInput(null); 
     out.println(outputLine); 

     while ((inputLine = in.readLine()) != null) { 
      System.out.println(inputLine); 
      outputLine = kkp.processInput(inputLine); 
      out.println(outputLine); 
     } 

    } 

    public static void shutDown() throws IOException 
    { 
     System.exit(1); 
     out.close(); 
     in.close(); 
     clientSocket.close(); 
     serverSocket.close(); 
    } 

} 

而我的客戶端代碼:

class ClientThread implements Runnable { 

     public void run() { 
      Socket kkSocket = null; 
      PrintWriter out = null; 
      BufferedReader in = null; 


      try { 
       InetAddress serverAddr = InetAddress.getByName(serverIpAddress); 
       kkSocket = new Socket(serverAddr, 4444); 
       out = new PrintWriter(kkSocket.getOutputStream(), true); 
       in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); 
      } catch (UnknownHostException e) { 
       showToast("Kan ikke finde host fra: "+settings.getString("ip", "86.52.16.102")); 
       System.err.println("Don't know about host: taranis."); 
       System.exit(1); 
      } catch (IOException e) { 
       showToast("Kan ikke udveksle oplysninger med: "+settings.getString("ip", "86.52.16.102")); 
       System.err.println("Couldn't get I/O for the connection to: taranis."); 
       System.exit(1); 
      } 

      String fromServer; 
      String fromUser; 

      try { 
       while ((fromServer = in.readLine()) != null) { 
        System.out.println("Server: " + fromServer); 
        if (fromServer.equals("Shutting down") || fromServer.equals("Wrong pin!")) 
        { 
         showToast(fromServer); 

         break; 
        } 


        //fromUser = stdIn.readLine(); 
        fromUser = pinkode.getText().toString()+","+time; 
        if (fromUser != null) { 
         System.out.println("Client: " + fromUser); 
         out.println(fromUser); 
         fromUser = null; 
        } 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      out.close(); 
      try { 
       in.close(); 
       kkSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 

回答

0

儘量把這部分代碼:

try { 
     clientSocket = serverSocket.accept(); 
     } catch (IOException e) { 
      System.err.println("Accept failed."); 
      System.exit(1); 
     } 

    out = new PrintWriter(clientSocket.getOutputStream(), true); 
    in = new BufferedReader(
      new InputStreamReader(
      clientSocket.getInputStream())); 
    Protocol kkp = new Protocol(); 

    outputLine = kkp.processInput(null); 
    out.println(outputLine); 

    while ((inputLine = in.readLine()) != null) { 
     System.out.println(inputLine); 
     outputLine = kkp.processInput(inputLine); 
     out.println(outputLine); 
    } 

裏面一個while(true){ ... }

+0

謝謝,那份工作。你能解釋爲什麼嗎? :) – gedemagt

+0

由於accept()方法等待客戶端嘗試連接服務器,並且在客戶端,當傳輸完成時關閉套接字。您需要打開另一個套接字...請參閱:http://www.coderanch.com/t/557585/java/java/ServerSocket-accept。希望這個幫助:) – amp