2012-03-30 74 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