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端口
其他线程不运行了
{
ss.accept();
//启动一新的线程处理请求
}
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();
}
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();
}
}
final ServerSocket ss = new ServerSocket(6060);
new Thread(new Runnable() {
public void run() {
while (true) {
try {
ss.accept();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();