2 Pluspunkte 0 Minuspunkte
Wie kann ich unter Windows mit MinGW die IP Adresse eines Server finden um einen HTTP GET Request in C abzusetzen?
von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Du kannst die Funktion getaddrinfo verwenden, um die IP-Adresse eines Servers basierend auf dem Hostnamen zu erhalten.

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

int main() {

    const char *hostname = "example.com";
    const char *port = "80"; // HTTP-Port

    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        fprintf(stderr, "Failed to initialize Winsock\n");
        return 1;
    }

    struct addrinfo hints, *res;
    int sockfd;

    ZeroMemory(&hints, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    if (getaddrinfo(hostname, port, &hints, &res) != 0) {
        fprintf(stderr, "getaddrinfo failed\n");
        WSACleanup();
        return 1;
    }

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    
    if (sockfd == INVALID_SOCKET) {
        fprintf(stderr, "Error creating socket\n");
        freeaddrinfo(res);
        WSACleanup();
        return 1;
    }

    if (connect(sockfd, res->ai_addr, (int)res->ai_addrlen) == SOCKET_ERROR) {
        fprintf(stderr, "Error connecting to server\n");
        closesocket(sockfd);
        freeaddrinfo(res);
        WSACleanup();
        return 1;
    }

    const char *http_request = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
    send(sockfd, http_request, (int)strlen(http_request), 0);

    char buffer[4096];
    int bytes_received;

    while ((bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) {
        buffer[bytes_received] = '\0';
        printf("%s", buffer);
    }

    if (bytes_received == SOCKET_ERROR) {
        fprintf(stderr, "Error receiving data from server\n");
    }

    closesocket(sockfd);
    freeaddrinfo(res);
    WSACleanup();

    return 0;
}
von (732 Punkte)  
Ich bekomme dabei den folgenden Fehler:

C:\Users\deniz\Desktop>gcc main.c -o main -lws2_32
main.c: In function 'main':
main.c:24:9: warning: implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
     if (getaddrinfo(hostname, port, &hints, &res) != 0) {
         ^~~~~~~~~~~
main.c:33:9: warning: implicit declaration of function 'freeaddrinfo' [-Wimplicit-function-declaration]
         freeaddrinfo(res);
         ^~~~~~~~~~~~
C:\Users\deniz\AppData\Local\Temp\cc9Zi4Ca.o:main.c:(.text+0xca): undefined reference to `getaddrinfo'
C:\Users\deniz\AppData\Local\Temp\cc9Zi4Ca.o:main.c:(.text+0x172): undefined reference to `freeaddrinfo'
C:\Users\deniz\AppData\Local\Temp\cc9Zi4Ca.o:main.c:(.text+0x1f4): undefined reference to `freeaddrinfo'
C:\Users\deniz\AppData\Local\Temp\cc9Zi4Ca.o:main.c:(.text+0x251): undefined reference to `freeaddrinfo'
collect2.exe: error: ld returned 1 exit status
Füge diese Zeile vor deinen include Statements ein:

#define _WIN32_WINNT 0x0501
Linux HTTP Request in C