- main.c
- --------------------------------------------------------
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include "socket.h"
- extern int h_errno;
- int main(int argc, char** argv)
- {
- int ret;
- //通常,在给定的协议族中 已存在协议进行对特定的socket进行支持,所以 protocol设置为0
- int fd_sock = socket(AF_INET, SOCK_STREAM, 0);
- if(fd_sock == -1)
- {
- perror("sock\n");
- exit(1);
- }
- //测试gethostent函数
- test_gethostent();
- putchar('\n');
- //测试getnetent函数
- test_getnetent();
- ret = shutdown(fd_sock, SHUT_RDWR);
- if(ret == -1)
- {
- perror("shutdow\n");
- exit(1);
- }
- #if 0
- close 与 shutdown的区别
- close 只会在最后一个引用被释放时才会关闭socket描述符
- shutdow 不论什么状态都会关闭,即使有多个引用此socket
- shutdow 还可以选择性关闭 读端还是写端
- close 只能读写端一起关闭
- #endif
- return 0;
- }
- --------------------------------------------------------
- socket.c
- --------------------------------------------------------
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include "socket.h"
- void show_hostent(struct hostent* host)
- {
- printf("hostname: %s\n",host->h_name);
- while(1)
- {
- if(*host->h_aliases == NULL)
- {
- printf("hostaliases: NULL\n");
- break;
- }
- if(strcmp(*host->h_aliases,"(null)") == 0)
- {
- printf("hostaliases: %s\n",*host->h_aliases);
- break;
- }
- printf("hostaliases: %s\n",*host->h_aliases);
- host->h_aliases++;
- }
- //AF_INET=2 AF_INET6=10
- switch(host->h_addrtype)
- {
- case AF_INET:
- {
- printf("addresstype:AF_INET\n");
- break;
- }
- case AF_INET6:
- {
- printf("addresstype:AF_INET6\n");
- break;
- }
- default:
- {
- printf("addresstype:other AF_XXX\n");
- break;
- }
- }
- printf("address_length:%d byte\n",host->h_addrtype);
- while(*host->h_addr_list != NULL)
- {
- //INET_ADDRSTRLEN ,INET6_ADDRSTRLEN 两个在头文件里面定义了足够大的空间
- char buf[INET_ADDRSTRLEN];
- memset(buf,INET_ADDRSTRLEN,0);
- inet_ntop(AF_INET, (void*)*host->h_addr_list, buf, INET_ADDRSTRLEN);
- printf("host->h_addr_list: %s\n",buf);
- host->h_addr_list++;
- }
- }
- void test_gethostent()
- {
- struct hostent* host = gethostent();
- if(host == NULL)
- {
- perror("gethostent\n");
- exit(1);
- }
- show_hostent(host);
- }
- void test_getnetent(void)
- {
- struct netent *net = getnetent();
- if(net == NULL)
- {
- perror("getnetent\n");
- exit(1);
- }
- show_netent(net);
- }
- void show_netent(struct netent* net)
- {
- printf("n_name:%s\n", net->n_name);
- while(1)
- {
- if(*net->n_aliases == NULL)
- {
- printf("n_aliases: NULL\n");
- break;
- }
- printf("n_aliases:%s\n",*net->n_aliases);
- net->n_aliases++;
- }
- // The type of the network number; always AF_INET.
- printf("n_addrtype:%d(AF_INET)\n", net->n_addrtype);
- char buf[INET_ADDRSTRLEN];
- memset(buf,INET_ADDRSTRLEN,0);
- inet_ntop(AF_INET, (void*)&net->n_net, buf, INET_ADDRSTRLEN);
- printf("n_net: %s\n",buf);
- }
- --------------------------------------------------------
- socket.h
- --------------------------------------------------------
- #ifndef SOCKET_H
- #define SOCKET_H
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <string.h>
- #include <arpa/inet.h>
- void test_gethostent(void);
- void show_hostent(struct hostent*);
- void test_getnetent(void);
- void show_netent(struct netent*);
- #endif//end SOCKET_H
- --------------------------------------------------------
- makefile
- --------------------------------------------------------
- obj =main.o socket.o
- main:$(obj)
- gcc -o main main.o socket.o
- main.o:main.c
- gcc -c main.c
- socket.o:socket.c
- gcc -c socket.c
- .PHONY:clean
- clean:
- rm -f main *~ *gch *.o
转载于:https://blog.51cto.com/642364/1128764