I have this code:
#include <cstdio>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
int main() {
int sock = socket(AF_INET, SOCK_DGRAM,0);
printf("%d\n",sock);
}
It works good on linux, socket returns 4.
But it does not work on windows when compiled with mingw g++ main.cc -lws2_32
. It returns -1.
Why is this happening?