java_tcpsocket_server

package java_learning;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class java_tcpsocket_server {
	private int port = 3122;
	private ServerSocket serverSocket;
	public java_tcpsocket_server() throws Exception{
		  serverSocket = new ServerSocket(port,100);//绑定端口号,设置最大连接数,即连接请求队列长度
		  System.out.println("服务器启动!");
    }
	
public void Doservice(){
	while(true){
		Socket socket = null;
		try {
		    socket = serverSocket.accept();
		    System.out.println("New connection accepted "+
		    socket.getInetAddress()+":"+socket.getPort());
		} catch (IOException e) {
		    e.printStackTrace();
		}finally{
		    if(socket!=null){
		    	try {
		    		socket.close();
		    	} catch (IOException e) {
		    		e.printStackTrace();
		    	}
		    }
		}
	}
}
public static void main(String[] args) throws Exception{
 java_tcpsocket_server server = new java_tcpsocket_server();
  Thread.sleep(60000*10);
  server.Doservice();
 }
} 


你可能感兴趣的:(java_learning)