我有一个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