Java NIO connect to socket

后端 未结 2 821
予麋鹿
予麋鹿 2021-01-07 11:56

I\'m trying to connect to a remote server and send a login message in my Thread:

@Override
public void run() {
    try {
        address = new InetSocketAddr         


        
2条回答
  •  天涯浪人
    2021-01-07 12:17

    i've found an answer.. i should use:

        socketChannel = SocketChannel.open(address);            
        socketChannel.configureBlocking(false);
    
        while (!socketChannel.finishConnect());
    
       //my code after connection
    

    because the NIO is in not blocking mode we have to wait until it finish its connection

提交回复
热议问题