GenericServer.java Source code

Java tutorial

Introduction

Here is the source code for GenericServer.java

Source

import java.io.BufferedOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class GenericServer {
    public static void main(String args[]) {
        int serverPort = 1234;
        try {
            ServerSocket server = new ServerSocket(serverPort);
            do {
                Socket client = server.accept();
                (new ServerThread(client)).start();
            } while (true);
        } catch (IOException ex) {
            System.exit(0);
        }
    }
}

class ServerThread extends Thread {
    Socket client;

    public ServerThread(Socket client) {
        this.client = client;
    }

    public void run() {
        try {
            ServiceOutputStream outStream = new ServiceOutputStream(
                    new BufferedOutputStream(client.getOutputStream()));
            ServiceInputStream inStream = new ServiceInputStream(client.getInputStream());
            ServiceRequest request = inStream.getRequest();
            while (processRequest(outStream)) {
            }
            client.close();
        } catch (IOException ex) {
            System.exit(0);
        }
    }

    public boolean processRequest(ServiceOutputStream outStream) {
        return false;
    }
}

class ServiceInputStream extends FilterInputStream {
    public ServiceInputStream(InputStream in) {
        super(in);
    }

    public ServiceRequest getRequest() throws IOException {
        ServiceRequest request = new ServiceRequest();
        return request;
    }
}

class ServiceOutputStream extends FilterOutputStream {
    public ServiceOutputStream(OutputStream out) {
        super(out);
    }
}

class ServiceRequest {
}