Reputation: 633
I am trying to open up a TCP socket in my client application so that it can talk to my server.
after going through the documentation, i am curious about the socket constructor, which takes two parameters.
Socket(InetAddress dstAddress, int dstPort)
Creates a new streaming socket connected to the target host specified by the parameters dstAddress and dstPort.
and its description is as above. So as I learned, after creating the socket, I should have explicitly called the connect function in order for it to connect to the server. But in some TCP client sample codes I found online, none of them actually calls the connect function
connect(SocketAddress remoteAddr, int timeout)
So I am thinking if the constructor automatically connects to the server after being created? the three-ways handshake is done. or I have to explicitly call the connect function after the constructor? Thank you so much
Upvotes: 0
Views: 1392
Reputation: 20961
Only the two constructors that don't take any kind of target do not connect:
Socket()
Creates a new unconnected socket.
Socket(Proxy)
Creates a new unconnected socket using the given proxy type.
All the other constructors where you pass the target as hostname or address do connect:
Socket(String, int)
Creates a new streaming socket connected to the target host specified by the parameters dstName and dstPort.
Socket(String, int, InetAddress, int)
Creates a new streaming socket connected to the target host specified by the parameters dstName and dstPort.
Socket(InetAddress, int)
Creates a new streaming socket connected to the target host specified by the parameters dstAddress and dstPort.
Socket(InetAddress, int, InetAddress, int)
Creates a new streaming socket connected to the target host specified by the parameters dstAddress and dstPort.
I left out the two deprecated constructors.
This is straight from the Android API documentation for java.lang.Socket
.
Upvotes: 1
Reputation: 28063
Socket constructors work according with the documentation, some make also connection, other not. Please see: http://download.oracle.com/javase/6/docs/api/java/net/Socket.html
Handshake is automatically done, you don't need to take care of it.
Upvotes: 0