| Author |
What is the difference in these two methods : Socket.bind() , Socket.connect()
|
Esmaeil Ashrafi
Ranch Hand
Joined: Feb 22, 2010
Posts: 73
|
|
Actually i cant find out what does the method bind() exactly do ?
I read the documentation, and until yet whenever i wrote a client/server application, i simply did below steps :
for example in a client.
And i know if use the constructor : Socket(); (that is without the destination address specifications), i can use the method connect(endPoint), but what does the bind() method do, or which situation it applies ?
I also read "The Java Tutorials" in section "what is a network interface" and at the point :
encountered with the question "Waht does the bind() method do here ?"
Thanks in advance
|
I'm really tired of being engaged with stuff other than Java and programming
Wish to get back soon to my love...
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
Each socket has two end points - one on the local machine and one on the remote machine. connect() creates the connection with the remote machine, bind() creates the connection with the local machine. If you omit the call to bind() the socket will get an unspecified local end point.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Esmaeil Ashrafi
Ranch Hand
Joined: Feb 22, 2010
Posts: 73
|
|
Rob Prime wrote:Each socket has two end points - one on the local machine and one on the remote machine. connect() creates the connection with the remote machine, bind() creates the connection with the local machine. If you omit the call to bind() the socket will get an unspecified local end point.
Very good
And :
if i omit to bind() my socket to a "local address,local port",does then the InetSocketaddress specified by local underlying system, determinable by the remote endPoint ?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
It will use one of the local network cards (probably the first one) and a port that is determined by the operating system (possibly random, I have no idea). The local address is in no way related to the remote address - other than the fact that they share a socket connection between them.
|
 |
Esmaeil Ashrafi
Ranch Hand
Joined: Feb 22, 2010
Posts: 73
|
|
Rob Prime wrote:It will use one of the local network cards (probably the first one) and a port that is determined by the operating system (possibly random, I have no idea). The local address is in no way related to the remote address - other than the fact that they share a socket connection between them.
Thank you
But right away, i read the Socket documentations and there are two method there :
Socket.getInetAddress()
and
Socket.getPort()
so we can have these lines of code where implementing a ServerSocket :
Do you confirm ?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
I would say those methods are what you need to get the remote address and port, yes. And to really confirm it you could test it of course.
|
 |
 |
|
|
subject: What is the difference in these two methods : Socket.bind() , Socket.connect()
|
|
|