当前位置: 首页>>代码示例>>Java>>正文


Java Socket.getChannel方法代码示例

本文整理汇总了Java中java.net.Socket.getChannel方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.getChannel方法的具体用法?Java Socket.getChannel怎么用?Java Socket.getChannel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.Socket的用法示例。


在下文中一共展示了Socket.getChannel方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: MMOConnection

import java.net.Socket; //导入方法依赖的package包/类
public MMOConnection(SelectorThread<T> selectorThread, Socket socket, SelectionKey key, boolean tcpNoDelay)
{
	_selectorThread = selectorThread;
	_socket = socket;
	_address = socket.getInetAddress();
	_readableByteChannel = socket.getChannel();
	_writableByteChannel = socket.getChannel();
	_port = socket.getPort();
	_selectionKey = key;
	
	_sendQueue = new NioNetStackList<>();
	
	try
	{
		_socket.setTcpNoDelay(tcpNoDelay);
	}
	catch (SocketException e)
	{
		e.printStackTrace();
	}
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:22,代码来源:MMOConnection.java

示例2: allocateCommBuffer

import java.net.Socket; //导入方法依赖的package包/类
public static ByteBuffer allocateCommBuffer(int size, Socket sock) {
  // I expect that size will almost always be the same value
  if (sock.getChannel() == null) {
    // The socket this commBuffer will be used for is old IO (it has no channel).
    // So the commBuffer should be heap based.
    return ByteBuffer.allocate(size);
  }
  LinkedBlockingQueue<ByteBuffer> q = commBufferMap.get(size);
  ByteBuffer result = null;
  if (q != null) {
    result = q.poll();
  }
  if (result == null) {
    result = ByteBuffer.allocateDirect(size);
  } else {
    result.position(0);
    result.limit(result.capacity());
  }
  return result;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:21,代码来源:ServerConnection.java

示例3: SocketOrChannelConnectionImpl

import java.net.Socket; //导入方法依赖的package包/类
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()));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:SocketOrChannelConnectionImpl.java

示例4: SocketInputWrapper

import java.net.Socket; //导入方法依赖的package包/类
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);
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:11,代码来源:SocketInputWrapper.java

示例5: SocketOrChannelConnectionImpl

import java.net.Socket; //导入方法依赖的package包/类
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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:29,代码来源:SocketOrChannelConnectionImpl.java

示例6: peerFromSocket

import java.net.Socket; //导入方法依赖的package包/类
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();
    }
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:35,代码来源:TcpPeerServer.java

示例7: setComms

import java.net.Socket; //导入方法依赖的package包/类
@Override
public void setComms(Socket socket, InputStream is, OutputStream os, ByteBuffer bb,
    MessageStats msgStats) throws IOException {
  Assert.assertTrue(socket != null);
  this.socket = socket;
  this.sockCh = socket.getChannel();
  this.is = is;
  this.os = new BufferedOutputStream(os);
  this.cachedCommBuffer = bb;
  this.msgStats = msgStats;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:12,代码来源:ChunkedStreamMessage.java

示例8: createConnection

import java.net.Socket; //导入方法依赖的package包/类
/**
     * 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;
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:60,代码来源:ShellAgent.java

示例9: connect

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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.");
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:55,代码来源:NetUtils.java

示例10: getInputStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * Return a {@link SocketInputWrapper} for the socket and set the given
 * timeout. If the socket does not have an associated channel, then its socket
 * timeout will be set to the specified value. Otherwise, a
 * {@link SocketInputStream} will be created which reads with the configured
 * timeout.
 * 
 * Any socket created using socket factories returned by {@link #NetUtils},
 * must use this interface instead of {@link Socket#getInputStream()}.
 * 
 * In general, this should be called only once on each socket: see the note
 * in {@link SocketInputWrapper#setTimeout(long)} for more information.
 *
 * @see Socket#getChannel()
 * 
 * @param socket
 * @param timeout timeout in milliseconds. zero for waiting as
 *                long as necessary.
 * @return SocketInputWrapper for reading from the socket.
 * @throws IOException
 */
public static SocketInputWrapper getInputStream(Socket socket, long timeout) 
                                         throws IOException {
  InputStream stm = (socket.getChannel() == null) ? 
        socket.getInputStream() : new SocketInputStream(socket);
  SocketInputWrapper w = new SocketInputWrapper(socket, stm);
  w.setTimeout(timeout);
  return w;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:30,代码来源:NetUtils.java

示例11: SocketOutputStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:18,代码来源:SocketOutputStream.java

示例12: SocketInputStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:18,代码来源:SocketInputStream.java

示例13: SocketInputStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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());
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:16,代码来源:SocketInputStream.java

示例14: getOutputStream

import java.net.Socket; //导入方法依赖的package包/类
/**
 * 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);            
}
 
开发者ID:naver,项目名称:hadoop,代码行数:25,代码来源:NetUtils.java


注:本文中的java.net.Socket.getChannel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。