SocketChannel connect - 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 connect method.

This code example shows how to use the following methods:connect, finishConnect, isConnected, isConnectionPending, close
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
    } 
 
    public void testIsConnected() throws Exception {
        assertFalse(this.channel1.isConnected());// not connected
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.connect(localAddr1));
        assertFalse(this.channel1.isConnected());
        assertTrue(this.channel1.isConnectionPending());
        assertTrue(tryFinish());
        assertTrue(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isConnected());
    } 
 
    public void testIsConnectionPending() throws Exception {
        // ensure 
        ensureServerClosed(); 
        this.channel1.configureBlocking(false);

7
CodeRank
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:connect
        return socketChannel.isConnected();
    } 
 
    @Override 
    public boolean connect(SocketAddress socketAddress) throws IOException {
        return socketChannel.connect(socketAddress);
    } 
 
    @Override 
    public int read(ByteBuffer byteBuffer) throws IOException {
        return socketChannel.read(byteBuffer);
    } 
 
    @Override 
    public int write(ByteBuffer byteBuffer) throws IOException {
        return socketChannel.write(byteBuffer);
    } 
 
    @Override 
    public SelectableChannel configureBlocking(boolean b) throws IOException {
6
CodeRank
This code example shows how to use the following methods:register, connect, finishConnect, isConnected
    @Override 
    public boolean connect(SocketAddress remote) throws IOException {
 
        long start = System.currentTimeMillis();
        //log.debug("trying to connect"); 
        socketChannel.connect(remote);
        while (selector.select(connectTimeout) > 0) {
            selector.selectedKeys().remove(channelKey);
            //log.debug("selected connect"); 
            //log.debug("Spent " + (System.currentTimeMillis() - start)); 
            if (!channelKey.isConnectable()) {
                throw new IllegalStateException("Socket channel is in not connectable state");
            } 
 
            socketChannel.finishConnect();
            channelKey = socketChannel.register(selector, SelectionKey.OP_READ);
            if (log.isDebugEnabled()) { 
                log.debug("Connected socket in " + (System.currentTimeMillis() - start));
            } 
            if (!socketChannel.isConnected()) {

6
CodeRank
This code example shows how to use the following methods:connect
		throw new erjang.NotImplemented(); 
	} 
 
	@Override 
	public boolean connect(InetSocketAddress remote) throws IOException {
		return ch.connect(remote);
	} 
 
	@Override 
	public boolean finishConnect() throws IOException {
		return ch.finishConnect();
	} 
 
	@Override 
	public InetSocketAddress getLocalSocketAddress() {
		return (InetSocketAddress) ch.socket().getLocalSocketAddress();
	} 
 
	@Override 
	public void listen(int backlog) throws IOException {
6
CodeRank
This code example shows how to use the following methods:connect
    return channel.write(buffer);
  } 
 
  @Override 
  public boolean connect(SocketAddress remote) throws IOException {
    return channel.connect(remote);
  } 
 
  @Override 
  public SelectionKey register(Selector sel, int ops) throws ClosedChannelException {
    return channel.register(sel, ops);
  } 
 
  @Override 
  public void doHandshake() { 
    // we don't need to do any handshaking 
  } 
 
  @Override 
  public int outBufferRemaining() { 
5
CodeRank
This code example shows how to use the following methods:connect
    } 
  } 
 
  /** {@inheritDoc} */ 
  public boolean startConnect() throws IOException {
    return socketChannel_.connect(socketAddress_);
  } 
 
  /** {@inheritDoc} */ 
  public boolean finishConnect() throws IOException {
    return socketChannel_.finishConnect();
  } 
 
} 
5
CodeRank
This code example shows how to use the following methods:register, connect, finishConnect, isConnected
    @Override 
    public boolean connect(SocketAddress remote) throws IOException {
 
        long start = System.currentTimeMillis();
        //log.debug("trying to connect"); 
        socketChannel.connect(remote);
        while (selector.select(connectTimeout) > 0) {
            selector.selectedKeys().remove(channelKey);
            //log.debug("selected connect"); 
            //log.debug("Spent " + (System.currentTimeMillis() - start)); 
            if (!channelKey.isConnectable()) {
                throw new IllegalStateException("Socket channel is in not connectable state");
            } 
 
            socketChannel.finishConnect();
            channelKey = socketChannel.register(selector, SelectionKey.OP_READ);
            if (log.isDebugEnabled()) {
                log.debug("Connected socket in " + (System.currentTimeMillis() - start));
            } 
            if (!socketChannel.isConnected()) {

5
CodeRank
This code example shows how to use the following methods:register, connect, configureBlocking
	public Thread connect() throws IOException {
		this.running = true;
		this.readyState = WEBSOCKET_STATE_CONNECTING;
		// open socket 
		socketChannel = SocketChannel.open();
		socketChannel.configureBlocking(false);
		// set address 
		socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
		// start a thread to make connection 
 
		// More info: 
		// http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82 
		// http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect 
		System.setProperty("java.net.preferIPv4Stack", "true");
		System.setProperty("java.net.preferIPv6Addresses", "false");
 
		selector = Selector.open();
		socketChannel.register(selector, SelectionKey.OP_CONNECT);
		Log.v("websocket", "Starting a new thread to manage data reading/writing");
 

5
CodeRank
This code example shows how to use the following methods:connect, socket
 
        if (port == -1) {
            throw new IOException();
        } 
 
        mSocketChannel.connect(new InetSocketAddress("127.0.0.1", port)); //$NON-NLS-1$
        mSocketChannel.socket().setSoTimeout(40000);
    } 
 
    public BufferedReader getInputStream() throws IOException {
        if (mIn == null) {
            mIn = new BufferedReader(new InputStreamReader(mSocketChannel.socket().getInputStream()));
        } 
        return mIn;
    } 
 
    public BufferedWriter getOutputStream() throws IOException {
        if (mOut == null) {
            mOut =
                    new BufferedWriter(new OutputStreamWriter(mSocketChannel.socket()
5
CodeRank

Related SocketChannel connect Questions & Answers:

connect Method Overview
Connects this channel's socket.
See Code Examples for other Java 8 SocketChannel Methods: