This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes What is the difference in these two methods : Socket.bind() , Socket.connect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "What is the difference in these two methods : Socket.bind() , Socket.connect()" Watch "What is the difference in these two methods : Socket.bind() , Socket.connect()" New topic
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: 19654
    
  18

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 - OCEEJBD 6
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: 19654
    
  18

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: 19654
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference in these two methods : Socket.bind() , Socket.connect()
 
Similar Threads
TCP/Socket mechanics of "Accept()" etc.
Socket problem
Problem connecting to "localhost"
java.net Socket port number question
Address already in use