星海's Blog

老头初学编程
APUE学习笔记(二)

APUE学习笔记(三)

星海 posted @ 2011年9月01日 21:25 in APUE && UNP , 1763 阅读
#include	<stdlib.h>
#include	<stdio.h>
#include	<netdb.h>
#include	<arpa/inet.h>


/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: 注意h_aliases指向指针数组,注意netname的增减。
 * =====================================================================================
 */
int
main ( int argc, char *argv[] )
{
        char **netname = NULL;
        struct hostent *tmp;
        char a[INET_ADDRSTRLEN];

        while ((tmp = gethostent()) != NULL) {
                printf("netname: %s\n", tmp->h_name);
                for (netname = tmp->h_aliases; *netname != NULL; netname++)  {
                        printf("alias : %s\n", *netname);
                }

                printf("%d\n", tmp->h_addrtype);
                for (int i = 0; tmp->h_addr_list[i] != NULL; i++) {
                        printf("ip  %s\n",  inet_ntop(AF_INET, tmp->h_addr_list[i], a, INET_ADDRSTRLEN));
                }
                printf("\n");
        }
        endnetent();
        return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
#include	<stdlib.h>
#include	<stdio.h>
#include	<netdb.h>
#include	<arpa/inet.h>


/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: getnetent,此命令的实现问题在于只是读取了/etc/networks里的内
 *  容,对本机地址,主机名等并不探测,目前尚不了解此命令的限定。
 * =====================================================================================
 */
int
main ( int argc, char *argv[] )
{
        char *netname = NULL;
        struct netent *tmp;
        char a[INET_ADDRSTRLEN];

        while ((tmp = getnetent()) != NULL) {
                printf("netname: %s\n", tmp->n_name);
                for (netname = *(tmp->n_aliases); netname != NULL; netname++)  {
                        printf("alias : %s\n", netname);
                }
                printf("%d\n", tmp->n_addrtype);
                int iii = tmp->n_net;
                printf("ip  %s\n",  inet_ntop(AF_INET, &iii, a, INET_ADDRSTRLEN));
        }
        endnetent();
        return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
#include	<stdio.h>
#include	<stdlib.h>
#include	<netdb.h>
#include	<arpa/inet.h>

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: 只是协议名字与端口号,例如TCP IP协议等。依赖于/etc/protocols文件
 * =====================================================================================
 */
int
main ( int argc, char *argv[] )
{
        char **name;
        struct protoent *tmp;

        while ((tmp = getprotoent()) != NULL) {
                printf("protocol name: %s\n", tmp->p_name);
                for (name = tmp->p_aliases; *name != NULL; name++) {
                        printf("alias: %s \n", *name);
                }
                printf("protocol num: %d\n", tmp->p_proto);
        }
        endprotoent();
        return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
#include	<stdio.h>
#include	<stdlib.h>
#include	<netdb.h>
#include	<arpa/inet.h>

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: 获取服务名字,采用协议与端口号等,例如www服务,ftp服务。参照/etc/services文件获取
 * =====================================================================================
 */
int
main ( int argc, char *argv[] )
{
        char **name;
        struct servent *tmp;

        while ((tmp = getservent()) != NULL) {
                printf("service name: %s\n", tmp->s_name);
                for (name = tmp->s_aliases; *name != NULL; name++) {
                        printf("alias: %s \n", *name);
                }
                printf("port num: %d\n", ntohs(tmp->s_port));
                printf("protocol name: %s\n", tmp->s_proto);
                printf("\n\n\n");
        }
        endservent();
        return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
#include	<stdio.h>
#include	<stdlib.h>
#include	<sys/socket.h>
#include	<netdb.h>
#include	<arpa/inet.h>

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  getaddrinfo
 *  Description:  注意错误消息要调用gai_strerror。
 *  		  hint,还有struct sockaddr到sockaddr_in的转变。且sin_port为网
 *  		 网络字节序。
 * =====================================================================================
 */
int
main ( int argc, char *argv[] )
{
        int err;
        char *name;
        struct addrinfo *ailist;
        struct addrinfo hint;
        struct sockaddr_in *sinp;
        char addr[INET_ADDRSTRLEN];

        hint.ai_flags = AI_CANONNAME;
        hint.ai_family = AF_INET;
        hint.ai_socktype = 0;
        hint.ai_protocol = 0;
        hint.ai_canonname = NULL;
        hint.ai_addr = NULL;
        hint.ai_next = NULL;
        if ((err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0) { /* 注意返回值 */
                printf("%s \n", gai_strerror(err));
                exit(1);
        }
        do {
                printf("family: %d \n", ailist->ai_family);
                printf("socktype: %d \n", ailist->ai_socktype);
                if (ailist->ai_family == AF_INET) {
                        sinp = (struct sockaddr_in *)ailist->ai_addr;
                        printf("port: %d\n", ntohs(sinp->sin_port));
                        name = inet_ntop(AF_INET, &sinp->sin_addr, addr, INET_ADDRSTRLEN);
                        printf("addr: %s\n", name ? name : "fuck addr");
                        printf("canonname %s\n", ailist->ai_canonname);
                }
        } while ((ailist = ailist->ai_next) != NULL);
        freeaddrinfo(ailist);
        return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */

aasdf

Avatar_small
SBI Customer Care Nu 说:
2021年12月23日 19:36

The State Bank Of India has introduced the SBI Customer Care Number for Helpdesk to Check status for Various Services and register complaints about ATM Debit Card, Credit Card, Mobile Banking, Home Loan, Personal Loan, and other services helpline numbers. SBI Customer Care Number Here we have listed SBI’s 24X7 helpline number’s for Toll-Free (24X7) accessible from all landlines and mobile phones in the country in categories wide.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter