简体   繁体   中英

Java - cannot connect to server - connection refused

I am using the java.nio package to create a TCP client-server connection between two android devices. Here is what the client does:

SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress(gameAddr, 8001));

Here is what the server does:

try 
        {
            tokenizer = new FixedSeparatorMessageTokenizer(Strings.DELIMITER, Charset.forName("UTF-8"));
            selector = SelectorProvider.provider().openSelector();
            sChan = ServerSocketChannel.open();
            InetSocketAddress iaddr = new InetSocketAddress(InetAddress.getLocalHost(), 8001);

            sChan.configureBlocking(false);
            sChan.socket().bind(iaddr);

            sChan.register(selector, SelectionKey.OP_ACCEPT);
            sockets = new Vector<SocketChannel>();

        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        Iterator<SelectionKey> it;

        try {
            while (selector.isOpen()) 
            {

                selector.select();

                it = selector.selectedKeys().iterator();
                while (it.hasNext()) 
                {
                    SelectionKey key = it.next();

                    it.remove();
                    if (!key.isValid()) 
                    {
                        continue;
                    }

                    if (key.isConnectable()) 
                    {
                        SocketChannel ssc = (SocketChannel) key.channel();
                        if (ssc.isConnectionPending()) ssc.finishConnect();
                    }

                    if (key.isAcceptable()) 
                    {
                        ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                        SocketChannel newClient = ssc.accept();

                        newClient.configureBlocking(false);
                        newClient.register(selector, SelectionKey.OP_READ);
                        sockets.add(newClient);

                        System.out.println("Connection from " + newClient.socket().getInetAddress().getHostAddress());
                    }

                    if (key.isReadable()) {
                        SocketChannel sc = (SocketChannel) key.channel();
                        ByteBuffer data = ByteBuffer.allocate(sc.socket().getSendBufferSize());

                        System.out.println("Data from " + sc.socket().getInetAddress().getHostAddress());

                        if (sc.read(data) == -1) //is this a socket close?
                        {

                            continue;
                        }

                        data.flip();
                        tokenizer.addBytes(data);
                        while(tokenizer.hasMessage())
                        {
                            ParsedMessage pm = new ParsedMessage(tokenizer.nextMessage());
                            pm.setAttachment(sc);
                            synchronized(Q) { Q.add(pm); }
                        }

                    }
                }
            }
        }
        catch (Exception e) 
        {
           e.printStackTrace();
        }
    }

They both are using the same port 8001 (I tried different ports) and they are in the same LAN because UDP packets actually make it from one device to the other. What can be the problem?

"Connection refused" means that the connection attempt from the client reached the server (or a server), but there was nothing accepting connections at the specified IP address and port. Since you are using port 8001 in both cases, the simplest explanation is that either your server or your client aren't using the correct IP address to communicate with the other end.

You're using this line to create the server socket:

InetSocketAddress iaddr = new InetSocketAddress(InetAddress.getLocalHost(), 8001);

the simplest explanation is that InetAddress.getLocalHost() isn't returning the same IP address that the client is using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM