java.net.ConnectException:连接超时:没有更多信息

java.net.ConnectException: Connection timed out: no further information

今天我在不同的机器上测试服务器和客户端代码。 两者都在同一个 Wi-fi 网络上。

我使用以下代码创建了客户端,并在许多线程中遇到了这个异常:

java.net.ConnectException: Connection timed out: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source) at SocketTest.connect(Client.java:188) at SocketTest.run(Client.java:73) at java.lang.Thread.run(Unknown Source)

第 73 行是 connect(key) 第 188 行是 if(!(channel.finishConnect()))

所以客户端线程无法连接是因为没有来自服务器的回复?对吗?

问题)当我 运行 服务器和客户端都在同一台机器本地主机上时,不会出现此异常。可能是什么原因? (网络问题?)。

此外,我还在 public void bind(SocketAddress endpoint,int backlog) 中使用 Backlog 队列参数作为 2000。虽然确切的大小未知(大约 200?),但我使用了一个很大的值,因此最大值值将被使用。(对吗?或 Java 将排队?)。

这可能是一个原因吗:服务器将请求放入积压队列,直到它有时间服务它,超时可能发生在客户端?

客户:

public class Client {

public static void main(String[] args) {

    int n=100;
    SocketTest [] st= new SocketTest[n];
    for(int i=0;i<n;i++)
        st[i]= new SocketTest("hi");


    for(int i=0;i<n;i++)
    {
        if(i%50 == 0)
        try{
            Thread.sleep(5000);
        }
        catch(InterruptedException ie)
        {
            System.out.println(""+ie);
        }
        new Thread(st[i]).start();

    }
}
}
class SocketTest implements Runnable {

    private String message = "";
    ByteBuffer readBuffer = ByteBuffer.allocate(1000);
    private Selector selector;
    private int i;
    public static AtomicInteger cnt= new AtomicInteger(0);

    public SocketTest(String message){
        this.message = message;
    }

    @Override
    public void run() {
        SocketChannel channel;
        try {
            selector = Selector.open();
            channel = SocketChannel.open();
            channel.configureBlocking(false);

            channel.register(selector, SelectionKey.OP_CONNECT);

            channel.connect(new InetSocketAddress("192.168.1.10", 8511));

            while (!Thread.currentThread().isInterrupted()){

                selector.select();

                Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

                while (keys.hasNext()){
                    SelectionKey key = keys.next();
                    keys.remove();

                    if (!key.isValid()) continue;

                    if (key.isConnectable()){  
                            connect(key);
                        System.out.println("I am connected to the server");
                    }   
                    if (key.isWritable()){
                        write(key);
                    }
                    if (key.isReadable()){
                        read(key);
                    }
                }   
            }
        }
        catch(ClosedByInterruptException e)
        {
            // let go of thread
        }
        catch(CancelledKeyException e){

            e.printStackTrace();
        }
        catch (IOException e1) {
            System.out.println("IOE Occured|maybe Server died");
            e1.printStackTrace();
        } finally {
            close();
        }
    }

    private void close(){

        try {
                if(selector!=null)
                    selector.close();
            }
            catch(Exception e) 
            {
                    e.printStackTrace();
            }

    }

    private void read (SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();

        readBuffer.clear();
        int length;
        try{
        length = channel.read(readBuffer);

        } catch (IOException e){
            System.out.println("Reading problem, closing connection for  : "+channel.getLocalAddress());
            key.cancel();
            channel.close();
            return;
        }
        if (length == -1){
            System.out.println("Nothing was read from server");
            channel.close();
            key.cancel();
            return;
        }
        readBuffer.flip();
        byte[] buff = new byte[1024];
        readBuffer.get(buff, 0, length);
        //length=buff.length;

        String fromserver = new String(buff,0,length,"UTF-8");
        length = fromserver.length();
        System.out.println("Server said: "+fromserver);

        key.interestOps(SelectionKey.OP_WRITE);
    }

    private void write(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();
        i++;
        message = "location now "+i;
        if(i==2)
        {
            cnt.addAndGet(1);
            System.out.println("****"+cnt.get()+"****");
        }

        try{
            Thread.sleep(5000);
        }
        catch(InterruptedException ie)
        {
            System.out.println(""+ie);
            //Thread.currentThread().interrupt();
        }

        //assuming all goes in one shot
        channel.write(ByteBuffer.wrap(message.getBytes()));

        key.interestOps(SelectionKey.OP_READ/*|SelectionKey.OP_WRITE*/);
    }

    private void connect(SelectionKey key){
        SocketChannel channel= (SocketChannel) key.channel();

        try
        {
            if(!(channel.finishConnect())){
                //System.out.println("* Here *");
                return;
            }
        }
        catch(ConnectException e){
            System.out.println("Conect Exception");
            e.printStackTrace();
            try{channel.close();}
            catch(IOException ie){ie.printStackTrace();key.cancel();return;}
            return;
        }
        catch(IOException e)
        {
            System.out.println("BP 1"+e);
            e.printStackTrace();
            try{channel.close();}
            catch(IOException ie){ie.printStackTrace();key.cancel();return;}
            return;
        }
            //channel.configureBlocking(false);
            //channel.register(selector, SelectionKey.OP_WRITE);
        key.interestOps(SelectionKey.OP_WRITE);

    }
}

连接超时,因为服务器没有回复。

When I run both Server and Client on same machine localhost this exception does not arise. What may be the reasons ? (network problem ?).

为什么应该它出现?服务器在那里,客户端在那里,中间没有网络问题。这个问题没有意义。

Also I also use Backlog queue parameter in public void bind(SocketAddress endpoint,int backlog) as 2000. While exact size is unknown(around 200 ?) but I am using a large value so that maximum value will be used. Right?

没错。

or Java will make a queue?

我不知道这是什么意思。 Java 不对 backlog 参数做任何事情。它直接进入 TCP。

Can this be a reason: The Server puts the request in backlog queue and till it gets time to serve it, the timeout may have happened at Client ?

没有。只有 已完成 个连接进入积压队列。