Lacek
Lacek

Reputation: 1625

Java 7 doesn't throw BindException when binding an already used port using ServerSocket

I'm experimenting on ServerSocket in Java on Windows 7 x64. I wrote a little program that host a HTTP server on port 8080 and only returns a static HTML response that contains the toString() of the class loader.

What I did in the program mainly:

  1. Create a ServerSocket
  2. call setReuseAddress(false) on the serverSocket
  3. Bind port 8080 to this socket
  4. Use a forever loop to accept socket and give response

First I tried with JRE 1.6.0_23 and everything is great: first instance launched and responds normally, second instance cannot be launched since exception is thrown:

Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind

Unexpected thing happens when I tried with JRE 1.7.0_5: both instance can be launched successfully but only the first instance gives responses. After the first instance is kill, the second instance then starts to responds.

Am I doing anything wrong or is this a bug of JRE 7?

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerSocket {
    private static final String HEADER = "HTTP/1.1 200 OK\r\n" + "Content-type: text/html\r\n"
            + "Connection: close\r\n" + "\r\n";

    private static final int PORT = 8080;

    private static void handle(Socket socket) {
        System.out.println(socket.getInetAddress() + ":" + socket.getPort());
        StringBuilder buffer = new StringBuilder();
        buffer.append(HEADER);
        buffer.append(TestServerSocket.class.getClassLoader());
        try {
            socket.getOutputStream().write(buffer.toString().getBytes());
        } catch (IOException e) {
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }

    }

    public static void main(String[] args) throws IOException {
        int port;

        try {
            port = Integer.parseInt(args[0]);
        } catch (Exception e) {
            port = PORT;
        }

        final ServerSocket server = new ServerSocket();
        server.setReuseAddress(false);
        server.bind(new InetSocketAddress(port));

        // Terminator thread, stop when Ctrl-D is entered
        new Thread() {
            public void run() {
                try {
                    while (System.in.read() != 4);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    server.close();
                } catch (IOException e) {
                }
                System.exit(0);
            }
        }.start();

        System.out.println("Listening on: " + port);
        Socket client = null;
        while (true) {
            try {
                client = server.accept();
                handle(client);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Upvotes: 2

Views: 1592

Answers (1)

Anupam Saini
Anupam Saini

Reputation: 2421

To Isolate the problem, I would recommend that you run the following test code. Apache HttpCore basic server. It's standard API and uses ServerSocket in this particular example, so there is a very small chance that it would fail on your environment ( java 7).

In case it fails you will know for sure problem is not with your code. Meanwhile I will try your code on JDK 7 on my work-machine and will update.

Upvotes: 1

Related Questions