SocketChannel socket - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for SocketChannel socket method.

This code example shows how to use the following methods:socket, configureBlocking
    public SocketConnection(SocketChannel socket, MessageSerializer<T> serializer) {
        this.socket = socket;
        try { 
            // NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while 
            // keeping writes active. For example, Socket.shutdownInput() does not work on Windows. 
            socket.configureBlocking(false);
            outstr = new SocketOutputStream(socket);
            instr = new SocketInputStream(socket);
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        } 
        InetSocketAddress localSocketAddress = (InetSocketAddress) socket.socket().getLocalSocketAddress();
        localAddress = new SocketInetAddress(localSocketAddress.getAddress(), localSocketAddress.getPort());
        InetSocketAddress remoteSocketAddress = (InetSocketAddress) socket.socket().getRemoteSocketAddress();
        remoteAddress = new SocketInetAddress(remoteSocketAddress.getAddress(), remoteSocketAddress.getPort());
        objectReader = serializer.newReader(instr, localAddress, remoteAddress);
        objectWriter = serializer.newWriter(outstr);
    } 
 
    @Override 
8
CodeRank
This code example shows how to use the following methods:socket, validOps, isBlocking, isRegistered, provider
This code example shows how to use the following methods:connect, socket, configureBlocking, AbstractSelectableChannel.register
        return false; 
    } 
 
    private void configureSocket() throws IOException {
        if (socketChannel!=null) {
            socketChannel.configureBlocking(false);
            socketChannel.socket().setSendBufferSize(getTxBufSize());
            socketChannel.socket().setReceiveBufferSize(getRxBufSize());
            socketChannel.socket().setSoTimeout((int)getTimeout());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerOn()?getSoLingerTime():0);
            socketChannel.socket().setTcpNoDelay(getTcpNoDelay());
            socketChannel.socket().setKeepAlive(getSoKeepAlive());
            socketChannel.socket().setReuseAddress(getSoReuseAddress());
            socketChannel.socket().setOOBInline(getOoBInline());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
            socketChannel.socket().setTrafficClass(getSoTrafficClass());
        } else if (dataChannel!=null) {
            dataChannel.configureBlocking(false);
            dataChannel.socket().setSendBufferSize(getUdpTxBufSize());
            dataChannel.socket().setReceiveBufferSize(getUdpRxBufSize());

6
CodeRank
This code example shows how to use the following methods:toString, socket, isOpen
	} 
	 
	@Override 
	public String toString() {
		 
		return "InetClientSocket[" + ch.toString()  
			+ " open="+ch.isOpen()
			+ " addr="+ch.socket().getLocalSocketAddress()
			+ " remote="+ch.socket().getRemoteSocketAddress()
		    + "]";		 
	} 
} 
7
CodeRank
This code example shows how to use the following methods:socket, close, configureBlocking
	protected void doAccept(final Selector selector, ServerSocketChannel server, long now) throws IOException {
		logger.debug("New accept"); 
		SocketChannel channel = server.accept();
		if (this.isShuttingDown()) {
			if (logger.isInfoEnabled()) { 
				logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress()
						+ " rejected; the server is in the process of shutting down."); 
			} 
			channel.close();
		} 
		else { 
			try { 
				channel.configureBlocking(false);
				Socket socket = channel.socket();
				setSocketAttributes(socket);
				TcpNioConnection connection = createTcpNioConnection(channel);
				if (connection == null) {
					return; 
				} 
				connection.setTaskExecutor(this.getTaskExecutor());

6
CodeRank
This code example shows how to use the following methods:isConnected, socket, configureBlocking
   * @param socketChannel Already created SocketChannel object 
   * @throws IOException if there is an error setting up the streams 
   */ 
  public TNonblockingSocket(SocketChannel socketChannel) throws IOException {
    this(socketChannel, 0, null);
    if (!socketChannel.isConnected()) throw new IOException("Socket must already be connected");
  } 
 
  private TNonblockingSocket(SocketChannel socketChannel, int timeout, SocketAddress socketAddress)
      throws IOException {
    socketChannel_ = socketChannel;
    socketAddress_ = socketAddress;
 
    // make it a nonblocking channel 
    socketChannel.configureBlocking(false);
 
    // set options 
    Socket socket = socketChannel.socket();
    socket.setSoLinger(false, 0);
    socket.setTcpNoDelay(true);

6
CodeRank
This code example shows how to use the following methods:socket
        this.socket = socket;
    } 
 
    @Override 
    public String toString() {
        return String.format("%s to %s", socket.socket().getLocalSocketAddress(), socket.socket().getRemoteSocketAddress());
    } 
 
    public <T> RemoteConnection<T> create(ClassLoader messageClassLoader) {
        return new SocketConnection<T>(socket, new DefaultMessageSerializer<T>(messageClassLoader));
    } 
 
    public <T> RemoteConnection<T> create(MessageSerializer<T> serializer) {
        return new SocketConnection<T>(socket, serializer);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:socket
        return socketChannel.isBlocking();
    } 
 
    @Override 
    public Socket socket() {
        return socketChannel.socket();
    } 
 
    @Override 
    public boolean isConnected() { 
        return socketChannel.isConnected();
    } 
 
    @Override 
    public boolean connect(SocketAddress socketAddress) throws IOException {
        return socketChannel.connect(socketAddress);
    } 
 
    @Override 
    public int read(ByteBuffer byteBuffer) throws IOException {
6
CodeRank
This code example shows how to use the following methods:socket, close
			throws IOException, SocketException {
		if (sk != null) {
			sk.cancel();
		} 
		if (sc != null) {
			sc.socket().setSoLinger(true, 0); // await TIME_WAIT status
			sc.socket().shutdownOutput();
			sc.close();
		} 
	} 
 
	public void closeChannel(Selector selector) throws IOException {
		if (this.serverSocketChannel != null) {
			this.serverSocketChannel.close();
		} 
	} 
 
	public void unbind() throws IOException {
		stop(); 
	} 
5
CodeRank
This code example shows how to use the following methods:socket, configureBlocking

Related SocketChannel socket Questions & Answers:

socket Method Overview
Retrieves a socket associated with this channel.
See Code Examples for other Java 8 SocketChannel Methods: