2012-11-03 190 views
0

我正在尝试构建java微博应用程序 我已完成代码,但由于以下错误无法连接到我自己的计算机(我谷歌和有人说我需要更改,端口号,我改变了端口号和什么都没有发生)客户机/服务器java应用程序中的JVM_Bind错误

Exception in thread "Thread-4" java.net.BindException: Address already in use: JVM_Bind 

下面是该服务器的代码:

public class server extends Thread { 
    public Socket client = null; 
    public ServerSocket server = null; 
    private int port = 4444; 

public void run(){ 
    while (true){ //loop waits for incomming connection 
     try { //start the server and listen to a port 
       server = new ServerSocket(port); 
      }catch (IOException e){ 
       System.err.println(e); 
       System.exit(-1); 
      } 

      try { //establish a connection when receiving request 
       client = server.accept(); 
      }catch (IOException e){ 
       System.err.println(e); 
       System.exit(1); 
      } 

      Thread t = new Thread(new Connection(client)); 
      t.start(); 
     } 
    } 
} 

这是启动服务器和侦听端口代码4444

Server server = new Server(); 
server.start(); //to listen to a port 

谢谢

+0

你试过了哪些端口? – thedayofcondor

+0

4444.默认端口总是4444谢谢 – kaboom

+0

也我试过7777和8888 – kaboom

回答

1

您必须在进入循环之前创建的ServerSocket。目前你正试图在每一次迭代中创建它,这是没有意义的,而且你没有关闭它,所以第二次创建失败。

相关问题