TCP simultaneous open
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: TCP simultaneous open

  1. #1
    Join Date
    Nov 2008
    Location
    india
    Posts
    53

    TCP simultaneous open

    I am TCP simultaneous open.I have two client application. Each client opens two socket
    1. Listening Socket used in listen ()
    2. Connection Socket used in connect()

    I tried binding these socket at same address using setsockopt but wg it shows error address in use 10048 when i call connect().

    I wanted to know whether it is possible to bind a listening socket(listen()) with connection socket

    note:i am implementing TCP hole punching where it is mandatory to bind the local address with both socket.

    Regards

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,300

    Re: TCP simultaneous open

    Error 10048 is WSAEADDRINUSE:
    Only one usage of each socket address (protocol/network address/port) is normally permitted.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2008
    Location
    india
    Posts
    53

    Re: TCP simultaneous open

    Thanks for replay.I am trying to bind both socket with same ip and port using setsocktopt. but this not working.
    Tell me am i doing wrong here.how can aceive this

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,300

    Re: TCP simultaneous open

    Quote Originally Posted by lok.vikram View Post
    I am trying to bind both socket with same ip and port using setsocktopt. but this not working.
    Well, it is not working according to design. And the description of the "Error 10048" clearly say about it.
    Why are you trying to set the port/address for connecting socket?
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2008
    Location
    india
    Posts
    53

    Re: TCP simultaneous open

    I am implementing TCP hole punching for that i need to bind two or more socket to same address.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,300

    Re: TCP simultaneous open

    Sorry, I have no idea what "TCP hole punching" is.
    Victor Nijegorodov

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,108

    Re: TCP simultaneous open

    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.

  8. #8
    Join Date
    Nov 2008
    Location
    india
    Posts
    53

    Re: TCP simultaneous open

    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.
    1.Listening socket
    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

  9. #9
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,108

    Re: TCP simultaneous open

    OK, I have read up on hole punching here: http://www.brynosaurus.com/pub/net/p2pnat/

    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.

    HTH,
    Richard

  10. #10
    Join Date
    Nov 2008
    Location
    india
    Posts
    53

    Smile Re: TCP simultaneous open

    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.
    thanks
    Last edited by lok.vikram; December 28th, 2010 at 04:55 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center