2012-03-30 72 views
3

我有一個java服務器,它使用TCP和套接字連接到Android應用程序(客戶端)併發送字符串(當前從掃描器對象中獲取),然後顯示爲客戶端的通知。咆哮轉發到Java服務器

繼承人的服務器代碼沒有所有的進口。

公共類服務器{

// define our Main method 
public static void main(String[] args) throws IOException { 
    // set up our Server Socket, set to null for the moment. 
    ServerSocket serverSocket = null; 
      boolean isConnected = false; 

    // Lets try and instantiate our server and define a port number . 
    try { 
     serverSocket = new ServerSocket(6789); 
       isConnected = true; 
     System.out.println("*** I am the Server ***\n"); 
     // make sure to always catch any exceptions that may occur. 
    } catch (IOException e) { 
     // always print error to "System.err" 
     System.err.println("Could not listen on port: 6789."); 
     System.exit(1); 
    } 

    // We always want to check for connection from Clients, so lets define 
    // a for ever loop. 
    for (;;) { 
     // define a local client socket 
     Socket clientSocket = null; 
     // lets see if there are any connections and if so, accept it. 
     try { 
      clientSocket = serverSocket.accept(); 
      // don't forget to catch your exceptions 
     } catch (IOException e) { 
      System.err.println("Accept failed."); 
      System.exit(1); 
     } 

     // Prepare the input & output streams via the client socket. 

     // fancy way of saying we want to be able to read and write data to 
     // the client socket that is connected. 

     BufferedReader inFromClient = new BufferedReader(new InputStreamReader(
       clientSocket.getInputStream())); 
     PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), 
       true); 

       while (isConnected) { 
     // read a sentence from client 
     String hiFromClient = inFromClient.readLine(); 


        // Set up the logging system and timestamp and log message. 

        Calendar currentDate = Calendar.getInstance(); 
     SimpleDateFormat formatter= 
     new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss"); 
     String dateNow = formatter.format(currentDate.getTime()); 


     try{ 
      // Create file 
      File fstream = new File("log.txt"); 
         FileWriter out = new FileWriter(fstream); 
           out.write(hiFromClient + " " + dateNow); 

           //Close the output stream 
           out.close(); 
        } catch (Exception e){//Catch exception if any 
          System.err.println("Error: " + e.getMessage()); 
        } 


     // Print the client sentence to the screen 
     System.out.println("The Client said: "+hiFromClient); 

     // Reply to the client 

        Scanner scanner = new Scanner(System.in); 
        String sentence = scanner.nextLine(); 

     outToClient.println(sentence); 
        System.out.println("The Server said: " + sentence); 

     }  
     // always remember to close all connections. 


     inFromClient.close(); // the reader 
     outToClient.close(); // the writer 
     clientSocket.close(); // and the client socket 
    } 
}} 

低吼使用端口23053的通知轉發。我希望做的事情是在23053上收聽,並以字符串形式發送任何內容到6789處連接的客戶端。可悲的是,Growl綁定了端口號,因此無法建立新的Socket連接。

任何人都可以從端口號獲取通知的任何想法growl使用,甚至只是使用growl作爲客戶端本身的服務器(客戶端的代碼與使用Socket的方式非常類似於服務器ServerSocket的

的)任何幫助,將不勝感激,它擊毀我的大腦

一切順利,

帕特里克。

回答

1

有一個圓的方式,你可以做到這一點。如果您絕望,請繼續閱讀:

Growl可以將收到的任何通知轉發給運行Growl的另一臺計算機(在「網絡」選項卡上配置)。 Growl使用GNTP協議(基於TCP的協議:http://www.growlforwindows.com/gfw/help/gntp.aspx)轉發消息。訣竅是,'其他機器運行咆哮'不必真的是另一臺機器或本身運行咆哮,它只需要看起來像咆哮。 Growl(在Mac上,我假設你正在使用的)會自動檢測運行Growl的網絡上的任何其他機器(使用Bonjour並查找_gntp._tcp服務名稱),所以如果你的服務器宣稱自己支持GNTP協議,咆哮應顯示它在可用目的地列表中。 (Growl for Windows也可以讓你手動添加一個主機名/端口來轉發,但我不認爲目前Mac版本允許)。

那麼你可以配置Growl使用其已經內置的功能將通知轉發到你的服務器。你將不得不在你的服務器中實現代碼來接收GNTP數據包(格式與HTTP頭非常相似)並解析它們。然後,您可以使用您當前的服務器代碼轉發通知。

還在我身邊嗎?我確實說過這是一回事,但它不僅在技術上是可行的,但是我之前已經構建了一個低吼模擬守護進程,以便我可以將從低吼傳遞到我的自定義代碼的通知。不要認爲它是最好的主意,而只是你提出的一個選擇。

+0

感謝Brian,我目前正在爲此嘗試使用jgntp(http://code.google.com/p/jgntp)將服務器註冊爲「Machine running Growl」。我想知道Gntp在數據包傳輸方面與HTTP有多相似,但不知道如何讓它脫落,但即時到達! – patrickjquinn 2012-04-07 02:32:58