#include <WinSock2.h>
#include <WS2tcpip.h>
#include "stdio.h"
int main(int argc, wchar_t *argv[]) {
//init Windows Sockets
//IResult
int IResult = NULL;
WSADATA wsaDataStruct;
IResult = WSAStartup(MAKEWORD(2, 2), &wsaDataStruct);
if (IResult != 0) // Error
{
//printf u are fucked;
printf("Error: Winsock2 Init, %d", IResult);
return 1;
}
//Result is output of getaddrinfo, hints is a helper struct
struct addrinfo*
result = NULL, //Linked list of addresses
* ptr = NULL,
hints = {};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
//Hostname
char hostname[64] = {};
gethostname(hostname, 64);
IResult = getaddrinfo(hostname, "4500", &hints, &result);
if (IResult != 0) { //Smth bad happened get Error
printf("Error: Winsock2 addrinfo, %d", IResult);
WSACleanup();
return 2;
}
//Loop addresses
addrinfo* addr = result;
while (addr!=nullptr)
{
char ip[16];
inet_ntop(addr->ai_family, addr->ai_addr, ip, 16);
printf("Address found: %s", ip);
addr = addr->ai_next;
}
return 0;
}
I am looping through the linked list of addrinfo
structs and so far it returns one struct only which has an address of 2.0.17.148, while my machine has a local address of 192.168.2.1
I have only 1 network interface, and addrinfo
returns a valid struct.
result
is pointing to the interface address you expect? Or even to a validaddrinfo
structure? Please try to show us a minimal reproducible example, and try to list all the interfaces available on your system.inet_ntop
to see if there were any problems?