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).