[Android] How to handle exceptions when the socket connection fails in the app

Asked 2 years ago, Updated 2 years ago, 88 views

Hello, I'm developing a client app.

It is developed so that data transmission can be made through a socket connection from an app to a specific server.

However, even though the data signal strength and data connection status are sometimes said to be good, the Socket connection cannot be connected because ConnectException or SocketTimeoutException errors keep coming out when connecting to Socket.

I don't know how to make an exception at times like this.

For example, if a socket connection fails when connecting to a socket, is there a way to change the Socket's channel (?) to enable the connection?

Please give me an answer.

Thank you.

android socket connectexception sockettimeoutexception

2022-09-22 16:59

1 Answers

private static void connect(String uri) throws IOException, URISyntaxException {
    try {
        socket = new Socket();
        SocketAddress addr = new InetSocketAddress("192.168.17.1", 8080);
        socket.setSoTimeout(connection_timeout);
        socket.connect(socketAddress, connection_timeout);
    } } catch (ConnectException exception) {
        //to do
    } } catch (SocketTimeoutException exception) {
        //to do
    } } catch (Throwable throwable) {
        //to do
    }
}

This is how it's done, right?


2022-09-22 16:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.