Question : how to use the same port without repeatly

i got the error

INFO: Exception in PortListener
SEVERE: null
java.net.BindException: Address already in use
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
        at java.net.ServerSocket.bind(ServerSocket.java:319)
        at java.net.ServerSocket.<init>(ServerSocket.java:185)
        at java.net.ServerSocket.<init>(ServerSocket.java:97)
        at tcplistener.PortListener$ToDoTask.run(PortListener.java:67)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)


how to solve the problem
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
public class PortListener extends HttpServlet {
   private Socket sock;
   private ServerSocket serverSocket;
   
   private Timer timer;
   private int a = 0;
   
public void init() throws ServletException

{
    try{
            timer = new Timer();
            timer.schedule(new ToDoTask(), 5000);
            
           
    }catch(Exception ex)
    {
       Logger.getLogger(PortListener.class.getName()).log(Level.SEVERE, null, ex) ;
    }
}

class ToDoTask extends TimerTask
 {
   public void run()
    {
       try
        {
                timer.cancel();
                InputStream is = getServletContext().getResourceAsStream("/WEB-INF/navl.properties");
                Properties  p  = new Properties();
                p.load(is);
                is.close();
                String pro = p.getProperty("dataPort");
                int port = Integer.parseInt(pro);
                serverSocket = new ServerSocket(port);
        
        while (true)
        {
           
            
            System.out.println("Listening on ...... " + port);
            sock = serverSocket.accept();
            Thread t  = new Thread(new ModuleHandler(sock));
            t.start();
                    
           
            //startthread(sock);
        }


		
        }catch(Exception ex) 
        {
                System.out.println("Exception in PortListener");
                Logger.getLogger(PortListener.class.getName()).log(Level.SEVERE, null, ex) ;
               
        }
        


    }


  }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
}      
       
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Answer : how to use the same port without repeatly

In this case, the 1st timer thread creates the ServerSocket and it might not be garbage collected when the 2nd timer thread run; that's why you get the bind exception.

You can get on which port the socket is listening by using the getLocalPort() method.

Random Solutions  
 
programming4us programming4us