简体   繁体   中英

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.

They are identical.

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

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