THRIFT-64. java: Allow TServerSocket to bind to a specific IP address
authorDavid Reiss <dreiss@apache.org>
Thu, 24 Jul 2008 19:20:32 +0000 (19:20 +0000)
committerDavid Reiss <dreiss@apache.org>
Thu, 24 Jul 2008 19:20:32 +0000 (19:20 +0000)
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

lib/java/src/com/facebook/thrift/transport/TServerSocket.java

index 14a925b..b4ad6fb 100644 (file)
@@ -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() + ".");
     }
   }