Seth De
Seth De

Reputation: 417

What is the difference between socket.connect(endpoint) and new Socket(ip,port)

When creating client connection to serversocket we can create client sockets in two ways My question is what is the difference between below two approaches,

 InetAddress addr = InetAddress.getByName("localhost");
  SocketAddress sockaddr = new InetSocketAddress(addr, 1111); 
  Socket clientSocket = new Socket();
  clientSocket.connect(sockaddr);

And the other approach

Socket clientSocket=new Socket("localhost",1111);

What are the advantages and disadvantages

Thank you.

Upvotes: 1

Views: 1159

Answers (1)

user207421
user207421

Reputation: 311039

They are identical.

The advantage of new Socket() is that you can then call the other overload of connect(), that takes a timeout parameter.

Upvotes: 1

Related Questions