0

I am trying to create a UDP client on a Windows machine using C++ (see code below).

The problem is the project I am saddled with also links to the <functional> library, which overrides the bind function with a couple of template functions, and VS doesn't know which version of bind() I want to use.

How do I tell the compiler that I want to use the Winsock version of the bind() function?

    SOCKET s = INVALID_SOCKET;

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
    {
        WSACleanup();
        return;
    }

    sockaddr_in addr_recv;

    memset((char *)&addr_recv, 0, sizeof(addr_recv));
    addr_recv.sin_family = AF_INET;
    addr_recv.sin_port = htons(0);
    addr_recv.sin_addr.s_addr = htonl(INADDR_ANY);

    int server_addr_len = sizeof(addr_recv);

    // compiler doesn't know which bind I mean.
    int iResult = bind(s, (SOCKADDR *)&addr_recv, server_addr_len);

    if (iResult == SOCKET_ERROR)
    {
        closesocket(s);
        WSACleanup();
        return;
    }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
cHorse
  • 11
  • 1
  • 8
  • What other `bind` it gets confused with? You should remove the other declaration from compiler sight. – Yksisarvinen Oct 08 '18 at 21:05
  • If all version of `bind` co-exist happily in one translation unit then they must have different call signatures, or be in different namespaces. The one you want may have C linkage (which will also give it a different signature). You may have to try some language linkage magic: https://en.cppreference.com/w/cpp/language/language_linkage – Richard Oct 08 '18 at 21:17
  • @Yksisarvinen there's some sort of library, for lack of a better term, called functional. It appears to be part of VC. It specifies a couple of template functions that use some other form of bind and the compiler can't tell which one I mean. I don't know how else to explain it. – cHorse Oct 08 '18 at 21:23
  • Do you mean `std::bind()` from ``? Well, then you fell victim of `using namespace std;`. See [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yksisarvinen Oct 08 '18 at 21:26
  • 3
    Win32 API functions exist in the global namespace, so use `::bind()` to tell the compiler you want to call `bind()` from the global namespace (ie, Winsock's), assuming there is no `using namespace ...` statement to bring another namespaced `bind()` into the global namespace (in which case, overload resolution takes place to figure out which overload matches the parameters you specify). – Remy Lebeau Oct 08 '18 at 21:31
  • @Yksisarvinen I think thats exactly what's going on. Great.... one namespace is using and it's conflicting with the std namespace version of bind. Or something to that effect. – cHorse Oct 08 '18 at 21:36
  • @Remy Lebeau unfortunately there is another namespace being used and the compiler can't seem to resolve which version I mean even though the two functions don't look to have the same signature to me. – cHorse Oct 08 '18 at 21:38
  • @cHorse That can only happen if there is an explicit `using namespace ...` statement (typically `using namespace std;` by newbies who don't know any better) that is bringing that other namespace's content into the global namespace. Simply having a `#include ` statement would not cause this problem without `using namespace ...` afterwards. So get rid of that `using` statement. Don't dump a namespace's content into the global namespace, that defeats the purpose of using namespaces. – Remy Lebeau Oct 08 '18 at 21:43
  • @Remy Lebeau unfortunately I think I'm stuck. I don't know how familiar you are with an application called Kanzi but they use their own namespace which unfortunately uses `` at some point and there's no practical way to not use their namespace. Fortunately now that I know what's wrong I figured out a hack to make it work. So annoying. – cHorse Oct 08 '18 at 22:08
  • @cHorse "*how familiar [are] you with an application called Kanzi*" - I'm not. "*they use their own namespace which unfortunately uses *" - that is perfectly fine. When used properly, namespaces *prevent* naming conflicts. That is not causing the conflict at hand. Something in your project has a `using std::bind` or `using namespace std` statement that brings `std::bind()` into the global namespace where it doesn't belong. That is what you need to find and fix. "*I figured out a hack to make it work*" - and what is that hack exactly? Sounds like you are not fixing the root cause. – Remy Lebeau Oct 08 '18 at 23:56
  • @Remy Lebeau Goodish news. Apparently whoever wrote the original code I inherited didn't realize you don't need to `bind` on a UDP client. So I removed that code. This of course fixed the problem I was having, but I still didn't get to the bottom of what was going on. I searched for `using namespace std` and `using std::bind` and didn't find anything. Chances are the problem is buried in the kanzi namespace somewhere and I don't have access to that. – cHorse Oct 09 '18 at 22:45
  • @cHorse "*Chances are the problem is buried in the kanzi namespace somewhere and I don't have access to that*" - yes, you do, because it is affecting your code, so it has to be in code that is public, such as in kanzi's header files that your code uses. – Remy Lebeau Oct 09 '18 at 23:01
  • @RemyLebeau apparently all I had to do was add `::bind` and rewrite rest of the code like a real programmer should and the problem went away. EDIT: I just now realized you suggested that 8 comments ago. – cHorse Oct 17 '18 at 16:43
  • ::bind solved my problem too. Thanks. This should be the answere. – jovan Jul 15 '22 at 12:48

0 Answers0