/************************************************************************
 * This library is free software; you can redistribute it and/or        *
 * modify it under the terms of the GNU Library General Public          *
 * License as published by the Free Software Foundation; either         *
 * version 2 of the License, or (at your option) any later version.     *
 *                                                                      *
 * This library is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
 * Library General Public License for more details.                     *
 *                                                                      *
 * You should have received a copy of the GNU Library General Public    *
 * License along with this library; if not, write to the                *
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,         *
 * Boston, MA  02111-1307, USA.                                         *
 *                                                                      *
 * The GNU Library General Public License file is included in           *
 * Online-Documentation                                                 *
 *                                                                      *
 * GNU's Homepage - http://www.gnu.org/                                 *
 *                                                                      *
 * Questions & comments referring to Hexi: please contact us:           *
 *                                                                      *
 * eMail:                                                               *
 *      ramsey@dbai.tuwien.ac.at                                        *
 * WWW:                                                                 *
 *      http://www.dbai.tuwien.ac.at/proj/ramsey/                       *
 *                                                                      *
 * (c) Wolfgang Slany 1987, Bidan Zhu 1996, Andreas Beer 1999           *
 * HexiServer.java                Version 2.5                31.07.1999 *
 ***********************************************************************/

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

public class HexiServer
{
	private static int portnumber = 8808;

	public static void main(String[] args)
	{
		ServerSocket serverSocket = null;
       		boolean listening = true;
		boolean ex_port = true;

		try
		{
			serverSocket = new ServerSocket(portnumber);
		}
		catch (IOException e)
		{
			System.err.println("HexiServer: Could not generate socket on port: " + portnumber + ", " + e.getMessage());
			ex_port = false;
		}
		if (ex_port) System.out.println ("HexiServer: started at port "+portnumber);
	        while (listening)
		{
			Socket clientSocket = null;
			try
			{
				clientSocket = serverSocket.accept();
				System.out.println("HexiServer: A client socket is generated "+clientSocket.hashCode());
			}
			catch (IOException e)
			{
				System.err.println("HexiServer: Accept failed. " + e.getMessage());
				continue;
			}
			catch (NullPointerException e)
			{
				System.err.println("HexiServer: Was not able to create socket. " + e.getMessage());
				continue;
			}
			HexiServerThread ServerThread = new HexiServerThread(clientSocket);
			System.out.println("A serverthread is generated "+ServerThread.hashCode());
			ServerThread.start();
		}

		/* Will never be reached: */

		try {
			serverSocket.close();
		}
		catch (IOException e)
		{
			System.err.println("Could not close server socket." + e.getMessage());
		}
	}
}