/**
 * TcpServerDemo shows simple TCP/IP server programming in
 * Java. It waits for a connection, reads one byte of input from the
 * socket, sends out one byte (1 plus the input byte), and closes
 * the connection.
 *
 * Copyright (c) 1999-2002 E. W. Grundke.  All rights reserved.
 * @author  E. W. Grundke
 */

import java.io.*;
import java.net.*;

public class TcpServerDemo {
    public static void main (String args[]) {
	int port = 0;
        if (args.length < 1) { // must have 1+ command-line parameters
            System.out.println("Usage:   java TcpServerDemo <port>");
            System.out.println("Example: java TcpServerDemo 6763");
            System.exit(1);
        }
	try {
	    port = Integer.parseInt(args[0]);
	}
	catch(NumberFormatException e) {
            System.out.println("Error:  Bad port number: "+args[0]);
            System.exit(2);
	}
	ServerSocket sSocket = null;
        try {
            sSocket = new ServerSocket(port);  // the local port number
	}
        catch (IOException e) {
	    System.out.println(e);
	    System.exit(3);
	}
	while (true) {
	    try {
		Socket socket = sSocket.accept();  // wait 
		InputStream in = socket.getInputStream();
		byte b = (byte)in.read();  // read one byte
		OutputStream out = socket.getOutputStream();		
		out.write(b+1);  // add 1 and send it out!
		socket.close();
	    }
	    catch (IOException e) {System.out.println(e);}
	}
    }
}
