@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { try { socket.connect(new AFUNIXSocketAddress(socketFile), connectTimeout); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; } }
@Override protected AFUNIXSocketAddress addressFromHost(String host, int port) throws IOException { return new AFUNIXSocketAddress(socketFile, port); } }
@Override protected AFUNIXSocketAddress addressFromHost(String host, int port) throws IOException { String path = System.getProperty(PROP_SOCKET_DEFAULT); if (path == null || path.isEmpty()) { throw new IllegalStateException("Property not configured: " + PROP_SOCKET_DEFAULT); } File socketFile = new File(path); return new AFUNIXSocketAddress(socketFile, port); } }
@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { try { socket.connect(new AFUNIXSocketAddress(socketFile), connectTimeout); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; } }
@Override public Socket connect(String host, int portNumber, Properties props) throws SocketException, IOException { // Adjust the path to your MySQL socket by setting the // "junixsocket.file" property // If no socket path is given, use the default: /tmp/mysql.sock final File socketFile = new File(props.getProperty("junixsocket.file", "/tmp/mysql.sock")); socket = AFUNIXSocket.connectTo(new AFUNIXSocketAddress(socketFile)); return socket; } }
public class DevLogServer { public static void main(String[] args) throws IOException { final File socketFile = new File("/dev/log"); AFUNIXServerSocket server = AFUNIXServerSocket.newInstance(); try { server.bind(new AFUNIXSocketAddress(socketFile)); } catch (Exception e) { throw e; } } }
@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { try { socket.connect(new AFUNIXSocketAddress(socketFile), connectTimeout); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; } }
UnixPipe(IPCClient ipcClient, HashMap<String, Callback> callbacks, String location) throws IOException { super(ipcClient, callbacks); socket = AFUNIXSocket.newInstance(); socket.connect(new AFUNIXSocketAddress(new File(location))); }
@Override public Socket connectSocket(Socket socket, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException { int connTimeout = HttpConnectionParams.getConnectionTimeout(params); // int soTimeout = HttpConnectionParams.getSoTimeout(params) try { // socket.setSoTimeout(soTimeout) socket.connect(new AFUNIXSocketAddress(socketFile), connTimeout); // socket.connect(new AFUNIXSocketAddress(socketFile)) } catch (SocketTimeoutException e) { throw new ConnectTimeoutException("Connect to '" + socketFile + "' timed out"); } return socket; }
public void run() throws IOException { final File socketFile = new File(path); socketFile.deleteOnExit(); final ExecutorService executorService = Executors.newCachedThreadPool(); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); executorService.execute(new ClientConnection(this, server.accept())); } } finally { executorService.shutdown(); } }
@Override protected AFUNIXSocketAddress addressFromHost(String host, int port) throws IOException { host = stripBrackets(host); if (host.startsWith(FILE_SCHEME_PREFIX_ENCODED)) { try { host = URLDecoder.decode(host, "UTF-8"); } catch (Exception e) { throw (UnknownHostException) new UnknownHostException().initCause(e); } } if (!host.startsWith(FILE_SCHEME_PREFIX)) { throw new UnknownHostException(); } String path = host.substring(FILE_SCHEME_PREFIX.length()); if (path.isEmpty()) { throw new UnknownHostException(); } if (path.startsWith(FILE_SCHEME_LOCALHOST)) { path = path.substring(FILE_SCHEME_LOCALHOST.length()); } if (!path.startsWith("/")) { throw new UnknownHostException(); } File socketFile = new File(path); return new AFUNIXSocketAddress(socketFile, port); } }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); try (Socket sock = server.accept()) { System.out.println("Connected: " + sock); try (InputStream is = sock.getInputStream(); // OutputStream os = sock.getOutputStream()) { System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes("UTF-8")); os.flush(); byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read, "UTF-8")); } } } } } }
/** * Start the proxy * @return the proxy's listening socket address * @throws IOException on socket binding failure */ public InetSocketAddress start() throws IOException { listenSocket = new ServerSocket(); listenSocket.bind(new InetSocketAddress(listenHostname, listenPort)); logger.debug("Listening on {} and proxying to {}", listenSocket.getLocalSocketAddress(), unixSocketFile.getAbsolutePath()); acceptThread = new Thread(() -> { while (running) { try { Socket incomingSocket = listenSocket.accept(); logger.debug("Accepting incoming connection from {}", incomingSocket.getRemoteSocketAddress()); AFUNIXSocket outgoingSocket = AFUNIXSocket.newInstance(); outgoingSocket.connect(new AFUNIXSocketAddress(unixSocketFile)); new ProxyPump(incomingSocket, outgoingSocket); } catch (IOException ignored) { } } }); acceptThread.setDaemon(true); acceptThread.setName("tcp-unix-proxy-accept-thread"); acceptThread.start(); return (InetSocketAddress) listenSocket.getLocalSocketAddress(); }
sock.connect(new AFUNIXSocketAddress(socketFile)); } catch (SocketException e) { System.out.println("Cannot connect to server. Have you started it?");
/** * Create a plain {@link Socket} to docker API endpoint */ public Socket getSocket() throws IOException { try { final URI uri = new URI(dockerHost.getUri()); if ("unix".equals(uri.getScheme())) { final AFUNIXSocketAddress unix = new AFUNIXSocketAddress(new File("/var/run/docker.sock")); final Socket socket = AFUNIXSocket.newInstance(); socket.connect(unix); return socket; } final SSLConfig sslConfig = toSSlConfig(dockerHost.getCredentialsId()); if (sslConfig != null) { return sslConfig.getSSLContext().getSocketFactory().createSocket(uri.getHost(), uri.getPort()); } else { return new Socket(uri.getHost(), uri.getPort()); } } catch (Exception e) { throw new IOException("Failed to create a Socker for docker URI " + dockerHost.getUri(), e); } }
/** * Uses the {@link #socketFile} property as the path to initiate a file socket ignoring all parameters of this * method. * * @param host Unsupported, will be ignored * @param port Unsupported, will be ignored * * @return unix socket * * @throws IOException if the file system location cannot be URL decoded */ @Override public Socket createSocket(final String host, final int port) throws IOException { final File file; if (socketFile == null) { throw new IOException("socketFile (specified in org.ldaptive.ldapi.socketFile" + " or passed through factory constructor) MUST be specified to call this method."); } file = new File(URLDecoder.decode(socketFile, "UTF-8")); final AFUNIXSocketAddress localAddress = new AFUNIXSocketAddress(file); final AFUNIXSocket sock = AFUNIXSocket.newInstance(); sock.connect(localAddress); return sock; }
@Override public Socket createSocket(String host, int port) throws IOException { final RMIClientSocketFactory cf = defaultClientFactory; if (cf != null && port < AFUNIXRMIPorts.AF_PORT_BASE) { return cf.createSocket(host, port); } final AFUNIXSocketAddress addr = new AFUNIXSocketAddress(getFile(port), port); return AFUNIXSocket.connectTo(addr); }
@SuppressWarnings({"unchecked", "exports"}) @Override public <T extends Closeable> T connect(String hostname, int portNumber, PropertySet props, int loginTimeout) throws IOException { // Adjust the path to your MySQL socket by setting the // "junixsocket.file" property // If no socket path is given, use the default: /tmp/mysql.sock RuntimeProperty<String> prop = props.getStringProperty("junixsocket.file"); String sock; if (prop != null && !prop.isExplicitlySet()) { sock = prop.getStringValue(); } else { sock = "/tmp/mysql.sock"; } final File socketFile = new File(sock); this.rawSocket = AFUNIXSocket.connectTo(new AFUNIXSocketAddress(socketFile)); this.sslSocket = rawSocket; return (T) rawSocket; }
@Override public ServerSocket createServerSocket(int port) throws IOException { if (port == 0) { port = newPort(); final AFUNIXSocketAddress addr = new AFUNIXSocketAddress(getFile(port), port); final AnonymousServerSocket ass = new AnonymousServerSocket(port); ass.bind(addr); return ass; } final RMIServerSocketFactory sf = defaultServerFactory; if (sf != null && port < AFUNIXRMIPorts.AF_PORT_BASE) { return sf.createServerSocket(port); } final AFUNIXSocketAddress addr = new AFUNIXSocketAddress(getFile(port), port); return AFUNIXServerSocket.bindOn(addr); }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); try (Socket sock = server.accept()) { System.out.println("Connected: " + sock); try (InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream()) { byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read)); System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes()); os.flush(); } } } } } }