This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference in these two methods : Socket.bind() , Socket.connect()

 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic