学习socket编程,以下是错误部分的剪切/粘贴。
仅供参考,我正在关注本教程。
无法偿还的gethostbyname()
回报struct hostent
struct hostent *gethostbyname(const char *name);
使用以下代码。
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <stdlib.h>
6 #include <strings.h>
7
8 int main(int argc, char *argv[])
9 {
10 int sockfd, portno, n;
11 struct sockaddr_in serv_addr;
12 struct hostent *server;
13
14 server = gethostbyname(argv[1]);
15
16 /* compose serv_addr */
17 bzero( (char *)&serv_addr, sizeof(serv_addr) );
18 serv_addr.sin_family = AF_INET;
19 bcopy( (char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
20 serv_addr.sin_port = htons(portno);
21
22 return 0;
23 }
我有以下 gcc 错误/警告
$gcc client2.c
client2.c: In function ‘main’:
client2.c:14: warning: assignment makes pointer from integer without a cast
client2.c:19: error: dereferencing pointer to incomplete type
client2.c:19: error: dereferencing pointer to incomplete type
请指出我做错了什么?