Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用getaddrinfo编译错误_C_Sockets - Fatal编程技术网

C 使用getaddrinfo编译错误

C 使用getaddrinfo编译错误,c,sockets,C,Sockets,我正在尝试学习套接字编程的基础知识,并且正在使用我找到的指南中的一些代码,但是我遇到了编译错误。代码和错误如下所示 #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #define PORT "21467" int main(void) { int status; struct addrinfo hints; struct addrinfo *servinfo; // wil

我正在尝试学习套接字编程的基础知识,并且正在使用我找到的指南中的一些代码,但是我遇到了编译错误。代码和错误如下所示

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define PORT "21467"

int main(void)
{
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty

hints.ai_family = AF_INET; 
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me

if ((status = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
    //fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
}
// servinfo now points to a linked list of 1 or more struct addrinfos
// ... do everything until you don't need servinfo anymore ....
freeaddrinfo(servinfo); // free the linked-list

return 0;
}
任何帮助都将不胜感激

如果你做了一个测试,你会看到手册页面上说需要
#include
。把它放在文件的顶部。对退出执行相同的操作

编辑


需要
-lnsl-lsocket
进行链接(假设是linux)

@Ryaan:如果您指定用于编译和链接的命令、工具集和操作系统的版本(我猜是OS X),这将非常有用.我刚刚在一台带有GCC4.6.1的Linux机器上进行了尝试,添加了#include和#include后,编译正常,没有任何警告。您使用的是什么操作系统和编译器?我在运行GCC4.2.1的Unix上运行这个。很抱歉没有包括这个。在包含了这两个库之后,我只引用了未定义的第一个库,下面给出了错误。-lnsl-lsocket用于Solaris,其中套接字内容不在-lc(即Linux/glibc)中。您的权利。我读错了Makefile的一部分。
server.c: In function 'main':
server.c:16: warning: incompatible implicit declaration of built-in function 'memset'
server.c:24: warning: incompatible implicit declaration of built-in function 'exit'
Undefined                       first referenced
 symbol                             in file
getaddrinfo                         /var/tmp//ccU0yRDe.o
freeaddrinfo                        /var/tmp//ccU0yRDe.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status