From: David Reiss Date: Thu, 24 Jul 2008 19:20:32 +0000 (+0000) Subject: THRIFT-64. java: Allow TServerSocket to bind to a specific IP address X-Git-Tag: 0.2.0~459 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6144e428d5f55ef5cb81dbef1af8a140f94ad53c;p=common%2Fthrift.git THRIFT-64. java: Allow TServerSocket to bind to a specific IP address Add a new constructor to TServerSocket that accepts an InetSocketAddress and binds to that address. Define the old constructor (which just takes a port) in terms of the new one. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@679505 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/java/src/com/facebook/thrift/transport/TServerSocket.java b/lib/java/src/com/facebook/thrift/transport/TServerSocket.java index 14a925b3..b4ad6fb4 100644 --- a/lib/java/src/com/facebook/thrift/transport/TServerSocket.java +++ b/lib/java/src/com/facebook/thrift/transport/TServerSocket.java @@ -64,7 +64,15 @@ public class TServerSocket extends TServerTransport { * Creates just a port listening server socket */ public TServerSocket(int port, int clientTimeout) throws TTransportException { + this(new InetSocketAddress(port), clientTimeout); port_ = port; + } + + public TServerSocket(InetSocketAddress bindAddr) throws TTransportException { + this(bindAddr, 0); + } + + public TServerSocket(InetSocketAddress bindAddr, int clientTimeout) throws TTransportException { clientTimeout_ = clientTimeout; try { // Make server socket @@ -72,10 +80,10 @@ public class TServerSocket extends TServerTransport { // Prevent 2MSL delay problem on server restarts serverSocket_.setReuseAddress(true); // Bind to listening port - serverSocket_.bind(new InetSocketAddress(port_)); + serverSocket_.bind(bindAddr); } catch (IOException ioe) { serverSocket_ = null; - throw new TTransportException("Could not create ServerSocket on port " + port + "."); + throw new TTransportException("Could not create ServerSocket on address " + bindAddr.toString() + "."); } }