2014-06-15 133 views
3

我在我的客戶端有2個按鈕,每個按鈕都有一個監聽器。如何通過套接字發送字符串和整數?

在我的firt按鈕監聽器中,我發送了一個字符串在套接字上,並且在spanwed後我得到一個整數數組。那裏沒問題。這是我的代碼。

public void rollDice() { 

    try { 
     DataOutputStream sout1 = new DataOutputStream(socket.getOutputStream()); 
     String line = "dice"; 
     PrintStream out1 = new PrintStream(sout1); 
     out1.println(line); 
    } catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

} 

隨着第二收聽我要不要發出首先把服務器到正確的狀態,之後我想送一個整數進程繼續進行。這是我的代碼,但它似乎不工作。服務器正在打印一個隨機數,即使我發送一個「2」。

public void sendDice() { 

    try { 
     DataOutputStream sout2 = new DataOutputStream(socket.getOutputStream()); 
     String line = "pick"; 
     PrintStream out2 = new PrintStream(sout2); 
     out2.println(line); 

     out2.write(diceListLength); 
    } catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
} 

這是服務器端。

public void run() { 

     boolean running = true; 

     try { 

      // Create streams for reading/writing lines of text to the socket 
      DataInputStream input = new DataInputStream(s.getInputStream()); 
      DataInputStream inputInt = new DataInputStream(s.getInputStream()); 

      ObjectOutputStream output = new ObjectOutputStream(s.getOutputStream()); 

      // Print a message: 
      System.out.println("\nClient from: " + s.getInetAddress() + " port " + s.getPort()); 

      while(running) { 
       String st = input.readLine(); 

       if (st.equals("dice")) { 
        for (int i = 0; i < diceRolled.length - number; i++) { 
         diceRolled[i] = (int) (1 + Math.random() * 6); 
         System.out.print(diceRolled[i] + " "); 
        } 
        output.writeObject(diceRolled); 
        output.reset(); 
       } else if (st.equals("pick")) { 
        number = inputInt.readInt(); 
        System.out.print(number); 

       } 
      } 
     } catch (IOException e) { 
      System.out.println("Error: " + e.getMessage()); 
     // Always be sure to close the socket 
     } finally { 
      try { 
       if (s != null) { 
        System.out.println(s.getLocalSocketAddress() + " closed."); 
        s.close(); 
       }     
      } catch (Exception e) { 
       System.out.println("Error: " + e.getMessage()); 
      } 
     } 
    } 
+1

服務器端的'SocketInputStream'在哪裏? – Nullpointer

+0

@Nullpointer我正在使用'DataInputStream',但我沒有粘貼所有的代碼。更新。 – TheNerdFromYesterday

+0

嘗試打印'st'..的值是否正確? – Nullpointer

回答

2

嘗試對PrintStream的設置自動刷新,當你創建它...一個整數不會換行前發送或緩衝區已滿。

java's documentation

autoFlush - 一個布爾值;如果爲真,每當一個字節數組被寫入輸出緩衝區將被刷新,的其中一個println方法被調用時,或者一個新行字符或字節(「\ n」)被寫入

同樣有用的:

  1. 使用基於行的消息傳遞,即第二個消息類型可以是"pick:4"(與st.startsWith("pick")一起檢查),然後解析該整數。用你的代碼,你可以很容易地失去狀態。 (單行消息是「僞原子」)。
  2. 不要在每個偵聽器方法中創建DataInputStreams,使它們成爲對象變量(與PrintStreams相同...)。每次點擊都不需要(重新)創建對象。
+0

感謝您的回覆。我會閱讀文件,我會測試你的方法,我會批准你的答案。我不知道我不能通過套接字發送一個整數。非常感謝。 – TheNerdFromYesterday

+0

嗯,當然你可以,但庫中的默認設置會阻止你這樣做。特別是考慮到只發送32位有效載荷,3次握手等等的網絡流量開銷;) – Oerd

+0

所以你必須告訴它你想要做的就是(通過'autoFlush')。但是,我還是建議將命令與整數連接起來,然後將它單行發送(即「pick:4」,「pick-4」,甚至是「pick4」)。 – Oerd

相關問題