public static void main(String[] args) throws Exception {
    ServerSocket ss = new ServerSocket(6060);
    while(true) {
        //
        Thread.sleep(100000);
    }
}这样当程序运行一会后,通过netstat -ano就看到6060端口下没有进程了
public static void main(String[] args) throws Exception {
    Socket s = new Socket();
    s.bind(new InetSocketAddress("127.0.0.1", 6060));
    while(true) {
        //
        Thread.sleep(100000);
    }
}这样做,netstat -ano根本就看不到6060下有进程
请各位给出代码,可以让JAVA程序持续的不断的占用6060端口

解决方案 »

  1.   

    ss.accept()不行啊
    其他线程不运行了
      

  2.   

    while(true)
    {
    ss.accept();
    //启动一新的线程处理请求
    }
      

  3.   


    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(6060);
        new Thread(new Runnable() {
    public void run() {
        while (true) {
        try {
    ss.accept();
        } catch (Exception e) {
    e.printStackTrace();
        }
            }
         }
         }).start();
    }
      

  4.   

    将ss.accept放到一个单独的线程中。
    Thread:import java.net.*;
    public class ListenThread extends Thread{
    public ListenThread(){

    }
    public void run(){
    try{
    ServerSocket ss = new ServerSocket(6060);
    ss.accept();
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    }main:public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    ListenThread thread = new ListenThread();
    thread.start();

    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
      

  5.   


    final ServerSocket ss = new ServerSocket(6060);
        new Thread(new Runnable() {
        public void run() {
            while (true) {
            try {
             ss.accept();
            } catch (Exception e) {
             e.printStackTrace();
            }
            }
         }
     }).start();