package com.anxpp.io.calculator.bio;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * BIOԴ
 * @author yangtao__anxpp.com
 * @version 1.0
 */
public final class ServerNormal {
	//ĬϵĶ˿ں
	private static int DEFAULT_PORT = 12345;
	//ServerSocket
	private static ServerSocket server;
	//ݴü˿ڣûв·ʹĬֵ
	public static void start() throws IOException{
		//ʹĬֵ
		start(DEFAULT_PORT);
	}
	//ᱻʣ̫ҪЧʣֱӽзͬ
	public synchronized static void start(int port) throws IOException{
		if(server != null) return;
		try{
			//ͨ캯ServerSocket
			//˿ںϷҿУ˾ͼɹ
			server = new ServerSocket(port);
			System.out.println("˿ںţ" + port);
			Socket socket;
			//ͨѭͻ
			//ûпͻ˽룬acceptϡ
			while(true){
				socket = server.accept();
				//µĿͻ˽ʱִĴ
				//Ȼ󴴽һµ̴߳Socket·
				new Thread(new ServerHandler(socket)).start();
			}
		}finally{
			//һЩҪ
			if(server != null){
				System.out.println("ѹرա");
				server.close();
				server = null;
			}
		}
	}
}