Java Daemons and Servers

Written by Larry Gray on . Posted in Daemons / Servers

Java Daemons and Servers

A server is a daemon but a daemon is not necessarily a server? I think so. I think all a daemon is, is a process that runs in the background, meaning no user interface. You start it up, it hides, runs and does it thing. A server does the same thing but accepts connections from clients which request it to do things for them. The server then does as requested if it can and responds to the client with details, data or files. A server's user interface is the client itself though actually they can have admin user interfaces that are web or GUI based. They usually do not have user interfaces but are simply configured, started and stopped.

If a java daemon(server) is started then in a process list you will see the java VM instead of the actual Java class name. If more than one is started you will only see multiple java VM's listed in the process list on the System. It might be difficult to know which process to kill if for example you wanted to kill a certain Java server. Below I list common types of servers.

  • Web Servers(files and webpages)
  • EMail Servers
  • Game Servers
  • Chat Servers
  • Messenger Servers
  • Time Servers
  • News Servers
  • Database (SQL)
  • Name Servers(DNS)

How do clients and servers communicate? TCP/IP protocol or (Transmission Control Protocol/Internet Protocol). In Java we use the sockets API. A socket connects to ports. TCP/IP defines just over 65,000 logical ports on a computer. This is really only a way of routing incoming and outgoing messages to and from clients and servers, computers and computers. The first few 1000 ports are the most used. Some ports have been standardized. For example HTTP is on port 80. You may run a web server on other ports. For example "http:80//www.somedomain.com/myfolder" is exactly the same as "http://www.somedomain.com/myfolder". But you may run a 2nd server on another port such as 8000. "http:8000//www.somedomain.com/myfolder". "http://" and"http:8000//" are two completely different web sites hosted on the same computer. The thing to remember here is that web browsers default to the standard port 80 unless you tell them the port explicitly.

 IPv4 Address. . . . . . . . . . . : 192.168.1.10
Use ipconfig to find your IP address like above. Then edit the Client source using your IP address.

Java Server

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(10100);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out =
                        new PrintWriter(socket.getOutputStream(), true);
                    out.println("Hello World from the Server!");
                } finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }
}
Java client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client{
    public static void main(String[] args) throws IOException {
        String serverAddress = "192.168.1.10";
        Socket s = new Socket(serverAddress, 10100);
        BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        System.out.println(answer);
        System.exit(0);
    }
}
Run the server using javaw.exe instead of java.exe to run it in the background. Then run the client.

c:\dev\java>javac Server.java

c:\dev\java>javaw Server

c:\dev\java>javac Client.java

c:\dev\java>java Client
Hello World from the Server!

c:\dev\java>

So can a mail server and web page server run on the same port? Absolutely not! At least not at the same time. This is what makes TCP/IP so nice. Its like a traffic cop for messaging between computers. TCP/IP and Client/Server design is what the Internet is. And can your desktop computer be a server? Sure, any computer can be a server or client. What do you think malware and spyware is? Someone has maliciously installed a server or (Trojan horse) on your computer which gives them complete access(and slows your computer at the same time).

Java Console Applications

Written by Larry Gray on . Posted in Console Applications

Java Console Applications

I gave a very small introduction to Java console application execution in the Java Desktop applications article. Java console applications are started in the same manner. I talked about path and classpath which are both very important. If the path and classpath are not set properly then your console application will fail to execute. I explain how to set the classpath on windows in the Desktop Applications article. There is much help on the internet for telling you how to set the system path to add java bin folder. If you do not add the JDK bin folder to the path then when you type 'java' at the command line or any other java tools you will get a not found error.

(DOS OR Windows Shell example)

set path=%path%;c:\dev\java\jdk8\bin;
set classpath=%classpath%;.;c:\dev\java\myproject\bin;
echo %path%    shows you the current path setting
echo %classpath%   shows you the current classpath setting

Java was not designed from the ground up for operation at the console(text screen) as a user interface. The standard Java API's and installation provide only very basic access to the system console. Java was made more for the windows GUI and is used more as background processes like servers. Many server applications are started from the console or from scripts which are run by the system. Some popular java utility applications operate from the command line. For more advanced console programming 3rd party API's are needed such as JCurses. JCurses might allow more complete control of the screen as in plotting characters to exact x,y positions. JCurses may let you alter background colors and foreground colors. Another 3rd party API is Java Native Access which I have not tried. This lib will allow you to output text in color to the console from what I understand. Another option for someone wanting to step back in time and have a terminal would be to make your own GUI component which emulates a text screen(JPanel), system input(KeyListener) and system output (plotting ASCII characters(drawImage()) in cells).

Below is an example of where I used JCurses with my Java Ledger project.

This slideshow requires JavaScript.

Java console or GUI applications may be given arguments when started from the command line in the console. This is merely a list of words which were separated by spaces in the command. i.e. 'java MyJavaApp Hello World' might return Hello (new line) then World (new line). Most command line commands have options. With Java arguments given to the main method (which is the starting point in any java application) the programmer may code options into the startup of the application using whatever syntax he likes such as '/s /q' or '-sq' or whatever seems appropriate.

Using the Java System object one may access the 'standard input', 'standard output' and ' standard error'. Standard input will allow the programmer to gather a line of text at the end(bottom) of the console from the user. System output allows the java programmer to output lines of text at will to the console. System execute command would allow the programmer to use a DOS command like CLS to clear the console. This isn't robust but it works and can get the job done. Also Java apps can handle redirection of input output in the same manner as any other system processes. I'm fairly sure you could direct the output of one Java application as input for another for example. You may certainly redirect the output from a Java application to a text file.

Pieces of example code.

// here we make a BufferedReader to get a line of text from the console.
br = new BufferedReader(new InputStreamReader(System.in)); // here we use System.in
//next in an infinite loop use
String input = br.readLine();
// then you can output the result back to the console
System.out.println("input:"+input);  // here we see the use of System.out
// if you throw exceptions in your code the output will go to System.err (error).

To be honest I have not seen many or any Java console apps that request key input from the console. I use the console mostly for quick and dirty debugging output. Though logging API's basically do the same thing better but with files or console. If a person wanted too, he could grab some input by using JOptionPane.showInputDialog() method. Using this in a console app means a GUI dialog will popup in front of the console and request input in a text field. When the user hits enter the dialog closes and text is returned as a String from the method call. You may do this without even using or displaying a JFrame.

In conclusion the console even with Java is useful and necessary at least for execution of applications and output from applications. I am even working on a text screen emulation component for the GUI as I described above for a retro text based adventure game using Java.