我有一個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的
的)任何幫助,將不勝感激,它擊毀我的大腦
一切順利,
帕特里克。
感謝Brian,我目前正在爲此嘗試使用jgntp(http://code.google.com/p/jgntp)將服務器註冊爲「Machine running Growl」。我想知道Gntp在數據包傳輸方面與HTTP有多相似,但不知道如何讓它脫落,但即時到達! – patrickjquinn 2012-04-07 02:32:58