How to connect android application to local domun

  android, c++, sockets

I am using esp32 to bring up a tcp server. In order not to access this server by ip, I use mDNS on the esp32 side. I can successfully contact the tcp server from the PC at esp32.local. Now I want to do the same from android application. I wrote a little c ++ code which I run on android.

 int sockfd, connfd;
    struct sockaddr_in servaddr, cli;

    // socket create and varification
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        hello ="socket creation failed...";
        return env->NewStringUTF(hello.c_str());
    }
    else

        hello="Socket successfully created..n";
    bzero(&servaddr, sizeof(servaddr));

    // assign IP, PORT
    servaddr.sin_family = AF_INET;
    std::string url = "esp32.local";
    servaddr.sin_addr.s_addr = inet_addr(url.c_str());
    servaddr.sin_port = htons(1234);
    struct hostent *result;
    result = gethostbyname(url.c_str());
    //  destAddr.sin_addr.s_addr = ((in_addr*)hosten->h_addr_list[0])->s_addr;
    if (!result)
    {
        hello ="gethostbyname...";
        return env->NewStringUTF(hello.c_str());
    }

    // puts(result->h_name);
    memmove(&(servaddr.sin_addr.s_addr), result->h_addr, result->h_length);

    // connect the client socket to server socket
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
        hello="connection with the server failed..";
        return env->NewStringUTF(hello.c_str());
    }
    else
        printf("connected to the server..n");

    // function for chat
    char buff = 0xA;
    write(sockfd, &buff, sizeof(buff));

    // close the socket
    close(sockfd);

When trying to connect from android application, I get an error in function gethostbyname. . What do I need to do to access the .local domain?

Source: Windows Questions C++

LEAVE A COMMENT