Java Code Examples for java.net.Socket#getChannel()
The following examples show how to use
java.net.Socket#getChannel() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketOrChannelConnectionImpl.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public SocketOrChannelConnectionImpl(ORB orb, Acceptor acceptor, Socket socket) { this(orb, acceptor, socket, (socket.getChannel() == null ? false : orb.getORBData().connectionSocketUseSelectThreadToWait()), (socket.getChannel() == null ? false : orb.getORBData().connectionSocketUseWorkerThreadForEvent())); }
Example 2
Source File: NetUtils.java From stratosphere with Apache License 2.0 | 5 votes |
public static void connect(Socket socket, SocketAddress endpoint, int timeout) throws IOException { if (socket == null || endpoint == null || timeout < 0) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { SocketIOWithTimeout.connect(ch, endpoint, timeout); } }
Example 3
Source File: SocketOrChannelConnectionImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public SocketOrChannelConnectionImpl(ORB orb, Acceptor acceptor, Socket socket) { this(orb, acceptor, socket, (socket.getChannel() == null ? false : orb.getORBData().connectionSocketUseSelectThreadToWait()), (socket.getChannel() == null ? false : orb.getORBData().connectionSocketUseWorkerThreadForEvent())); }
Example 4
Source File: TcpPeerServer.java From hadoop with Apache License 2.0 | 5 votes |
public static Peer peerFromSocket(Socket socket) throws IOException { Peer peer = null; boolean success = false; try { // TCP_NODELAY is crucial here because of bad interactions between // Nagle's Algorithm and Delayed ACKs. With connection keepalive // between the client and DN, the conversation looks like: // 1. Client -> DN: Read block X // 2. DN -> Client: data for block X // 3. Client -> DN: Status OK (successful read) // 4. Client -> DN: Read block Y // The fact that step #3 and #4 are both in the client->DN direction // triggers Nagling. If the DN is using delayed ACKs, this results // in a delay of 40ms or more. // // TCP_NODELAY disables nagling and thus avoids this performance // disaster. socket.setTcpNoDelay(true); SocketChannel channel = socket.getChannel(); if (channel == null) { peer = new BasicInetPeer(socket); } else { peer = new NioInetPeer(socket); } success = true; return peer; } finally { if (!success) { if (peer != null) peer.close(); socket.close(); } } }
Example 5
Source File: Message.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
public void setComms(Socket socket, ByteBuffer bb, MessageStats msgStats) throws IOException { this.sockCh = socket.getChannel(); if (this.sockCh == null) { setComms(socket, socket.getInputStream(), socket.getOutputStream(), bb, msgStats); } else { setComms(socket, null, null, bb, msgStats); } }
Example 6
Source File: TLSStreamReader.java From Openfire with Apache License 2.0 | 5 votes |
public TLSStreamReader(TLSWrapper tlsWrapper, Socket socket) throws IOException { wrapper = tlsWrapper; // DANIELE: Add code to use directly the socket channel if (socket.getChannel() != null) { rbc = ServerTrafficCounter.wrapReadableChannel(socket.getChannel()); } else { rbc = Channels.newChannel( ServerTrafficCounter.wrapInputStream(socket.getInputStream())); } inNetBB = ByteBuffer.allocate(wrapper.getNetBuffSize()); inAppBB = ByteBuffer.allocate(wrapper.getAppBuffSize()); }
Example 7
Source File: SocketOrChannelConnectionImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public SocketOrChannelConnectionImpl(ORB orb, Acceptor acceptor, Socket socket, boolean useSelectThreadToWait, boolean useWorkerThread) { this(orb, useSelectThreadToWait, useWorkerThread); this.socket = socket; socketChannel = socket.getChannel(); if (socketChannel != null) { // REVISIT try { boolean isBlocking = !useSelectThreadToWait; socketChannel.configureBlocking(isBlocking); } catch (IOException e) { RuntimeException rte = new RuntimeException(); rte.initCause(e); throw rte; } } this.acceptor = acceptor; serverRequestMap = Collections.synchronizedMap(new HashMap()); isServer = true; state = ESTABLISHED; }
Example 8
Source File: NetUtils.java From RDFS with Apache License 2.0 | 5 votes |
public static void connect(Socket socket, SocketAddress endpoint, int timeout, int ipTosValue) throws IOException { if (socket == null || endpoint == null || timeout < 0 || ipTosValue < NOT_SET_IP_TOS || ipTosValue > IP_TOS_MAX_VALUE) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { // set the socket IP_TOS value if (ipTosValue != NOT_SET_IP_TOS) { LinuxSystemCall.setIPTOSVal(ch, ipTosValue); } SocketIOWithTimeout.connect(ch, endpoint, timeout); } // There is a very rare case allowed by the TCP specification, such that // if we are trying to connect to an endpoint on the local machine, // and we end up choosing an ephemeral port equal to the destination port, // we will actually end up getting connected to ourself (ie any data we // send just comes right back). This is only possible if the target // daemon is down, so we'll treat it like connection refused. if (socket.getLocalPort() == socket.getPort() && socket.getLocalAddress().equals(socket.getInetAddress())) { LOG.info("Detected a loopback TCP socket, disconnecting it"); socket.close(); throw new ConnectException( "Localhost targeted connection resulted in a loopback. " + "No daemon is listening on the target port."); } }
Example 9
Source File: SocketInputWrapper.java From hadoop with Apache License 2.0 | 5 votes |
SocketInputWrapper(Socket s, InputStream is) { super(is); this.socket = s; this.hasChannel = s.getChannel() != null; if (hasChannel) { Preconditions.checkArgument(is instanceof SocketInputStream, "Expected a SocketInputStream when there is a channel. " + "Got: %s", is); } }
Example 10
Source File: TLSStreamHandler.java From Openfire with Apache License 2.0 | 4 votes |
/** * Creates a new TLSStreamHandler and secures the plain socket connection. When connecting * to a remote server then {@code clientMode} will be <code>true</code> and * {@code remoteServer} is the server name of the remote server. Otherwise {@code clientMode} * will be <code>false</code> and {@code remoteServer} null. * * @param socket the plain socket connection to secure * @param configuration the configuration for the connection * @param clientMode boolean indicating if this entity is a client or a server. * @throws java.io.IOException if an exception occurs */ public TLSStreamHandler(Socket socket, ConnectionConfiguration configuration, boolean clientMode) throws IOException { wrapper = new TLSWrapper(configuration, clientMode); tlsEngine = wrapper.getTlsEngine(); reader = new TLSStreamReader(wrapper, socket); writer = new TLSStreamWriter(wrapper, socket); // DANIELE: Add code to use directly the socket-channel. if (socket.getChannel() != null) { rbc = socket.getChannel(); wbc = socket.getChannel(); } else { rbc = Channels.newChannel(socket.getInputStream()); wbc = Channels.newChannel(socket.getOutputStream()); } initialHSStatus = HandshakeStatus.NEED_UNWRAP; initialHSComplete = false; netBBSize = tlsEngine.getSession().getPacketBufferSize(); appBBSize = tlsEngine.getSession().getApplicationBufferSize(); incomingNetBB = ByteBuffer.allocate(netBBSize); outgoingNetBB = ByteBuffer.allocate(netBBSize); outgoingNetBB.position(0); outgoingNetBB.limit(0); appBB = ByteBuffer.allocate(appBBSize); if (clientMode) { socket.setSoTimeout(0); socket.setKeepAlive(true); initialHSStatus = HandshakeStatus.NEED_WRAP; tlsEngine.beginHandshake(); } else if (configuration.getClientAuth() == Connection.ClientAuth.needed) { // Only REQUIRE client authentication if we are fully verifying certificates if (JiveGlobals.getBooleanProperty(ConnectionSettings.Server.TLS_CERTIFICATE_VERIFY, true) && JiveGlobals.getBooleanProperty(ConnectionSettings.Server.TLS_CERTIFICATE_CHAIN_VERIFY, true) && !JiveGlobals .getBooleanProperty(ConnectionSettings.Server.TLS_ACCEPT_SELFSIGNED_CERTS, false)) { tlsEngine.setNeedClientAuth(true); } else { // Just indicate that we would like to authenticate the client but if client // certificates are self-signed or have no certificate chain then we are still // good tlsEngine.setWantClientAuth(true); } } }
Example 11
Source File: ShellAgent.java From netbeans with Apache License 2.0 | 4 votes |
/** * Creates a new Connection using the agent. Throws an IOException if the * connection attempt is not successful or times out. If an old connection is * still open, it is closed and event is fired. * * @return the Connection the new connection * @throws IOException */ @NbBundle.Messages(value = { "MSG_AgentConnectionBroken=Control connection with JShell VM is broken, could not connect to agent", "MSG_AgentNotReady=The JShell VM is not ready for operation" }) public JShellConnection createConnection() throws IOException { JShellConnection old; synchronized (this) { if (closed) { throw new IOException(Bundle.MSG_AgentConnectionBroken()); } if (expectDebugger && debuggerMachine == null) { return null; } // old = connection; // connection = null; } /* if (old != null) { old.shutDown(); // notify about the old connection being trashed ShellLaunchEvent ev = new ShellLaunchEvent(mgr, old, false); mgr.fire((l) -> l.connectionClosed(ev)); } */ SocketChannel sc = SocketChannel.open(); sc.configureBlocking(true); Socket sock = sc.socket(); sock.connect(connectAddress, ShellLaunchManager.CONNECT_TIMEOUT); // turn to nonblocking mode sc.configureBlocking(false); boolean notify = false; JShellConnection con = new JShellConnection(this, sock.getChannel()); /* synchronized (this) { if (connection == null) { connection = con; notify = true; } else { con = connection; } } */ synchronized (this) { connections.add(con); } if (notify) { ShellLaunchEvent ev = new ShellLaunchEvent(mgr, con, false); mgr.fire((l) -> l.connectionInitiated(ev)); } return con; }
Example 12
Source File: NetUtils.java From hadoop with Apache License 2.0 | 4 votes |
/** * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but * also takes a local address and port to bind the socket to. * * @param socket * @param endpoint the remote address * @param localAddr the local address to bind the socket to * @param timeout timeout in milliseconds */ public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) throws IOException { if (socket == null || endpoint == null || timeout < 0) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (localAddr != null) { Class localClass = localAddr.getClass(); Class remoteClass = endpoint.getClass(); Preconditions.checkArgument(localClass.equals(remoteClass), "Local address %s must be of same family as remote address %s.", localAddr, endpoint); socket.bind(localAddr); } try { if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { SocketIOWithTimeout.connect(ch, endpoint, timeout); } } catch (SocketTimeoutException ste) { throw new ConnectTimeoutException(ste.getMessage()); } // There is a very rare case allowed by the TCP specification, such that // if we are trying to connect to an endpoint on the local machine, // and we end up choosing an ephemeral port equal to the destination port, // we will actually end up getting connected to ourself (ie any data we // send just comes right back). This is only possible if the target // daemon is down, so we'll treat it like connection refused. if (socket.getLocalPort() == socket.getPort() && socket.getLocalAddress().equals(socket.getInetAddress())) { LOG.info("Detected a loopback TCP socket, disconnecting it"); socket.close(); throw new ConnectException( "Localhost targeted connection resulted in a loopback. " + "No daemon is listening on the target port."); } }
Example 13
Source File: NetUtils.java From big-c with Apache License 2.0 | 4 votes |
/** * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but * also takes a local address and port to bind the socket to. * * @param socket * @param endpoint the remote address * @param localAddr the local address to bind the socket to * @param timeout timeout in milliseconds */ public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) throws IOException { if (socket == null || endpoint == null || timeout < 0) { throw new IllegalArgumentException("Illegal argument for connect()"); } SocketChannel ch = socket.getChannel(); if (localAddr != null) { Class localClass = localAddr.getClass(); Class remoteClass = endpoint.getClass(); Preconditions.checkArgument(localClass.equals(remoteClass), "Local address %s must be of same family as remote address %s.", localAddr, endpoint); socket.bind(localAddr); } try { if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { SocketIOWithTimeout.connect(ch, endpoint, timeout); } } catch (SocketTimeoutException ste) { throw new ConnectTimeoutException(ste.getMessage()); } // There is a very rare case allowed by the TCP specification, such that // if we are trying to connect to an endpoint on the local machine, // and we end up choosing an ephemeral port equal to the destination port, // we will actually end up getting connected to ourself (ie any data we // send just comes right back). This is only possible if the target // daemon is down, so we'll treat it like connection refused. if (socket.getLocalPort() == socket.getPort() && socket.getLocalAddress().equals(socket.getInetAddress())) { LOG.info("Detected a loopback TCP socket, disconnecting it"); socket.close(); throw new ConnectException( "Localhost targeted connection resulted in a loopback. " + "No daemon is listening on the target port."); } }
Example 14
Source File: SocketOutputStream.java From RDFS with Apache License 2.0 | 2 votes |
/** * Same as SocketOutputStream(socket.getChannel(), timeout):<br><br> * * Create a new ouput stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketOutputStream#SocketOutputStream(WritableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketOutputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }
Example 15
Source File: SocketInputStream.java From hadoop with Apache License 2.0 | 2 votes |
/** * Same as SocketInputStream(socket.getChannel(), timeout): <br><br> * * Create a new input stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketInputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }
Example 16
Source File: SocketInputStream.java From hadoop-gpu with Apache License 2.0 | 2 votes |
/** * Same as SocketInputStream(socket.getChannel(), timeout): <br><br> * * Create a new input stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketInputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }
Example 17
Source File: NetUtils.java From big-c with Apache License 2.0 | 2 votes |
/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br><br> * * Any socket created using socket factories returned by {@link NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
Example 18
Source File: NetUtils.java From stratosphere with Apache License 2.0 | 2 votes |
/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br> * <br> * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * @param socket * @param timeout * timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
Example 19
Source File: SocketInputStream.java From stratosphere with Apache License 2.0 | 2 votes |
/** * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout()) * :<br> * <br> * Create a new input stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long) * @param socket * should have a channel associated with it. * @throws IOException */ public SocketInputStream(Socket socket) throws IOException { this(socket.getChannel(), socket.getSoTimeout()); }
Example 20
Source File: SocketOutputStream.java From hadoop-gpu with Apache License 2.0 | 2 votes |
/** * Same as SocketOutputStream(socket.getChannel(), timeout):<br><br> * * Create a new ouput stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketOutputStream#SocketOutputStream(WritableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketOutputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }