2014-06-15 135 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

相关问题