Problem with Casablanca SDK

Feb 3, 2015 at 1:15 PM
I have developed a VC++ listener application using Casablanca SDK. This is the source code.
int _tmain(int argc, _TCHAR* argv[])
{
    __uri_builder uri(L"http://localhost:2001/");__
    http_listener listener(uri.to_uri());
    std::cout << "Welcome to Casablanca" << std::endl;

    CheckOSVersion();

    listener.support(methods::GET, [](http_request req)
    {
        HKEY hKey;
        LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Casablanca", 0, KEY_READ, &hKey);

        bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
        bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);

        std::wstring strValueOfBinDir;
        GetStringRegKey(hKey, L"Encryption", strValueOfBinDir, L"1");

        GIDSdkWriteXML();

        http_response response (status_codes::OK);

        response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
        response.set_body(strValueOfBinDir, U("text/html"));

        req.set_request_uri(L"requestpath");
        req.reply(response);
    });

    listener.support(methods::PUT,[](http_request req)
    {
        std::cout << "Serving PUT" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a POST Request.)</body></html>"), 
            U("text/html"));
    });

    listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
            //req.reply(status_codes::OK, U("<html><body>Received.</body></html>"), U("text/html"));
        });
    }); 

    listener.open().wait();
    fgetc(stdin);
    listener.close().wait();
}
Everything is working fine. But when I change this line
__uri_builder uri(L"http://localhost:2001/");__
Where I replace the localhost with the IP Address, so that the server can be externally accessed. it crashes. Can someone tell me the solution to this?
Feb 3, 2015 at 9:10 PM
Hi maverick786us,

What do you mean by crashes, is an exception throw? If so what is the message of the exception? Can you share the exact string you are using to construct the uri_builder?

Here is a similar discussion about using localhost vs. '*' on Windows.

Thanks,
Steve
Feb 4, 2015 at 6:45 AM
This is the exception message
Unhandled exception at at 0x76A6C41F in Listener.exe: Microsoft C++ exception: web::http::http_exception at memory location 0x0038EEB0.
This is the string, where I replaced localhost with the IP Address.
uri_builder uri(L"http://192.168.15.63:2001/");
And this is where this exception happens.
listener.open().wait();
Feb 4, 2015 at 5:39 PM
Hi maverick786us,

Our listener throws exceptions of type http_listener. Try catching the exception and check what the actual error message and error code is.

Steve
Feb 5, 2015 at 12:33 PM
When I added the try and catch block...
try
    {
        listener.open().wait();
        fgetc(stdin);
        listener.close().wait();
    }
    catch(std::exception const & exp)
    {
        //exp.
    }
This is what it shows in exception.
-       exp <No type information available in symbol file for cpprest110d_2_4.dll>{_Mywhat=0x00000000 <NULL> _Mydofree=...} const std::exception &
+       __vfptr 0x108445c0 {cpprest110d_2_4.dll!const web::http::http_exception::`vftable'} {cpprest110d_2_4.dll!0x1016f9b1, ...}   void * *
+       _Mywhat 0x00000000 <NULL>   const char *
        _Mydofree   false   bool
Does Casablanca has its own exception mechanism?
Feb 5, 2015 at 5:30 PM
Hi maverick786us,

What does the exception message say? You still are not checking it. By the way you also aren't catching the http_exception type that is being thrown. It could contain additional information.
    try
    {
        listener.open().wait();
        fgetc(stdin);
        listener.close().wait();
    }
    catch(const http_exception &e)
    {
        const auto &msg = e.what();
        const auto &error_code = e.error_code();
    }
Check these fields for more information.

Steve
Feb 9, 2015 at 5:25 AM
Edited Feb 9, 2015 at 6:17 AM
Thanks steve for the help. The exception simply says "Error adding the URL to URL group". 2nd time it will show that my IP Address is already in use and I have to restart it.
Feb 9, 2015 at 1:39 PM
I have to send a POST request from a WCF web service form another PC, to this listener application, (just like the way we send a POST request in IIS Server.) thats why need to use the IP Address instead of local Host.
Feb 9, 2015 at 5:56 PM
Hi maverick786us,

What is the std::error_code? It will have the underlying error code encountered from the operating system, and can provide more details. Also instead of hardcoding in the IP address you could use '' for the host component: uri_builder uri(L"http://:2001/");

Steve
Feb 10, 2015 at 12:59 PM
This is what the error code says, when I put the cursor on it while debugging
+       error_code  {_Myval=32 _Mycat=cpprest110d_2_4.dll!0x5149ddd0 <No type information available in symbol file for cpprest110d_2_4.dll> {...} } const std::error_code &
If I don't use hard coded IP Address, then how will I access it from a network PC? Locally it will work if I use http://2001/ but that way, I won't be able to send request from a remote PC. How can I send request remotely, without using the IP Address?
Feb 10, 2015 at 2:47 PM
Hi maverick786us,

The error code value 32 is ERROR_SHARING_VIOLATION, which probably means that something is already listening on your machine on that port. You need to try using another.

The codeplex forum formatted my previous post wrong. Instead of hardcoding the IP address you can use a asterisk '*' for the host name component, i.e. http://*:2001/. This is just for the http_listener, from the client you obviously need to know the location of the server.

Steve
Feb 11, 2015 at 11:18 AM
stevetgates wrote:
Hi maverick786us,

The error code value 32 is ERROR_SHARING_VIOLATION, which probably means that something is already listening on your machine on that port. You need to try using another.

The codeplex forum formatted my previous post wrong. Instead of hardcoding the IP address you can use a asterisk '*' for the host name component, i.e. http://*:2001/. This is just for the http_listener, from the client you obviously need to know the location of the server.

Steve
Steve, I appreciate your help. When I change to

http://localhost:2001/ From an hard coded IP Address, it work. But, it only works locally. If I have to send request to this server from a remote PC, how can it be possible without using the IP Address of the PC, where this listener application resides? Can you demonstrate this with a sample source code?
Feb 11, 2015 at 3:49 PM
Hi maverick786us,

Like I mentioned before in the previous posts, use the asterisk for the hostname, i.e. http://*:4567/

Steve
Feb 12, 2015 at 7:08 AM
stevetgates wrote:
Hi maverick786us,

Like I mentioned before in the previous posts, use the asterisk for the hostname, i.e. http://*:4567/

Steve
OK I changed it from

uri_builder uri(L"http://localhost:2001/"); to uri_builder uri(L"http://*:2001/"); Now locally when I browse using http://localhost:2001/, it work, but not in network. Can you tell me the solution?
Feb 16, 2015 at 7:41 AM
Edited Feb 16, 2015 at 9:47 AM
Like, you've mentioed that, from the client, I need to know the location of the server. Therefore, when, I am using

uri_builder uri(L"http://*:2001/"); So From the client I have to take the IP Address of the PC in which this listener application is running followed by port #? Even after using this http://*:2001, the only way I am able to browse it from other PC is either using localhost or 127.0.0.1

Do I have to unlock port 2001 from the network in order to access it using network?