简体   繁体   中英

Socket.connect(InetSocketAddress, timeout) doesn't work, while new Socket(InetAddress, port) does

I've stumbled upon something very peculiar. When I'm trying to connect to a LAN server in my peer to peer application, it looks like socket.connect doesn't work, while the new Socket does. The .netAddress provided are link-local IPV6s, however that should(?) not be a factor in this bug. Here's the code that works:

    @Override
    public void createUserSocket() throws IOException, InvalidPortValueException {
        if (!portIsValid()) throw new InvalidPortValueException();
        shutdown();
        Log.d(TAG + ".createUserSocket", "TRYING TO CONNECT TO " + address.getHostAddress() + " : " + port);
        currentUserSocket = new SocketAdapter(address, port);
        currentUserSocket.setTimeout(SO_TIMEOUT);
        Log.d(TAG + ".createUserSocket", "connected to " + currentUserSocket.log());
    }

Here's the code that doesn't:

    @Override
    public void createUserSocket() throws IOException, InvalidPortValueException {
        if (!portIsValid()) throw new InvalidPortValueException();
        shutdown();
        currentUserSocket = new SocketAdapter();
        InetSocketAddress soAddr = new InetSocketAddress(address, port); 
        Log.e(TAG + ".createUserSocket", "TRYING TO CONNECT TO " + soAddr.getAddress().getHostAddress() + " " + soAddr.getPort() + " with timeout " + SO_TIMEOUT);
        currentUserSocket.connect(soAddr, SO_TIMEOUT);
        Log.d(TAG + ".createUserSocket", "connected to " + currentUserSocket.log());
    }

It's worthwhile to note SocketAdapter is just a wrapper class for the Socket class, and doesn't do any funny stuff. The relevant bits of code for SocketAdapter are below.

SO_TIMEOUT is a variable specifying timeout in millisecond time, and address & port remain the same from the two blocks:

private static final int SO_TIMEOUT = 2000;
private final InetAddress address;
private final int port;

The first example works as expected, connects and everything's OK. The second example doesn't work, and instead blocks forever.

I get the address & port to connect to externally; however, as I said before, the first example works, while the second example doesn't, blocking forever in the .connect method. I'd appreciate any insight for this sort of behaviour, since this is a critical part for the efficiency of my application. Thank you in advance!

Note, that the two devices I'm testing with, are API 26 and 30 respectively. Both are A-Series SAMSUNG phones (from different generations).

SocketAdapter

    private Socket socket;

    public SocketAdapter() {}

    public SocketAdapter(Socket s) {
        socket = s;
    }

    public SocketAdapter(InetAddress address, int port) throws IOException {
        socket = StaticSocketFactory.createSocket(address, port);
    }

    public InetAddress getInetAddress() {
        return socket.getInetAddress();
    }

    public void connect(SocketAddress addr, int soTimeout) throws IOException {
        socket.connect(addr, soTimeout);
    }

StaticSocketFactory

    static Socket createSocket(InetAddress addr, int port) throws IOException {
        return new Socket(addr, port);
    }

I don't understand how you try to create socket but if you want use socket you have to do it this way:

  1. Defind a serversocket

    ServerSocket serverSocket = new ServerSocket(8080);

  2. Then you can wait for a client to connect

Socket socket = serverSocket.accept();

  1. If your client send data then make a function that read http request with BufferedReader. If you want send data to the client I invite you to use HttpURLConnection ( https://docs.oracle.com/javase/8/docs/api/java.net/HttpURLConnection.html )

I invite you to create a thread for each client that connect to your server. And don't forget to close your socket.

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