Skip to content

Commit

Permalink
Merge pull request #85 from cossin/master
Browse files Browse the repository at this point in the history
Improved handling of ephemeral ports
  • Loading branch information
miniway committed Oct 21, 2013
2 parents 228a944 + bbed32a commit 77cc1d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/zmq/SocketBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public boolean bind (final String addr_)
// Save last endpoint URI
options.last_endpoint = listener.get_address ();

add_endpoint(addr_, listener);
add_endpoint(options.last_endpoint, listener);
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/zmq/TcpAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public String toString() {

}

public int getPort(){
if (address != null)
return address.getPort();
return -1;
}

//Used after binding to ephemeral port to update ephemeral port (0) to actual port
protected void updatePort(int port){
address = new InetSocketAddress(address.getAddress(), port);
}

@Override
public void resolve(String name_, boolean ipv4only_) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/zmq/TcpListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ public String get_address() {
public int set_address(final String addr_) {
address.resolve(addr_, options.ipv4only > 0 ? true : false);

endpoint = address.toString();
try {
handle = ServerSocketChannel.open();
handle.configureBlocking(false);
handle.socket().setReuseAddress(true);
handle.socket().bind(address.address(), options.backlog);
if (address.getPort()==0)
address.updatePort(handle.socket().getLocalPort());
} catch (IOException e) {
close ();
return ZError.EADDRINUSE;
}

socket.event_listening(endpoint, handle);
socket.event_listening(address.toString(), handle);
return 0;
}

Expand Down

0 comments on commit 77cc1d4

Please sign in to comment.