According to wiki, this is related to NAT traversal. I did not read deeper into it, but I don't see the point in binding 2 sockets to the same port (using the same protocol).
And from the OP, things seem to be a little confused: a client would not normally open a socket for listening (in that case, the program would be a server). A client opens a connection to a server using connect() (talking about TCP here), but would not care where the local socket is bound to.
Last thing: using setsockopt (and I assume using the option REUSEADDR) has only one reason: being able to bind to a (server) port after the server is crashed (and would leave the socket in a TIME_WAIT state which makes it impossible for immediate reuse). Any other use of that option leads to undefined behaviour.
I am implementing Peer to peer application where each client behave as client as well server.
I have three socket bind to same address/port.
2.ServerConnectionSocket (for connecting rendezvous server)
3.Peer connection socket (for connecting other peer)
1.each peer listen at istening socket
2.first both peer connects with server using ServerConnectionSocket
3. then each peer gets the IP port of each other from server
3.Each peer has listner thread where I demultiplex all the socket the
4. then they try connect with each other using peer connection socket
5.connect return 0 but listening socket never set in the listnerthraed
It seems that there is the possibility to use REUSEADDR (unlike stated earlier), but as the article says, it must be set on all sockets that want to share the port.
I would try do set this up as follows:
1. establish a connection to the rendezvous-server. The local IP address/port need not be bound first. You can retrieve it (the private endpoint) once the connection is established. Don't forget to set the REUSEADDR option. Receive B's private/public endpoints from the server.
2. Create the listening socket, set REUSEADDR and then bind it to the port you obtained in 1. Setup a thread to use this socket in the call to accept().
3. Create 2 new sockets, set REUSEADDR, bind the sockets to the port from 1., then try to connect to B's private and public endpoint. If the connection is refused, re-try.
Thanks,Richard for reply.I have been following the bryan Ford's article http://www.brynosaurus.com/pub/net/p2pnat/ from past one week.Now I have implemented TCP hole punching successfully.
One need to follow following section carefully
4.3 Behavior Observed by the Application.
Normally Client/Server is based on three way handshake process.
But here its two way handshake process, so successful connection conditions is as follows
1. either connect at both end return 0 and accept wont be called at all.
2.connect at one end succeed and accept at other end succeed.
so in first case I used the same socket to send msg to other peer as used in connect() api and second case I am using the same socket to send msg to other peer as used in connect() at one end and i use the socket returned by accept() to send msg to at other end.
normally second scenario is rare.that means we need to listen for TCp hole punching.
Last edited by lok.vikram; December 28th, 2010 at 04:55 AM.