سؤال

I'm having problems getting winsock to work, I'm just baffled and don't really know what to try next.

the getaddrinfo(NULL) is just there to show that it recognizes what arguments getaddrinfo should have but it still says it can't resolve it..

When I try to compile it in command line and add some error flags, this is what I get:

C:\MinGW\bin>g++ -O0 -g3 -Wall -c -fmessage-length=0 -o test2.exe C:\Users\David\
workspace\vmulti\Debug\test2.o -lws2_32 -lmingw32
g++: warning: C:\Users\David\workspace\vmulti\Debug\test2.o: linker input file un
used because linking not done

Here's the error the compiler throws at me, I'm compiling with mingw:

Function 'getaddrinfo' could not be resolved    test.cpp        /vmulti line 48  Semantic Error
Function 'getaddrinfo' could not be resolved    test.cpp        /vmulti line 50 Semantic Error
too few arguments to function 'int getaddrinfo(const char*, const char*, const addrinfo*, addrinfo**)'  test.cpp        /vmulti line 48 C/C++ Problem

Here's the code:

ws2tcpip.h:

#if (_WIN32_WINNT >= 0x0501)
void WSAAPI freeaddrinfo (struct addrinfo*);
int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
                    struct addrinfo**);
int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
                   char*,DWORD,int);
#else
/* FIXME: Need WS protocol-independent API helpers.  */
#endif

test.cpp (my program):

#define _WIN32_WINNT 0x0501
#include <string.h>
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <ws2tcpip.h>
using namespace std;

#define MYPORT "3490"  // the port users will be connecting to
#define BACKLOG 10     // how many pending connections queue will hold

void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(int argc, char *argv[])
{
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
                fprintf(stderr, "WSAStartup failed.\n");
                exit(1);
        }
    struct sockaddr_storage their_addr;
    int addr_size;
    struct addrinfo hints, *res;
    int sockfd, new_fd, len, numbytes;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;     // fill in my IP for me

    getaddrinfo(NULL);  // Line 48

    if(getaddrinfo(NULL, MYPORT, &hints, &res) == -1); // Line 50
      cout << "Getaddrinfo error" << endl;
هل كانت مفيدة؟

المحلول

Whey! I'm two years late to the party. But I've an answer for you & others in the same boat :D

It's not a bug in Eclipse, it's a bug in MinGW - right before freeaddrinfo/getaddrinfo/getnameinfo, the latest version of mingw's headers states:

#if (_WIN32_WINNT >= _WIN32_WINNT_WINXP)
/**
 * For WIN2K the user includes wspiapi.h for these functions.
 */

... which makes no sense, because mingw doesn't include that header.

To fix: I added forward declarations for the functions in my app & linked with ws2_32.lib as per usual. My app was a simple 1-file app - it might make more sense to add this to your own header & include it where needed.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#ifdef __cplusplus
extern "C" {
#endif
   void WSAAPI freeaddrinfo( struct addrinfo* );

   int WSAAPI getaddrinfo( const char*, const char*, const struct addrinfo*,
                 struct addrinfo** );

   int WSAAPI getnameinfo( const struct sockaddr*, socklen_t, char*, DWORD,
                char*, DWORD, int );
#ifdef __cplusplus
}
#endif

int main() { /* ... */ }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top