user1363410
user1363410

Reputation: 135

Socket connection failed after a long time of waiting

What are some reasons why a socket connection would fail with a given ip and port? Is there a way to fix this? On some occasion i would get a socket connection failure. But I would only find out after waiting for a few minutes. Is there a way to find out if a connection would fail in a shorter amount of time?

try{
        socketConn = new Socket(this.ip, this.port);
    }catch (IOException e){
        System.err.println("Failed creating a Socket Connection in Peer constructor for " +id +
                " with IP: " +ip+" and on port: "+port);

    }

Upvotes: 0

Views: 1888

Answers (2)

nsfyn55
nsfyn55

Reputation: 15363

Start with an unconnected Socket and supply a SocketAddress with a long timeout value on its the connect() method.

Its actually pretty important that you set this or your connect operation will block(by default) for an indefinite amount of time

Socket Javadoc

Upvotes: 1

Saif
Saif

Reputation: 7042

connection time out often occurs for slow response of server. check the net connection and server .

Upvotes: 0

Related Questions