Search code examples
c++socketsgetaddrinfo

c++ socket programming with url:port/url in getaddrinfo


Relearning C++ and fairly new to sockets. I have a Python app that connects properly and a .NET app that works.

The URL I need to call is domain.com:8080/signalr. I'm following what seems to be a standard example, e.g.:

if ((rv = getaddrinfo("www.domain.com", PORT, &hints, &servinfo)) != 0) {

...but no matter what I try, I get name or service unknown.

  • Calling domain.com/signalr:8080 will not work.
  • I tried leaving PORT null

This is running on Raspbian (Debian) if that matters.

Any suggestions would be greatly appreciated.


Solution

  • What getaddrinfo() does is name resolution. It does not implement the HTTP protocol. Also, it does not know how to parse the URL, you have to do that yourself.

    The right call would be something along the lines of:

    getaddrinfo("www.Domain.com", "8080", ...);
    

    And that will return the right struct sockaddr* to call connect() and do all the socket stuff.

    If you want a function that does all the HTTP protocol for you, you'll need a higher level library. I recommend libcurl.