Search code examples
c++socketsudpudp-data-transfer

bind existing socket with UDT


i'm using the UDT library to transfer files beetween to computers behind NAT. I already did a hole punching protocol in python and i know how it works. Here, i'm trying to bind an UDT socket to an existing UDP socket.

int udt_holepunching(SOCKET sock, SOCKADDR_IN sin, int size, SOCKADDR_IN peeraddr)
    {
      UDTSOCKET u;
      bool rdv = true;



      UDT::setsockopt(u, 0, UDT_RENDEZVOUS, &rdv, sizeof(bool));
      UDT::bind(u, sock);
      UDT::connect(u, &peeraddr, sizeof(peeraddr));
    }

with sock my existing UDP socket and peeraddr the adress of the computer that i want to talk

i have this error :

client.cpp: In function ‘int udt_holepunching(SOCKET, SOCKADDR_IN, int, SOCKADDR_IN)’:
client.cpp:29:20: error: invalid conversion from ‘SOCKET {aka int}’ to ‘const sockaddr*’ [-fpermissive]
   UDT::bind(u, sock);
                    ^
client.cpp:29:20: error: too few arguments to function ‘int UDT::bind(UDTSOCKET, const sockaddr*, int)’
In file included from client.cpp:13:0:
../src/udt.h:315:13: note: declared here
 UDT_API int bind(UDTSOCKET u, const struct sockaddr* name, int namelen);

It's kind of strange because in the UDT bind documentation, it seems to be possible.


Solution

  • The linked to reference shows two overloads. The compiler picks the first one (the tree argument overload) because you pass an UDTSOCKET as the first argument and have a second argument.

    Since the the second argument in the three-argument overload is not a SOCKET you get the first error. And since you're only passing two arguments to a function expecting three you get the second error.

    If you want to bind to a specific address (say the sin argument you pass to your function) then you need to pass a pointer to that address structure as the second argument, and the size of that structure as the third argument:

    UDT::bind(u, (const SOCKADDR*) &sin, sizeof(sin));
    

    If you want to use the second overload, then use it:

    UDT::bind(sock);
    

    You can't mix and match as you like, you have to pick one or the other.