2013-11-15 137 views
0

我似乎在這裏錯過了一些微不足道的東西。我正在使用java爲覆盆子pi和桌面命令行編寫應用程序。我可以將數據發送到pi,並且它會響應,但桌面應用程序沒有收到回覆(這是我想要的)。沒有例外被拋出。套接字不發送數據

下面是代碼: PI代碼:

package raspberrypiapp; 

import java.awt.Color; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.SocketException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author Michael 
*/ 
public class RemoteControlManager implements Runnable { 

    ServerSocket s; 
    Socket socket; 
    DataOutputStream dout; 
    DataInputStream din; 
    boolean connected = false; 
    RaspberryPiApp app; 

    public RemoteControlManager(RaspberryPiApp app) { 
     try { 
      s = new ServerSocket(12345); 
     } catch (IOException ex) { 
      Logger.getLogger(RemoteControlManager.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     this.app = app; 

     Thread t = new Thread(this); 
     t.start(); 
    } 

    public boolean connected() { 
     return socket == null ? connected : socket.isConnected(); 
    } 

    @Override 
    public void run() { 
     while (true) { 
      try { 
       if (!connected) { 
        socket = s.accept(); 
        dout = new DataOutputStream(socket.getOutputStream()); 
        din = new DataInputStream(socket.getInputStream()); 
        connected = true; 
       } else { 
//     dout.writeUTF("heartbeat"); 
        String message = din.readUTF(); 
//     System.out.println(parse(message)); 
        dout.writeUTF(parse(message)); 
//     dout.flush(); 
       } 
      } catch (SocketException ex) { 
       try { 
        socket.close(); 
        din.close(); 
        dout.close(); 
        socket = null; 
        connected = false; 
       } catch (IOException ex1) { 
        Logger.getLogger(RemoteControlManager.class.getName()).log(Level.SEVERE, null, ex1); 
       } 
      } catch (IOException ex) { 
       Logger.getLogger(RemoteControlManager.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 

    private String parse(String message) { 
     message = message.toLowerCase(); 
     String[] args = message.split(" "); 
     switch (args[0]) { 
      case "color": 
       if (args.length > 1) { 
        switch (args[1]) { 
         case "red": 
          app.color = Color.RED; 
          return "1a"; 
         case "green": 
          app.color = Color.GREEN; 
          return "1a"; 
         case "blue": 
          app.color = Color.BLUE; 
          return "1a"; 
         default: 
          return "!Do not recognize the color: \"" + args[1] + "\"."; 
        } 
       } else { 
        return "!You must include a color. Syntax: color [COLOR]"; 
       } 
      default: 
       return "!That command is not recognized. Please check spelling and syntax. Type \"help\" for help."; 
     } 
    } 
} 

桌面代碼(略):

public MainGUI() { 
     //<editor-fold defaultstate="collapsed" desc=" Center Window to Screen "> 
     GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice[] devices = g.getScreenDevices(); 

     int width = devices[0].getDisplayMode().getWidth(); 
     int height = devices[0].getDisplayMode().getHeight(); 

     int w = this.getSize().width; 
     int h = this.getSize().height; 
     int x = (width - w)/2; 
     int y = (height - h)/2; 
     this.setLocation(x, y); 
     //</editor-fold> 
     initComponents(); 
     try { 
      socket = new Socket(ip, 12345); 
      din = new DataInputStream(socket.getInputStream()); 
      dout = new DataOutputStream(socket.getOutputStream()); 
     } catch (UnknownHostException ex) { 
      Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (ConnectException ex) { 
      JOptionPane.showMessageDialog(this, "Could not find/connect to a Raspberry Pi at the address: \"" + ip + "\".", "Connection Error", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
     } catch (IOException ex) { 
      Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {            
     try { 
      dout.writeUTF(jTextField1.getText()); 
     } catch (IOException ex) { 
      Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     jTextField1.setText(""); 
    } 

@Override 
    public void run() { 
     while (true) { 
      String reply = null; 
      try { 
       reply = din.readUTF(); 
       System.out.println(reply); 
      } catch (IOException ex) { 
       Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex); 
      } 

      if (reply == null) { 
       JOptionPane.showMessageDialog(this, "No reply was recieved from the Raspberry Pi.", "Connection Error", JOptionPane.ERROR_MESSAGE); 
      } else if ("heartbeat".equals(reply)){ 
       // Do nothing. 
      } else if ("!".equals(reply.substring(0, 1))) { 
       JOptionPane.showMessageDialog(this, reply.substring(1), "Information", JOptionPane.WARNING_MESSAGE); 
      } 
     } 
    } 
+4

我建議你編寫最簡單的套接字回聲程序,以確保其餘代碼與此問題無關。 –

+1

免除了整個「解析」例程 - 讓pi回顯它看到的是套接字代碼是否正常工作。 – arcy

+0

並將flush()回撥。 – arcy

回答

1

你應該添加一個flush()叫你寫數據之後。套接字緩衝數據。

作爲使用阻塞套接字流的一般規則,每個流需要一個線程。試圖使用單個線程來管理輸入和輸出流是很危險的。

+0

對不起,我確實在那裏有沖水。它被註釋掉了,因爲我嘗試過,沒有沖洗。 – Miffiq

0

哇,我覺得真的很愚蠢。我忘記在桌面應用上啓動線程的原因。感謝jtahlborn試圖幫助!