List of usage examples for java.nio.channels SocketChannel open
public static SocketChannel open(SocketAddress remote) throws IOException
From source file:org.apache.pig.shock.SSHSocketImplFactory.java
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { String socksHost = System.getProperty("socksProxyHost"); Socket s;/* www . j a va 2s.com*/ InetSocketAddress addr = new InetSocketAddress(host, port); if (socksHost != null) { Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress(socksHost, 1080)); s = new Socket(proxy); s.connect(addr); } else { log.error(addr); SocketChannel sc = SocketChannel.open(addr); s = sc.socket(); } s.setTcpNoDelay(true); return s; }
From source file:Proxy.java
/** We handle only non-SSL connections */ void loop(Selector selector) { Set ready_keys;//w ww .j a va2 s .co m SelectionKey key; ServerSocketChannel srv_sock; SocketChannel in_sock, out_sock; InetSocketAddress src, dest; while (true) { if (verbose) log("[Proxy] ready to accept connection"); // 4. Call Selector.select() try { selector.select(); // get set of ready objects ready_keys = selector.selectedKeys(); for (Iterator it = ready_keys.iterator(); it.hasNext();) { key = (SelectionKey) it.next(); it.remove(); if (key.isAcceptable()) { srv_sock = (ServerSocketChannel) key.channel(); // get server socket and attachment src = (InetSocketAddress) key.attachment(); in_sock = srv_sock.accept(); // accept request if (verbose) log("Proxy.loop()", "accepted connection from " + toString(in_sock)); dest = (InetSocketAddress) mappings.get(src); // find corresponding dest if (dest == null) { in_sock.close(); log("Proxy.loop()", "did not find a destination host for " + src); continue; } else { if (verbose) log("Proxy.loop()", "relaying traffic from " + toString(src) + " to " + toString(dest)); } // establish connection to destination host try { out_sock = SocketChannel.open(dest); // uses thread pool (Executor) to handle request, closes socks at end handleConnection(in_sock, out_sock); } catch (Exception ex) { in_sock.close(); throw ex; } } } } catch (Exception ex) { log("Proxy.loop()", "exception: " + ex); } } }
From source file:org.apache.awf.web.SystemTest.java
@Test public void sendGarbageTest() throws IOException { InetSocketAddress socketAddress = new InetSocketAddress(PORT); SocketChannel channel = SocketChannel.open(socketAddress); channel.write(ByteBuffer.wrap(new byte[] { 1, 1, 1, 1 } // garbage ));//from www. j av a2s .co m channel.close(); }
From source file:org.apache.zookeeper.server.quorum.QuorumPeerMainTest.java
/** * verify if bad packets are being handled properly * at the quorum port//from w ww . j a v a 2 s . c o m * @throws Exception */ @Test public void testBadPackets() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); final int CLIENT_PORT_QP2 = PortAssignment.unique(); int electionPort1 = PortAssignment.unique(); int electionPort2 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort1 + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort2 + ";" + CLIENT_PORT_QP2; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); q1.start(); q2.start(); Assert.assertTrue("waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); Assert.assertTrue("waiting for server 2 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); byte[] b = new byte[4]; int length = 1024 * 1024 * 1024; ByteBuffer buff = ByteBuffer.wrap(b); buff.putInt(length); buff.position(0); SocketChannel s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort1)); s.write(buff); s.close(); buff.position(0); s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort2)); s.write(buff); s.close(); ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this); waitForOne(zk, States.CONNECTED); zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1"); zk.close(); q1.shutdown(); q2.shutdown(); }