Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolving network node using boost::asio and an std::wstring

I am using Boost::Asio for network communication. The following code fails with the following compilation error.

Code:

std::wstring hostName(L"myserver");
std::wstring portName(L"myport");
auto query = boost::asio::ip::udp::resolver::query(boost::asio::ip::udp::v4(), hostName, portName);

Compilation error:

Error   1   error C2665: 'boost::asio::ip::basic_resolver_query<InternetProtocol>::basic_resolver_query' :
none of the 5 overloads could convert all the argument types    

My questions:

  • Does Boost::Asio expect ANSI-strings (std::string) only?
  • Does this mean that hostnames with non-ANSI characters are not supported?
  • Or do I have to convert my wide strings to UTF8 first?
  • And in the latter case, does Boost::Asio provide standard methods to do this or can I use my own ANSI/Unicode conversion routines?

Currently running on Windows, using Visual Studio 2013.

Thanks in advance.

like image 778
Patrick Avatar asked Apr 29 '26 01:04

Patrick


1 Answers

Does Boost::Asio expect ANSI-strings (std::string) only? It expects std::string, but not necessarily ANSI, since std::string can store UTF-8.

Does this mean that hostnames with non-ANSI characters are not supported? I haven't personally tried it, but since this is essentially a wrapper on top of the underlying OS networking code, I can't find any reason it wouldn't support using UTF-8.

Or do I have to convert my wide strings to UTF8 first? Most likely, yes, since the wide string format is only used by core Windows APIs, while UTF-8 is used by everyone else including Boost.

And in the latter case, does Boost::Asio provide standard methods to do this or can I use my own ANSI/Unicode conversion routines? Boost doesn't appear to support wide-char. As long as you are using Windows-specific character encoding, you can either use the COM interfaces that are provided by Windows for converting (MultiByteToWideChar, WideCharToMultiByte), or use your own routines. If your own routines are known to be reliable/correct, and you are not already using COM, you might want to stick with your own rather than adding a dependency on COM to your project.

like image 111
Matt Jordan Avatar answered Apr 30 '26 16:04

Matt Jordan