winsock编译错误,它找不到addrinfo结构和一些相关的函数 [英] winsock compiling error, it cant find the addrinfo structures and some relating functions

查看:1060
本文介绍了winsock编译错误,它找不到addrinfo结构和一些相关的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习winsock通过Beej的网络编程指南书。我在windows下编程,并通过gcc运行它。这只是一个开始写我的第一个服务器程序,但它给我这些错误,当我尝试编译。

  / *服务器* / 
#include< iostream>
#include< windows.h>
#include< winsock2.h>
using namespace std;

const int winsockVersion = 2;
#define BACKLOG 10
#define PORT 3000


int main(void){

WSADATA wsadata;
if(WSAStartup(MAKEWORD(winsockVersion,0),& wsadata)== 0){


struct addrinfo hints,* res;

memset(& hints,0,sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

if(getaddrinfo(NULL,PORT,&hints,& res)== 0){
cout<< - 调用addrinfo成功! << endl;
}

cout<<res af_family< res-> ai_family<< endl;
}




//清除东西
if(WSACleanup()!= 0){
cout< ; - WSACleanup unsuccessful<< endl;
} else {
cout<< - WSACleanup successful< endl;
}


return 0;
}

这些是我收到的错误

  g ++ -o server.exe server.cpp -lws2_32 
进程已启动>>>
server.cpp:在函数`int main()':
server.cpp:20:error:aggregate`addrinfo hints'有不完整的类型,不能定义
server.cpp:25 :错误:`AI_PASSIVE'未在此范围中声明
server.cpp:27:错误:`getaddrinfo'未在此范围中声明
server.cpp:31:error:invalid use of undefined type `struct addrinfo'
server.cpp:20:错误:`struct addrinfo'的向前声明
server.cpp:54:2:warning:文件末尾没有换行符
< ;&过程完成。

不应该在windows.h或winsock.h中定义结构和函数。 / p>

解决方案



strong>
给任何绊倒这个的人,添加

  #define _WIN32_WINNT 0x501 
#include< ws2tcpip.h>如果getaddrinfo表示其未声明,则

解决方案

您可能想要 #include< ws2tcpip.h> 。请注意,在Stack Overflow之前, Google 是您的朋友的问题:您将从MSDN立即得到答案!


I've just started learning winsock through the "Beej's guide to network programming" book. I'm programming under windows and running it through gcc. This is just a start to writing my first server program but it gives me these errors when I try to compile.

/* Server */
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;

const int winsockVersion = 2;
#define BACKLOG 10
#define PORT 3000


int main(void){

    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(winsockVersion,0),&wsadata) == 0){


        struct addrinfo hints, *res;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;

        if ( getaddrinfo(NULL,PORT,&hints,&res) == 0 ){
            cout<<"-Call to get addrinfo successful!." << endl;
        }

        cout<<"res af_family" << res->ai_family << endl;
    }




    //clear stuff
    if( WSACleanup() != 0){
        cout<<"-WSACleanup unsuccessful" << endl;
    }else{
        cout<<"-WSACleanup successful" << endl;
    }


    return 0;
}

these are the errors I'm receiving

g++ -o server.exe server.cpp -lws2_32
Process started >>>
server.cpp: In function `int main()':
server.cpp:20: error: aggregate `addrinfo hints' has incomplete type and cannot be defined
server.cpp:25: error: `AI_PASSIVE' was not declared in this scope
server.cpp:27: error: `getaddrinfo' was not declared in this scope
server.cpp:31: error: invalid use of undefined type `struct addrinfo'
server.cpp:20: error: forward declaration of `struct addrinfo'
server.cpp:54:2: warning: no newline at end of file
<<< Process finished.

Shouldn't the structures and functions be defined in either windows.h or winsock.h?.

SOLUTION

EDIT to anyone who stumbles on this, add

#define _WIN32_WINNT 0x501
#include <ws2tcpip.h>

at the top of your source if getaddrinfo says that its undeclared.

解决方案

You probably want to #include <ws2tcpip.h>. Remember that before Stack Overflow, Google is your friend for this kind of questions : you will get immediate answers from MSDN !

这篇关于winsock编译错误,它找不到addrinfo结构和一些相关的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆