• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic