java.net.BindException How can I clear the sockets or what ever is causing it?

Posted by user2266067 on Stack Overflow See other posts from Stack Overflow or by user2266067
Published on 2013-07-02T22:52:55Z Indexed on 2013/07/02 23:05 UTC
Read the original article Hit count: 197

Filed under:
|
|

I need some help with, I guess a simple networking related problem I'm having. It will also help me better understand how all this works by knowing what isn't being .close()'ed. I'm sure this is pretty simple, but for me its all very new. This is the client program. I can most likely append the server then, if I can figure this out. Thanks

    public class Server {
    public static void main(String[] args) {
        start();
    }

    static int start = 0;

    public static void start() {
        try {
            ServerSocket serverSocket = new ServerSocket(4567);
            Socket socket = serverSocket.accept();
            //1) Take and echo input (In this case a message)
            BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = bf.readLine();
            System.out.println("Message recieved from Client:" + message);
            //2) Response of client message
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
            printWriter.println("Server echoing back the message ' " + message + " ' from Client");
        } catch (IOException e) {
            System.out.println("e " + e);
            System.exit(-1);
        }
        start++;

            clearUp();

        if (start < 5) {
            System.out.println("Closing binds and Restarting" + start);

            start();
        }
    }

    public void clearUp(){
        //How would I clear the stuff that is left bound
          so I can restart via start() and avoid the
          java.net.BindException: Address already in use: JVM_Bind ?
    }

}

How would I clear the stuff that is left bound so I can restart via start() and avoid java.net.BindException: Address already in use: JVM_Bind ?

© Stack Overflow or respective owner

Related posts about java

Related posts about networking