List of usage examples for java.net Socket getChannel
public SocketChannel getChannel()
From source file:Main.java
License:asdf
public static void main(String args[]) throws Exception { Socket s = new Socket("internic.net", 43); InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); String str = "asdfasdfasdf\n"; byte buf[] = str.getBytes(); out.write(buf);//from w ww .j av a 2 s. c o m int c; while ((c = in.read()) != -1) { System.out.print((char) c); } SocketChannel socketChannel = s.getChannel(); System.out.println(socketChannel.getLocalAddress()); s.close(); }
From source file:eu.stratosphere.nephele.net.NetUtils.java
/** * Returns InputStream for the socket. If the socket has an associated * SocketChannel then it returns a {@link SocketInputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getInputStream()} is returned. In the later * case, the timeout argument is ignored and the timeout set with {@link Socket#setSoTimeout(int)} applies for * reads.<br>/*from w w w.ja v a2 s .com*/ * <br> * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getInputStream()}. * * @see Socket#getChannel() * @param socket * @param timeout * timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return InputStream for reading from the socket. * @throws IOException */ public static InputStream getInputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getInputStream() : new SocketInputStream(socket, timeout); }
From source file:eu.stratosphere.nephele.net.NetUtils.java
/** * 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>//from w w w .j a va 2 s . c o m * <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); }
From source file:eu.stratosphere.nephele.net.NetUtils.java
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()"); }// www . j av a 2 s.c o m SocketChannel ch = socket.getChannel(); if (ch == null) { // let the default implementation handle it. socket.connect(endpoint, timeout); } else { SocketIOWithTimeout.connect(ch, endpoint, timeout); } }
From source file:Main.java
/** * Renders the details of a socket in the returned string * @param socket The socket to render//from w w w .ja v a 2 s . co m * @return the details of the socket as a string */ public static String render(Socket socket) { if (socket == null) return "NULL"; StringBuilder b = new StringBuilder("\nSocket ["); b.append("\n\tLocalPort:").append(socket.getLocalPort()); b.append("\n\tLocalAddress:").append(socket.getLocalAddress()); b.append("\n\tLocalSocketAddress:").append(socket.getLocalSocketAddress()); b.append("\n\tRemotePort:").append(socket.getPort()); b.append("\n\tRemoteAddress:").append(socket.getInetAddress()); b.append("\n\tRemoteSocketAddress:").append(socket.getRemoteSocketAddress()); b.append("\n\tChannel:").append(socket.getChannel()); b.append("\n\tHashCode:").append(socket.hashCode()); b.append("\n]"); return b.toString(); }
From source file:net.NetUtils.java
/** * This is a drop-in replacement for /*w w w . j a va 2 s .c o m*/ * {@link Socket#connect(SocketAddress, int)}. * In the case of normal sockets that don't have associated channels, this * just invokes <code>socket.connect(endpoint, timeout)</code>. If * <code>socket.getChannel()</code> returns a non-null channel, * connect is implemented using Hadoop's selectors. This is done mainly * to avoid Sun's connect implementation from creating thread-local * selectors, since Hadoop does not have control on when these are closed * and could end up taking all the available file descriptors. * * @see java.net.Socket#connect(java.net.SocketAddress, int) * * @param socket * @param endpoint * @param timeout - timeout in milliseconds */ 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); } // 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."); } }
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Like {@link NetUtils#connect(Socket, SocketAddress, int)} but also takes a local address and port to bind the * socket to./*from w w w . j a va 2s . c om*/ * * @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."); } }
From source file:com.sun.grizzly.http.jk.common.ChannelNioSocket.java
public void close(MsgContext ep) throws IOException { Socket s = (Socket) ep.getNote(socketNote); SelectionKey key = s.getChannel().keyFor(selector); if (key != null) { key.cancel();/* w w w . jav a 2 s .c o m*/ } s.close(); }
From source file:org.apache.flink.runtime.net.NetUtils.java
/** * Returns InputStream for the socket. If the socket has an associated * SocketChannel then it returns a {@link SocketInputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getInputStream()} is returned. In the later * case, the timeout argument is ignored and the timeout set with {@link Socket#setSoTimeout(int)} applies for * reads.<br>//from ww w.java 2 s. c o m * <br> * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getInputStream()}. * * @see Socket#getChannel() * @param socket * @param timeout * timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return InputStream for reading from the socket. * @throws IOException */ @SuppressWarnings("resource") public static InputStream getInputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getInputStream() : new SocketInputStream(socket, timeout); }
From source file:org.apache.flink.runtime.net.NetUtils.java
/** * 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>//from ww w. j a va2s. co m * <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 */ @SuppressWarnings("resource") public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }