-2

So I am trying to build up a Server-Client program in C++. I am trying to make a more "Friendly" "Connect" function. But, I am having a problem. I am getting this error when I am running it.

Error 1 error C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings

This is the Connect() function that it returns the error on:

bool Connect(std::string ip, TPort port) {
    sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr(ip.c_str());
    clientService.sin_port = htons(port);
    SOCKET connecter = connect(handle, 
                               (SOCKADDR *)& clientService,
                               sizeof(clientService));
    return (connecter == 0);
}

I searched a bit in the internet and didn't find something that helps me. The error comes from the line:

clientService.sin_addr.s_addr = inet_addr(ip.c_str());

I am using windows API , and Visual Studio 2013

Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
jeff style
  • 61
  • 1
  • 7

1 Answers1

0

There are two ways to work around this:

  • use inet_pton() or InetPton() instead, or
  • define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings.

Hope that helps.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Working !! Thank you alot , for some reason the inet_pton didnt work but the second solution did work ! ty – jeff style Jul 15 '15 at 03:41
  • @user4825345: And what about `InetPton`? You really should focus on getting the "correct" solution working (it works for everybody else!) rather than masking warnings. – Lightness Races in Orbit Jul 15 '15 at 10:49