Growing a redirect to a Java server
I have a java server that uses TCP and sockets to connect to an Android application (client) and sends strings (currently taken from the scanner object) which are then displayed as notifications by the client.
stores server code without import.
public class Server {
// 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
}
}}
Growl uses port 23053 for notification forwarding. What I hope to do is listen on 23053 and send any of that as a string to a client connected to 6789. Sadly Growl binds the port number so no new socket connection is made.
Does anyone have any ideas on how I can get notifications from the port number growl is using, or even just use growl as the server for the client itself (client code is very similar to servers, just using Socket instead of ServerSocket)
Any help on this would be greatly appreciated, her wrecking my brain
All the best
Patrick.
source to share
There is a round way to do this. If you are desperate, read on:
Growl can forward any notifications it receives to another machine that Growl is running on (configured in the Network tab). Growl uses GNTP protocol (TCP protocol: http://www.growlforwindows.com/gfw/help/gntp.aspx) to forward messages. The trick is that "the other machine running Growl" doesn't have to be another machine or run Growl as such, it just needs to appear to Growl as it is. Growl (on the Mac I believe you are using) will automatically detect any other computers on the network running Growl (using Bonjour and looking for the service name _gntp._tcp), so if your server advertises itself as supporting the GNTP protocol, Growl should show it in the list of available destinations. (Growl for Windows also allows you to manually add the host / port name for forwarding, but I don't believe the Mac version currently allows this).
So, you can configure Growl to send notifications to your server using the built-in capabilities. You will need to implement code on your server to receive GNTP packets (the format is very similar to HTTP headers) and parse them. Then you can forward notifications using your current server code.
Still with me? I said it was cool, but not only is it technically possible, but I've already created daemons that impersonate Growl so that I can get notifications redirected from Growl to my own code. Not suggesting this as a better idea, but just an alternative as you asked.
source to share