Writing Servers in Java

How can a server written in Java work like a UNIX daemon? To me it seems that a server cannot be an applet. Is it a standalone application?

Writing servers in Java is extremely easy. This is partly due to Java’s threading machinery and partly due to the rich set of networking class libraries that comes standard with Java.

Java servers are standalone Java applications (as opposed to applets) that run by themselves much as a UNIX daemon would. What’s amazing however, is that Java servers are platform-independent and take substantially fewer lines of code to write compared with the same programs written in C or C++.

Standalone Java server are started by directly invoking the Java interpreter on the Java class defining the main() method. Once started, the program typically binds and listens to a UDP or TCP port number and waits for incoming connections over the network. This works on any system supporting the TCP/IP protocol suite, be it a UNIX system or a Windows NT system. As soon as a connection request is detected, the program would either serve the request directly or spawn a Java thread to handle the request, and goes right back to listening to the port again.

Fundamental to the operation of Java servers is the ServerSocket class which is a standard part of the java.net package. As an example, the following is a simple Java date server that binds to TCP port 13000 and for every incoming request, provides the current date and time. To invoke the server, you simply need to run the Java interpreter and give it the name of the class file as follows:

        java DateServer————————-import java.io.*;import java.util.*;import java.net.*;public class DateServer {        public static void main(String argv[]) throws IOException {                ServerSocket serverSocket;                Socket s;                String peer;                PrintStream commandOut;                int port = 13000;                serverSocket = new ServerSocket(port);                while (true) {                        s = serverSocket.accept();                        commandOut = new PrintStream(new BufferedOutputStream(                                                s.getOutputStream()), true);                        commandOut.println(“The current time is ” + new Date());                        s.close();                }        }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS