CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  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
    Hanover Germany
    Posts
    20,395

    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
    Hanover Germany
    Posts
    20,395

    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
    Hanover Germany
    Posts
    20,395

    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,158

    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,158

    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 05:55 AM.

  11. #11
    Join Date
    Jun 2016
    Posts
    2

    Re: TCP simultaneous open

    Quote Originally Posted by lok.vikram View Post

    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
    I've been using the same document to figure out TCP Hole Punching, I'm building a C# Console Application that has to use TCP Hole Punching and I'm struggling to figure out the Port binding.

    I've posted 2 questions on stackoverflow about it http://stackoverflow.com/questions/3...not-connecting & http://stackoverflow.com/questions/3...ame-local-port but I still don't understand how to do the socket binding.

    I was wondering if you could explain me what I'm doing wrong or maybe share the code you used to connect the 2 clients.

    I'd really appreciate it, I've been struggling with this for days now, I've never worked with networking stuff before...

    P.S. I don't have any issue with the server, both clients get the other client details successfully.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: TCP simultaneous open

    I'm building a C# Console Application
    You might be better off starting a new thread in the c# forum here http://forums.codeguru.com/forumdisp...rp-Programming as this forum is for c++ networking.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Jun 2016
    Posts
    2

    Re: TCP simultaneous open

    Quote Originally Posted by 2kaud View Post
    You might be better off starting a new thread in the c# forum here http://forums.codeguru.com/forumdisp...rp-Programming as this forum is for c++ networking.
    I'm aware it's a C++ forum, but my question is more about the concept than the actual code, either way if I get a code sample I can rewrite it to C#.

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
  •  





Click Here to Expand Forum to Full Width

Featured