Skip to content

Bind address with automatic port selection from ephemeral port range needs to be updated #4

Description

@mpkusnierz

If you try to create a TcpCrusher bound to port 0 - the serverSocketChannel will use an automatically allocated port from the ephemeral port range; however because the TcpCrusher will be listening on this port, the connecting application needs to know what this port is so that it can connect. Currently the TcpCrusher doesn't expose the actual port that was allocated; so calling code is unable to know which port should be used to connect.

In the TcpAcceptor, I suggest setting the bindAddress aftering the channel.bind is called; and then setting the bindAddress to the localAddress returned by the serverSocketChannel

    if (bindAddress.getPort() == 0 && serverSocketChannel.getLocalAddress() instanceof InetSocketAddress) {
    	this.bindAddress = (InetSocketAddress) serverSocketChannel.getLocalAddress();
    } else {
    	this.bindAddress = bindAddress;
    }

And then adding a getter for the bindAddress

public InetSocketAddress getBindAddress() {
	return bindAddress;
}

Within the TcpCrusher class the bindAddress parameter should not be final, and within the open method, when the acceptor is created, I suggest that the bindAddress variable is reset (before the logging to say that the crusher is open) by calling the new getter on the acceptor:

if (bindAddress.getPort() == 0) {
    this.bindAddress = this.acceptor.getBindAddress();
}

Calling code is then able to inspect the port that was allocated by simply calling the existing TcpCrusher.getBindAddress().getPort() method

TcpCrusher.java.txt
TcpAcceptor.java.txt

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions