关于C语言函数gethostbyname

工作中遇到个奇怪的问题,就是函数gethostbyname在2.6.18-308.el5PAE编译的版本在2.6.32-431.el6.x86_64上运行,就无法获取linux主机的主机名,采用的函数代码是从网络上

复制的:

#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
#include<string.h>
#include<errno.h>
int main(int argc, char **argv)
{
    char   *ptr, **pptr;
    struct hostent *hptr;
    char   str[32];
    ptr = argv[1];
    char*message;


    if((hptr = gethostbyname(ptr)) == NULL)
    {
        printf(" gethostbyname error for host:%s\n", ptr);
        message=strerror(errno);
        printf("Mesg:%s\n",message);
        return 0;
    }


    printf("official hostname:%s\n",hptr->h_name);
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
        printf(" alias:%s\n",*pptr);


    switch(hptr->h_addrtype)
    {
        case AF_INET:
        case AF_INET6:
            pptr=hptr->h_addr_list;
            for(; *pptr!=NULL; pptr++)
                printf(" address:%s\n",
                       inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
            printf(" first address: %s\n",
                       inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
        break;
        default:
            printf("unknown address type\n");
        break;
    }


    return 0;
}
~
"1.c" 43L, 1107C 已写入
[test]:[/dsg11/cxf]$ gcc 1.c -o host_test
[test]:[/dsg11/cxf]$ ./host_test test

挺好用的,解决了工作中的问题,记录下



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用write函数对每个字符数组进行发送,或者将多个字符数组组合成一个字符串再发送。下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <string.h> #define SERVER_PORT 8888 #define MAX_LINE 256 int main(int argc, char *argv[]) { struct sockaddr_in sin; char *host; char buf[MAX_LINE]; int s; int len; if (argc == 2) { host = argv[1]; } else { fprintf(stderr, "usage: simplex-talk host\n"); exit(1); } /* translate host name into peer's IP address */ struct hostent *phe = gethostbyname(host); if (!phe) { fprintf(stderr, "simplex-talk: unknown host: %s\n", host); exit(1); } /* build address data structure */ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); sin.sin_port = htons(SERVER_PORT); /* active open */ if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk: socket"); exit(1); } if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("simplex-talk: connect"); close(s); exit(1); } /* send multiple strings */ char *str1 = "Hello"; char *str2 = "World"; char *str3 = "!"; len = strlen(str1); if (write(s, str1, len) != len) { perror("simplex-talk: write"); close(s); exit(1); } len = strlen(str2); if (write(s, str2, len) != len) { perror("simplex-talk: write"); close(s); exit(1); } len = strlen(str3); if (write(s, str3, len) != len) { perror("simplex-talk: write"); close(s); exit(1); } close(s); return 0; } ``` 在这个示例代码中,我们使用了三个字符数组 `str1`、`str2`、`str3` 来表示要发送的字符串,然后分别使用 `write` 函数将它们发送到服务器。注意,每次发送需要指定要发送的字符数组的长度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值