Forum Stats

  • 3,752,058 Users
  • 2,250,452 Discussions
  • 7,867,705 Comments

Discussions

java.net.BindException: Address already in use

skidmarks
skidmarks Member Posts: 103
edited May 11, 2011 7:15PM in New To Java
On Windows XP I get no error. On Redhat Linux I get the 'Address already in use' error. I've logged off then logged on and started the software from the command line and in eclipse with the same results. The point of error is executed three times (diagnostic output below). The socket binding is for three different port (all with IPAddress 127.0.0.1) and this is the only time it is executed.

Any idea why the difference in the two OSs and how to fix it?
        if (socket.getPort() != 0 || !socket.isUnresolved()) {
            try {
                datagramSocket = new DatagramSocket(socket.getPort());
                if (!datagramSocket.isBound())
                    datagramSocket.bind(socket);
            } catch (SocketException e1) {
                Diagnostic.out(this.name + " " + socketName, Constants.DiagnosticMSG.SOCKETEXCEPTION);
                e1.printStackTrace();
                return null;
            }
        } else {
            System.out.format("Port = 0 (%d) or Socket resolved %b%n"
                             ,  socket.getPort()
                             , !socket.isUnresolved());
        }
Diagnostic Output.
The first two errors do not occur in XP. The last two errors are a consequence of the first two failing.
java.net.BindException: Address already in use
	at java.net.PlainDatagramSocketImpl.bind0(Native Method)
	at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:77)
	at java.net.DatagramSocket.bind(DatagramSocket.java:369)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:228)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:281)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:253)
	at communication.SocketConnection.openSocket(SocketConnection.java:238)
	at communication.SocketConnection.init(SocketConnection.java:216)
	at communication.AbstractCommunication.superInit(AbstractCommunication.java:105)
	at communication.UDPCommunication.init(UDPCommunication.java:85)
	at server.UUTComm.init(UUTComm.java:81)
	at server.Server.init(Server.java:122)
	at execute.ExecuteCommand.init(ExecuteCommand.java:93)
	at parser.FileMode.main(FileMode.java:1196)
java.net.BindException: Address already in use
	at java.net.PlainDatagramSocketImpl.bind0(Native Method)
	at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:77)
	at java.net.DatagramSocket.bind(DatagramSocket.java:369)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:228)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:281)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:253)
	at communication.SocketConnection.openSocket(SocketConnection.java:238)
	at communication.SocketConnection.init(SocketConnection.java:216)
	at communication.AbstractCommunication.superInit(AbstractCommunication.java:105)
	at communication.UDPCommunication.init(UDPCommunication.java:85)
	at communication.UDPCommunication.receive(UDPCommunication.java:99)
	at server.UUTComm.run(UUTComm.java:49)
java.net.BindException: Address already in use
	at java.net.PlainDatagramSocketImpl.bind0(Native Method)
	at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:77)
	at java.net.DatagramSocket.bind(DatagramSocket.java:369)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:228)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:281)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:253)
	at communication.SocketConnection.openSocket(SocketConnection.java:238)
	at communication.SocketConnection.init(SocketConnection.java:216)
	at communication.AbstractCommunication.superInit(AbstractCommunication.java:105)
	at communication.UDPCommunication.init(UDPCommunication.java:85)
	at communication.UDPCommunication.send(UDPCommunication.java:127)
	at communication.UDPCommunication.send(UDPCommunication.java:151)
	at server.AbstractServerThread.run(AbstractServerThread.java:104)
	at java.lang.Thread.run(Thread.java:636)
Tagged:

Answers

  • 802316
    802316 Member Posts: 532
    edited May 11, 2011 3:07PM
    Are you sure there is no service using that port already? Can you try a different port e.g. a port above 1024?
  • 796440
    796440 Member Posts: 19,179
    For the PORT in question, execute, as root:
    netstat -anp | sed -n -e2p -e/PORT/p
    That will tell you if the port is open and the pid of the process that has opened it.
    796440
  • skidmarks
    skidmarks Member Posts: 103
    Thanks Peter and JVerd;

    Peter: All my ports are >= 1024
    JVerd: Ya' nailed it. I checked the computer and the port is used by Java. There are some additional issues to be check (as to why other developer platforms worked) but I have a good starting point.

    art
  • 796440
    796440 Member Posts: 19,179
    skidmarks wrote:
    JVerd: Ya' nailed it. I checked the computer and the port is used by Java.
    It's not "Java" that's using it. It's some particular application, running under the umbrella of the java executable. You can do some more digging to see what that particular process is. Maybe an old copy of your app that never got killed. Maybe some other application that just happened to pick the same port.
    There are some additional issues to be check (as to why other developer platforms worked) but I have a good starting point.
    It has nothing to do with what platform you're on. The particular linux host you're trying now happens to have something running that has grabbed that port, and the other host does not have that same app running or if it does, it's using a different port. The only way the platform comes into play is if it is an old copy of your app and you have some platform-dependent code in there that causes it to die on one platform but not on the other.
  • skidmarks
    skidmarks Member Posts: 103
    Thanks jVerd;

    Yes and No to the Linux stuff. On my machine, during startup an unwelcome process is initiated which uses the port. Nothing to do with my application or usage. I'm trying to track this down as we speak (or e-mail as the case may be). This is probably a configuration problem unique to the workstation I took over. In any case, I hope to either find out what the fool thing is and how to not-have-it-started, or I'll just kill it and watch the smoke.

    art
This discussion has been closed.