How to change gethostbyname into getaddrinfo?

287 Views Asked by At

I am writing a function to connect client-server. And i cannot use gethostbyname. This function is simple to use. Now i don't know how to use getaddrinfo. How can i change gethostbyname into getaddrinfo. Thank you so much!

void ConnectSocket()
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        return;
    }

skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

struct hostent* host;

host = gethostbyname("127.0.0.1");

SOCKADDR_IN sockAddr;

sockAddr.sin_port = htons(45444);
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

if (connect(skt, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != 0) {
    return;
}
}
0

There are 0 best solutions below