aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/network/TcpSocket.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/tigervnc/network/TcpSocket.java')
-rw-r--r--java/com/tigervnc/network/TcpSocket.java34
1 files changed, 14 insertions, 20 deletions
diff --git a/java/com/tigervnc/network/TcpSocket.java b/java/com/tigervnc/network/TcpSocket.java
index 9277dd14..1d127f50 100644
--- a/java/com/tigervnc/network/TcpSocket.java
+++ b/java/com/tigervnc/network/TcpSocket.java
@@ -171,29 +171,23 @@ public class TcpSocket extends Socket {
return ((InetSocketAddress)((SocketDescriptor)getFd()).socket().getRemoteSocketAddress()).getPort();
}
+ /* Tunnelling support. */
+ public static int findFreeTcpPort() {
+ java.net.ServerSocket sock;
+ int port;
+ try {
+ sock = new java.net.ServerSocket(0);
+ port = sock.getLocalPort();
+ sock.close();
+ } catch (java.io.IOException e) {
+ throw new SocketException("unable to create socket: "+e.toString());
+ }
+ return port;
+ }
+
private boolean closeFd;
static LogWriter vlog = new LogWriter("TcpSocket");
}
-/* Tunnelling support. */
-/*
-public int findFreeTcpPort() {
- int sock;
-
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- throw SocketException("unable to create socket", errorNumber);
-
- int port = 0;
- if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) < 0)
- throw SocketException("unable to find free port", errorNumber);
-
- socklen_t n = sizeof(addr);
- if (getsockname (sock, (struct sockaddr *)&addr, &n) < 0)
- throw SocketException("unable to get port number", errorNumber);
-
- closesocket(sock);
- return ntohs(addr.sin_port);
-}
-*/