解决:没有这样的主人知道吗? [英] Resolve: no such host is known?

查看:71
本文介绍了解决:没有这样的主人知道吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用c ++中的boost解析器解决端点时遇到了一个奇怪的问题。



案例:

我想尝试连接到一个网站http:// localhostIpAddress / test / using boost。

其中服务器的本地地址是172.34.22.11(比如说)。



我正面临错误说**解决:没有这样的主人知道**



但是当我连接到像google这样的网站时。能够成功解析和连接。

此外,即使我尝试在浏览器中打开http :: // localhostIpAddress / test /,它也会成功打开。



以下是我的代码:

I am facing a strange issue while trying to resolve endpoints using boost resolver in c++.

Case:
I am trying to connect to a website http://localhostIpAddress/test/ using boost.
where local address of server is "172.34.22.11"(say).

I am facing the error saying "**resolve: No such host is known**"

But when I am connecting to say website like google.com its able to resolve and connect successfully.
Also,even when I try to open "http:://localhostIpAddress/test/" in a browser, it opens successfully.

below is my code:

    int main()
    {
    		std::cout << "\nWebClient  is starting... \n";
    		boost::asio::io_service IO_Servicehttp;
    		boost::asio::ip::tcp::resolver Resolverhttp(IO_Servicehttp);
    		std::string porthttp = "http";
    		boost::asio::ip::tcp::resolver::query Queryhttp("172.34.22.11/test/", porthttp);  
    		boost::asio::ip::tcp::resolver::iterator EndPointIteratorhttp = Resolverhttp.resolve(Queryhttp);
    
    		g_ClientHttp = new HTTPClient(IO_Servicehttp, EndPointIteratorhttp);
    
    	}
    	catch (std::exception& e)
    	{
    		std::cerr << e.what();
    	}
    }

In HTTPClient.cpp

    HTTPClient::HTTPClient(boost::asio::io_service& IO_Servicehttp, boost::asio::ip::tcp::resolver::iterator EndPointIterhttp)
    : m_IOServicehttp(IO_Servicehttp), m_Sockethttp(IO_Servicehttp),m_EndPointhttp(*EndPointIterhttp)
    {
    	std::cout << "\n Entered: HTTPClient ctor \n";
    	boost::asio::ip::tcp::resolver::iterator endhttp;
    	boost::system::error_code error= boost::asio::error::host_not_found;
    
    	try
    	{
    		while (error && EndPointIterhttp != endhttp) //if error go to next endpoint
    		{
    			m_Sockethttp.async_connect(m_EndPointhttp,boost::bind(&HTTPClient::OnConnect_http, this, boost::asio::placeholders::error, ++EndPointIterhttp));
    		}
    		if(error)
    			throw boost::system::system_error(error);
    	}
    
    	catch (std::exception& e)
    	{
    		std::cerr << e.what() << std::endl;
    	}
    	m_IOServicehttp.run();
    }



我已经浏览了很多谷歌指导的网站,但没有找到与此问题相关的任何内容。

任何帮助或提示将不胜感激



我尝试过:



我试过把hosname172.34.22.11,我能够解决,但我的要求是连接到http://172.34.22.11/test并发送一些数据。

但我无法解析主机名。


I have gone through a lot of website directed by google but haven't found anything related to this issue.
Any help or tip will be much appreciated

What I have tried:

I have tried putting hosname "172.34.22.11" , which I am able to resolve but my requirement is to connect to "http://172.34.22.11/test" and send some data to it.
But I am unable to resolve the host name.

推荐答案

当您使用原始套接字实现协议时



for HTTP您需要按照以下步骤操作



注意:我将使用您提供的示例链接中的代码部分(不测试自己)



首先找到目标终点,你需要一个IP地址和端口(HTTP默认为80)也可以是8080或任何专用服务

如果您有域名或只是主机名,则需要解决问题是获取IP地址

如果您有 http://domain.com/services/api 等URL,则无法传递此URL直接解析

您必须只使用域名 http://domain.com 所以您需要将此URL拆分为2个部分作为主机和资源



考虑以下内容;我有2个变量作为域和资源,并将域变量传递给解析器

when you use raw sockets to implement a protocol

for HTTP you need to follow the steps below

Note : i will use code parts from the sample link you have provided (without testing myself)

first find target end point ,you need an IP address and port (default 80 for HTTP) can also be 8080 or any for dedicated services
If you have a domain name or just a host name you need to resolve this to obtain IP address
If you have a URL such as http://domain.com/services/api you can not pass this URL to resolver directly
You must use only domain name http://domain.com so you need to split this URL to 2 parts as host and resource

consider following; i have 2 variables as domain and resource and passing domain variable to resolver
std::string domain = "http://domain.com";
std::string resource = "/services/api";

boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(domain, "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);



这里我连接到服务器


And here i am connecting to the server

tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);



一旦建立连接就可以将其用于其他交易



并且将使用标题进一步导航

HTTP打包由3部分组成:状态/请求行,标题,内容




once the connection established you can use it for other transactions

and the further navigation will be made with headers
A HTTP packed consist of 3 parts : status/request line ,headers ,content

status/request line
header-field:value
header-field:value
header-field:value
header-field:value

content





现在你需要构造一个请求头,从服务器请求指定的资源

您必须在请求标头中提供主机字段,否则无法找到资源

因为服务器使用该字段将本地文件路径映射到请求资源

考虑使用IP 123.123.123.123的网络服务器,CP下有100个共享主机帐户(网站)
如果您没有提供主机名,您将达到服务器的默认登陆页面/ CP



你也可以添加其他http标头来限定你的请求





Now you need to contruct a request header , request specified resource from server
You have to provide Host field in request header ,resource can not be found otherwise
because server uses that to map local file path to requested resource
consider a web server at IP 123.123.123.123 and there are 100 shared host accounts (web sites) under a CP
if you do not provide host name you will reach default landing page of server/CP

you can also add other http headers to qualify your request

std::string domain = "http://domain.com";
std::string resource = "/services/api";
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET " << resource << " HTTP/1.0\r\n";
request_stream << "Host: " << domain << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: keep-alive\r\n\r\n";

boost::asio::write(socket, request);



如果你注意到有额外的(空)行标题的结尾 \\\\ n 告诉服务器没有更多标题可供阅读

对于简单的GET请求,您不需要发送内容



现在是获得回复的时候了,通过查询回复行验证回复

查看您是否已收到状态200以外的代码,或者http响应以外的其他代码




If you noticed that there is a extra (empty) line at the end of header \r\n that tells server there is no more headers to read
For a simple GET request you do not need to send a content

Now time has come to get response ,validate the response by cheking the response line
See if you have recevied a status code other than 200 , or something other than a http response

boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
std::istream response_stream(&response);

std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
     std::cout << "Invalid response\n";
      return 1;
    }
    if (status_code != 200)
    {
      std::cout << "Response returned with status code " << status_code << "\n";
      return 1;
    }
}





现在读取响应标题,检查内容类型,编码,长度等。



Now read response headers ,check content type ,encoding ,length etc..

boost::asio::read_until(socket, response, "\r\n\r\n");

  
   std::string header;
   while (std::getline(response_stream, header) && header != "\r")
     std::cout << header << "\n";
   std::cout << "\n";





最后响应变量包含您希望从服务器接收的内容



finally the response variable contains the content that you expected to receive from server


这篇关于解决:没有这样的主人知道吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆