八达网

标题: 今天学习linux下的网络编程,程序编译不过,求助! [打印本页]

作者: aaaxxxeee    时间: 2010-3-24 21:57
标题: 今天学习linux下的网络编程,程序编译不过,求助!
本帖最后由 aaaxxxeee 于 2010-3-24 21:59 编辑

编译报如下错误

  1. > cc -Wall -g -std=c99 -o GetAddrInfo GetAddrI
  2. GetAddrInfo.c: In function 'main':
  3. GetAddrInfo.c:16: error: storage size of 'addrCriteria' isn't known
  4. GetAddrInfo.c:25: warning: implicit declaration of function 'getaddrinfo'
  5. GetAddrInfo.c:27: warning: implicit declaration of function 'gai_strerror'
  6. GetAddrInfo.c:27: warning: passing argument 2 of 'DieWithUserMessage' makes poit
  7. GetAddrInfo.c:30: error: dereferencing pointer to incomplete type
  8. GetAddrInfo.c:31: error: dereferencing pointer to incomplete type
  9. GetAddrInfo.c:35: warning: implicit declaration of function 'freeaddrinfo'
  10. GetAddrInfo.c:16: warning: unused variable 'addrCriteria'
复制代码
-----
GetAddrInfo.c代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include "Practical.h"

  6. int main(int argc, char *argv[]) {

  7.   if (argc != 3) // Test for correct number of arguments
  8.     DieWithUserMessage("Parameter(s)", "<Address/Name> <Port/Service>");

  9.   char *addrString = argv[1];   // Server address/name
  10.   char *portString = argv[2];   // Server port/service

  11.   // Tell the system what kind(s) of address info we want
  12.   struct addrinfo addrCriteria;                   // Criteria for address match
  13.   memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
  14.   addrCriteria.ai_family = AF_UNSPEC;             // Any address family
  15.   addrCriteria.ai_socktype = SOCK_STREAM;         // Only stream sockets
  16.   addrCriteria.ai_protocol = IPPROTO_TCP;         // Only TCP protocol

  17.   // Get address(es) associated with the specified name/service
  18.   struct addrinfo *addrList; // Holder for list of addresses returned
  19.   // Modify servAddr contents to reference linked list of addresses
  20.   int rtnVal = getaddrinfo(addrString, portString, &addrCriteria, &addrList);
  21.   if (rtnVal != 0)
  22.     DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));

  23.   // Display returned addresses
  24.   for (struct addrinfo *addr = addrList; addr != NULL; addr = addr->ai_next) {
  25.     PrintSocketAddress(addr->ai_addr, stdout);
  26.     fputc('\n', stdout);
  27.   }

  28.   freeaddrinfo(addrList); // Free addrinfo allocated in getaddrinfo()

  29.   exit(0);
  30. }
复制代码
------------
Practical.h
  1. #ifndef PRACTICAL_H_
  2. #define PRACTICAL_H_

  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <sys/socket.h>

  6. // Handle error with user msg
  7. void DieWithUserMessage(const char *msg, const char *detail);
  8. // Handle error with sys msg
  9. void DieWithSystemMessage(const char *msg);
  10. // Print socket address
  11. void PrintSocketAddress(const struct sockaddr *address, FILE *stream);
  12. // Test socket address equality
  13. bool SockAddrsEqual(const struct sockaddr *addr1, const struct sockaddr *addr2);
  14. // Create, bind, and listen a new TCP server socket
  15. int SetupTCPServerSocket(const char *service);
  16. // Accept a new TCP connection on a server socket
  17. int AcceptTCPConnection(int servSock);
  18. // Handle new TCP client
  19. void HandleTCPClient(int clntSocket);
  20. // Create and connect a new TCP client socket
  21. int SetupTCPClientSocket(const char *server, const char *service);

  22. enum sizeConstants {
  23.   MAXSTRINGLENGTH = 128,
  24.   BUFSIZE = 512,
  25. };

  26. #endif // PRACTICAL_H_
复制代码
求指点!附件为所有代码。
源代码地址为:http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/

TCP_IP_IN_C.rar

44.9 KB, 下载次数: 145


作者: 解晓东    时间: 2010-3-24 22:07
本帖最后由 解晓东 于 2010-3-24 22:08 编辑

memset一般用在数组上吧,应该用bzero吧。
作者: aaaxxxeee    时间: 2010-3-24 22:15
不是我写的,是书附带的代码!
GetAddrInfo.c编译不通过!
作者: OruA    时间: 2010-3-24 22:17
一点都看不懂
作者: dantemustdie    时间: 2010-3-24 22:17
友情帮顶
作者: p-k    时间: 2010-3-24 22:28
#include <sys/types.h>
#include <sys/socket.h>
加上试试?
作者: 解晓东    时间: 2010-3-24 22:30
编译的时候Practical.h头文件没加进去。
作者: aaaxxxeee    时间: 2010-3-24 22:49
#include
#include
加上试试?
p-k 发表于 2010-3-24 22:28
  1. /* clientlookup.c */

  2. #include <netdb.h>
  3. #include <stdio.h>
  4. #include <string.h>

  5. /* add  from 8da */
  6. #include <sys/types.h>
  7. #include <sys/socket.h>

  8. int
  9. main ( int argc, const char **argv )
  10. {
  11.     struct addrinfo hints, *addr;
  12.     const char *host = argv[1], *service = argv[2];
  13.     int rc;

  14.     if ( argc != 3 )
  15.     {
  16.         fprintf ( stderr, "exactly two arguments are needed\n" );
  17.         return 1;
  18.     }

  19.     memset ( &hints, 0, sizeof ( hints ) );

  20.     hints.ai_socktype = SOCK_STREAM;
  21.     hints.ai_flags = AI_ADDRCONFIG;
  22.     if ( ( rc = getaddrinfo ( host, service, &hints, &addr ) ) )
  23.         fprintf ( stderr, "lookup failed\n" );
  24.     else
  25.         freeaddrinfo ( addr );

  26.     return 0;
  27. }
复制代码
------
  1. tmp >cc     clientlookup.c   -o clientlookup
  2. clientlookup.c: In function `main':
  3. clientlookup.c:14: error: storage size of 'hints' isn't known
  4. clientlookup.c:27: error: `AI_ADDRCONFIG' undeclared (first use in this function)
  5. clientlookup.c:27: error: (Each undeclared identifier is reported only once
  6. clientlookup.c:27: error: for each function it appears in.)
复制代码
从另一本书找了个短的代码还是不行啊
作者: lf426    时间: 2010-3-25 00:40
你要是对Linux和GCC一点了解都没有还是别这么玩了。
欢迎加入Linux阵营,但是请从头学起。
你连这段代码做什么的都不知道编译个毛啊,我两年前写了一个小教程,不过只写了第一节,你看对你入门有没点帮助。
http://www.cppblog.com/lf426/archive/2008/07/08/55641.html
作者: lf426    时间: 2010-3-25 00:55
addrinfo的声明在什么地方?
作者: aaaxxxeee    时间: 2010-3-25 09:10
你要是对Linux和GCC一点了解都没有还是别这么玩了。
欢迎加入Linux阵营,但是请从头学起。
你连这段代码做什么的都不知道编译个毛啊,我两年前写了一个小教程,不过只写了第一节,你看对你入门有没点帮助。
http: ...
lf426 发表于 2010-3-25 00:40

我了解啊。只是对getaddrinfo第一次用,能举个例子么?
作者: aaaxxxeee    时间: 2010-3-25 09:11
addrinfo的声明在什么地方?
lf426 发表于 2010-3-25 00:55

linux下,man 3 getaddrinfo。有显示这个定义啊。就是编译不过。
作者: nttstar    时间: 2010-3-25 09:27
GetAddrInfo.c:16: error: storage size of 'addrCriteria' isn't known

addrCriteria类型没定义, 只有声明
作者: KoMoS    时间: 2010-3-25 09:37
编译的时候Practical.h头文件没加进去。
解晓东 发表于 2010-3-24 22:30


我很赞同
作者: OruA    时间: 2010-3-25 09:40
NTT哥威武
作者: graces    时间: 2010-3-25 09:45
友情帮顶
作者: lf426    时间: 2010-3-25 10:00
12# aaaxxxeee


首先你要弄清楚addrinfo是在哪里申明的,找到源代码;然后你要搞清楚这个申明是从什么地方include进你的程序的。
作者: intothebbc    时间: 2010-3-25 10:11
GetAddrInfo.c:16: error: storage size of 'addrCriteria' isn't known
'addrCriteria' 结构是那引用的?

GetAddrInfo.c:30: error: dereferencing pointer to incomplete type
还有说你的指针 指向的类型不对 检查下你的指针 指的那!
GetAddrInfo.c:31: error: dereferencing pointer to incomplete type
作者: 朴善英    时间: 2010-8-15 23:08
一点都看不懂
OruA 发表于 2010-3-24 22:17



好的o老湿




欢迎光临 八达网 (https://www.8-da.com/) Powered by Discuz! X2.5