//Proxy instance, proxy ip = 10.0.0.1 with port 8080 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080)); conn = new URL(urlString).openConnection(proxy);
@Override public Permission getPermission() throws IOException { URL url = getURL(); String hostname = url.getHost(); int hostPort = url.getPort() != -1 ? url.getPort() : HttpUrl.defaultPort(url.getProtocol()); if (usingProxy()) { InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address(); hostname = proxyAddress.getHostName(); hostPort = proxyAddress.getPort(); } return new SocketPermission(hostname + ":" + hostPort, "connect, resolve"); }
public LearnerHandlerBean(final LearnerHandler learnerHandler, final Socket socket) { this.learnerHandler = learnerHandler; InetSocketAddress sockAddr = (InetSocketAddress) socket.getRemoteSocketAddress(); if (sockAddr == null) { this.remoteAddr = "Unknown"; } else { this.remoteAddr = sockAddr.getAddress().getHostAddress() + ":" + sockAddr.getPort(); } }
/** * get remote host. * * @return remote host */ public String getRemoteHost() { return remoteAddress == null ? null : remoteAddress.getAddress() == null ? remoteAddress.getHostName() : NetUtils.filterLocalHost(remoteAddress.getAddress().getHostAddress()); }
/** * Returns {@link InetSocketAddress#getHostString()} if Java >= 7, * or {@link InetSocketAddress#getHostName()} otherwise. * @param addr The address * @return the host string */ public static String getHostname(InetSocketAddress addr) { return PlatformDependent.javaVersion() >= 7 ? addr.getHostString() : addr.getHostName(); }
@Override public Object getParameter(String name) { if (name.equals(ConnRouteParams.DEFAULT_PROXY)) { Proxy proxy = client.proxy(); if (proxy == null) { return null; } InetSocketAddress address = (InetSocketAddress) proxy.address(); return new HttpHost(address.getHostName(), address.getPort()); } throw new IllegalArgumentException(name); }
public static void dump(String host, int port) { Socket s = null; try { byte[] reqBytes = new byte[4]; ByteBuffer req = ByteBuffer.wrap(reqBytes); req.putInt(ByteBuffer.wrap("dump".getBytes()).getInt()); s = new Socket(); s.setSoLinger(false, 10); s.setSoTimeout(20000); s.connect(new InetSocketAddress(host, port)); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); os.write(reqBytes); byte[] resBytes = new byte[1024]; int rc = is.read(resBytes); String retv = new String(resBytes); System.out.println("rc=" + rc + " retv=" + retv); } catch (IOException e) { LOG.warn("Unexpected exception", e); } finally { if (s != null) { try { s.close(); } catch (IOException e) { LOG.warn("Unexpected exception", e); } } } }
LOG.info("connecting to {} {}", host, port); Socket sock; InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port); if (secure) { LOG.info("using secure socket"); SSLContext sslContext = x509Util.getDefaultSSLContext(); SSLSocketFactory socketFactory = sslContext.getSocketFactory(); SSLSocket sslSock = (SSLSocket) socketFactory.createSocket(); sslSock.connect(hostaddress, timeout); sslSock.startHandshake(); sock = new Socket(); sock.connect(hostaddress, timeout); sock.setSoTimeout(timeout); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write(cmd.getBytes()); outstream.flush();
@Test public void testBadRequest() throws IOException { int port = server.getPort(); String msg = "GET /status HTTP/1.1\n\n"; InetSocketAddress sockAddr = new InetSocketAddress("127.0.0.1", port); Socket sock = new Socket(); sock.connect(sockAddr); OutputStream out = sock.getOutputStream(); out.write(msg.getBytes(Charset.forName("UTF-8"))); out.flush(); byte[] buf = new byte[2048]; int bytesRead = sock.getInputStream().read(buf); Assert.assertTrue("Connection should have been closed", bytesRead == -1); }
@Override public void run() { try (Socket connection = new Socket(serverAddress.getAddress(), serverAddress.getPort()); OutputStream os = connection.getOutputStream()) { os.write(payload); os.flush(); } catch (Exception e) { e.printStackTrace(System.err); } } });
private void connectInternal() throws IOException { int port = url.getPort(); int connectTimeout = getConnectTimeout(); if (port <= 0) { port = FTP_PORT; if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) { controlSocket = new Socket(); } else { controlSocket = new Socket(currentProxy); InetSocketAddress addr = new InetSocketAddress(hostName, port); controlSocket.connect(addr, connectTimeout); connected = true; ctrlOutput = controlSocket.getOutputStream(); acceptSocket = new ServerSocket(0); dataPort = acceptSocket.getLocalPort(); acceptSocket.setSoTimeout(getConnectTimeout()); if (getDoInput()) { getFile();
workerPool = Executors.newCachedThreadPool(); port = PortAssignment.unique(); localServerAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), port); listeningSocket = new UnifiedServerSocket(x509Util, true); listeningSocket.bind(localServerAddress); if (useSecureClient) { clientSocket = x509Util.createSSLSocket(); clientSocket.connect(localServerAddress); } else { clientSocket = new Socket(); clientSocket.connect(localServerAddress); clientSocket.getOutputStream().write(new byte[] { 1, 2, 3, 4, 5 });
this.url = MoreObjects.firstNonNull( url, new URL(MoreObjects.firstNonNull( (String) this.getSettings().get(SETTING_URL), DEFAULT_LIBRATO_API_URL))); this.proxyPort = proxyPort != null ? proxyPort : Settings.getIntegerSetting(getSettings(), SETTING_PROXY_PORT, null); if (this.proxyHost != null && this.proxyPort != null) { this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, this.proxyPort)); } else { this.proxy = null;
private static boolean isPortOpen(String host, Integer port) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), 1000); return true; } catch (ConnectException | SocketTimeoutException e) { return false; } catch (IOException e) { throw new UncheckedIOException(e); } } }
private void connectHttpProxy(SocketAddress endpoint, int timeout) throws IOException { super.connect(httpProxy.address(), timeout); if (!(endpoint instanceof InetSocketAddress)) { throw new SocketException("Expected an InetSocketAddress to connect to, got: " + endpoint); } InetSocketAddress isa = (InetSocketAddress) endpoint; String httpConnect = "CONNECT " + isa.getHostName() + ":" + isa.getPort() + " HTTP/1.0\n\n"; getOutputStream().write(httpConnect.getBytes(IOUtils.UTF8)); checkAndFlushProxyResponse(); }
boolean isNewSocket = false; if (socketFactory != null) { socket = socketFactory.createSocket(); } else if( socket == null ) { socket = new Socket( proxy ); isNewSocket = true; } else if( socket.isClosed() ) { throw new IOException(); socket.setTcpNoDelay( isTcpNoDelay() ); socket.setReuseAddress( isReuseAddr() ); if( !socket.isBound() ) { socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout ); sslContext.init(null, null, null); SSLSocketFactory factory = sslContext.getSocketFactory(); socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
SocketFactory socket_factory = SSLSocketFactory.getDefault(); SocketAddress address = new InetSocketAddress( InetAddress.getByName(hostname), port ); sock = socket_factory.createSocket(); sock.connect(address, SOCKET_TIMEOUT); sock.setSoTimeout(SOCKET_TIMEOUT); reader = new ReaderGuacamoleReader(new InputStreamReader(sock.getInputStream(), "UTF-8")); writer = new WriterGuacamoleWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
public void connect(InetAddress host, int port, InetAddress localAddr, int localPort) throws IOException { socket = socketFactory.createSocket(); socket.bind(new InetSocketAddress(localAddr, localPort)); socket.connect(new InetSocketAddress(host, port), connectTimeout); onConnect(); }
InetAddress addr = ee.nextElement(); if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) { InetSocketAddress socketAddress = new InetSocketAddress(addr, 0); ServerSocket sock = new ServerSocket(); sock.bind(socketAddress); sock.close(); ActorSystem as = AkkaUtils.createActorSystem( new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port))); as.shutdown();
@Override OutputStream openNewOutputStream() throws IOException { final Socket socket = socketFactory.createSocket(); // Prevent automatic closing of the connection during periods of inactivity. socket.setKeepAlive(true); // Important not to cache `InetAddress` in case the host moved to a new IP address. socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), connectionTimeoutMs); return new BufferedOutputStream(socket.getOutputStream(), sendBufferSize); } }