All pastes #879980 Raw Edit

Stuff

public text v1 · immutable
#879980 ·published 2008-01-29 18:49 UTC
rendered paste body
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
import java.nio.channels.*;

public class hw1_server{

	public static boolean KeepServerGoing;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		KeepServerGoing = true;
		// TODO Auto-generated method stub
		System.out.println("Kazan's Slow boring Java network directory listing server.");
		System.out.println("#Define POS Java");
		
		try {
		
			ServerSocket serv_socket = new ServerSocket(9877);
		
			// setup a non-blocking socket
			
			/*ServerSocketChannel serv_sk_chan = ServerSocketChannel.open();
			serv_sk_chan.configureBlocking(false);
			serv_sk_chan.socket().bind(new InetSocketAddress(9877));*/
			
			System.out.println("Waiting for client on port 9877.");
			
			int client_count = 0;
			
			while (KeepServerGoing) {
				/*SocketChannel accepted_client = serv_sk_chan.accept();
				
				// non-blocking accept returns null when a client isn't waiting
				if (accepted_client != null)
				{
					Socket client_sock = accepted_client.socket();
					// spawn a thread to service the client
					(new Thread(new hw1_srv_servicer(client_sock, client_count))).start();
					client_count++;
				}*/
				Socket accepted_client = serv_socket.accept();
				// spawn a thread to service the client
				(new Thread(new hw1_srv_servicer(accepted_client, client_count))).start();
				client_count++;
				
				
				try {
					Thread.sleep(250); // wait 250ms - so we don't hammer the system with out loop
				}
				catch (Exception e)
				{
					System.out.println("Sleep State interrupted");
					System.out.println(e);
				}
			}
			System.out.println("Server Terminating. " + Integer.toBinaryString(client_count) +  " Clients Serviced.");
		}
		catch (IOException e)
		{
			System.out.println(e);
		}
	}

}