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

谁说胖子不能爱 提交于 2019-12-25 06:36:40

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23572645/c-socket-programming-with-urlport-url-in-getaddrinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!