What are often used network programming functions/

2020-06-28 16:26发布

All of us who still do some kind of network programming (TCP/UDP, DNS or Client/Server) in C repeatedly use some code snippets again and again.

We do use some standard libraries but then also we do write some code very often which is not there in one library.

Is there a collection of such code snippets that are used very often. If not then lets build it here.

3条回答
地球回转人心会变
2楼-- · 2020-06-28 17:06

W. Richard Stephens wrote a collection of such snippets: UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-06-28 17:10

Here is UNIX Network Programming, Volume 1, Third Edition Source Code Here

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-06-28 17:11

Good question!

Here is a Name resolution Function

    struct hostent {
        char *h_name; // main name
        char **h_aliases; // alternative names (aliases)
        int h_addrtype; // address type (usually AF_INET)
        int h_length; // length of address (in octets)
        char **h_addr_list; // alternate addresses (in Network Byte Order)
    };
    #define h_addr h_addr_list[0] // First address of h_addr_list.


    struct hostent *info_stackoverflow;
    int i = 0;
    info_stackoverflow = gethostbyname( "www.stackoverflow.com" );
    printf("The IP address of %s is %s", 
           info_stackoverflow->h_name, 
           inet_ntoa( * ((struct in_addr *)info_stackoverflow->h_addr )));
    /* aliases */
    while( *(pc_ip->h_aliases + i) != NULL )
    {
        printf("\n\tAlias: %s", *(pc_ip->h_aliases + i) );
        i++;
    }
查看更多
登录 后发表回答