现在的位置: 首页 > 综合 > 正文

都是gethostname()惹的祸

2014年02月04日 ⁄ 综合 ⁄ 共 4557字 ⁄ 字号 评论关闭

今天同学实现一个经典的Unix/Linux网络编程的例子--时间服务器的时候,遇到了一件怪事:“Connection refused” 。服务器端的端口号已经成功打开,客户端也能够ping通服务器端,但是就是不能建立TCP连接获取服务器时间。代码是很经典的代码,贴一下。

服务器端:

/* timeserv.c - a socket-based time of day server
 */

#include  <stdio.h>
#include  <unistd.h>
#include  <sys/types.h>
#include  <sys/socket.h>
#include  <netinet/in.h>
#include  <netdb.h>
#include  <time.h>
#include  <strings.h>

#define   PORTNUM  13000   /* our time service phone number */
#define   HOSTLEN  256
#define   oops(msg)      { perror(msg) ; exit(1) ; }

int main(int ac, char *av[])
{
 struct  sockaddr_in   saddr;   /* build our address here */
 struct hostent  *hp;   /* this is part of our    */
 char hostname[HOSTLEN];     /* address           */
 int sock_id,sock_fd;       /* line id, file desc     */
 FILE *sock_fp;              /* use socket as stream   */
 char    *ctime();              /* convert secs to string */
 time_t  thetime;               /* the time we report     */

      /*
       * Step 1: ask kernel for a socket
       */

 sock_id = socket( PF_INET, SOCK_STREAM, 0 );    /* get a socket */
 if ( sock_id == -1 ) 
  oops( "socket" );

      /*
       * Step 2: bind address to socket.  Address is host,port
       */

 bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct     */

 gethostname( hostname, HOSTLEN );       /* where am I ?         */
 hp = gethostbyname( hostname );         /* get info about host  */
                                         /* fill in host part    */
 bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
 saddr.sin_port = htons(PORTNUM);        /* fill in socket port  */
 saddr.sin_family = AF_INET ;            /* fill in addr family  */

 if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )
        oops( "bind" );

      /*
       * Step 3: allow incoming calls with Qsize=1 on socket
       */

 if ( listen(sock_id, 1) != 0 ) 
  oops( "listen" );

      /*
       * main loop: accept(), write(), close()
       */

 while ( 1 ){
        sock_fd = accept(sock_id, NULL, NULL); /* wait for call */
  printf("Wow! got a call!\n");
        if ( sock_fd == -1 )
         oops( "accept" );       /* error getting calls  */

        sock_fp = fdopen(sock_fd,"w");  /* we'll write to the   */
        if ( sock_fp == NULL )          /* socket as a stream   */
         oops( "fdopen" );       /* unless we can't      */

        thetime = time(NULL);           /* get time             */
            /* and convert to strng */
        fprintf( sock_fp, "The time here is .." );
        fprintf( sock_fp, "%s", ctime(&thetime) ); 
        fclose( sock_fp );              /* release connection   */
 }
}

客户端:

/* timeclnt.c - a client for timeserv.c
 *              usage: timeclnt hostname portnumber
 */
#include       <stdio.h>
#include       <sys/types.h>
#include       <sys/socket.h>
#include       <netinet/in.h>
#include       <netdb.h>

#define        oops(msg)       { perror(msg); exit(1); }

main(int ac, char *av[])
{
 struct sockaddr_in  servadd;        /* the number to call */
 struct hostent      *hp;            /* used to get number */
 int    sock_id, sock_fd;            /* the socket and fd  */
 char   message[BUFSIZ];             /* to receive message */
 int    messlen;                     /* for message length */

     /*
      * Step 1: Get a socket
      */

 sock_id = socket( AF_INET, SOCK_STREAM, 0 );    /* get a line   */
 if ( sock_id == -1 ) 
  oops( "socket" );            /* or fail      */

     /*
      * Step 2: connect to server
      *         need to build address (host,port) of server  first
      */

 bzero( &servadd, sizeof( servadd ) );   /* zero the address     */

 hp = gethostbyname( av[1] );            /* lookup host's ip #   */
 if (hp == NULL) 
  oops(av[1]);             /* or die               */
 bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);

 servadd.sin_port = htons(atoi(av[2]));  /* fill in port number  */

 servadd.sin_family = AF_INET ;          /* fill in socket type  */

             /* now dial     */
 if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0)
        oops( "connect" );

     /*
      * Step 3: transfer data from server, then hangup
      */

 messlen = read(sock_id, message, BUFSIZ);     /* read stuff   */
 if ( messlen == - 1 )
        oops("read") ;
 if ( write( 1, message, messlen ) != messlen )  /* and write to */
        oops( "write" );                        /* stdout       */
 close( sock_id );    
}

    这个代码我至少在《Unix/Linux编程实践》和《Unix环境高级编程》中见过,但是拷到自己机器上运行,却依旧出现了相同的“Connection refused”的错误提示。Connection refused,就说明服务器端的端口没开放或者压根没连到服务器端。这到底是为什么呢,上面的代码找不到什么错误,纠结了一个晚上,终于搞明白了,其实还是在服务器端bind的时候出现差错了。

    看上面服务器端的代码,注意获取本地网络地址以及设置端口号的那一块:

gethostname( hostname, HOSTLEN );       /* where am I ?         */

hp = gethostbyname( hostname );         /* get info about host */

                                        /* fill in host part    */

bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);

saddr.sin_port = htons(PORTNUM);        /* fill in socket port */

saddr.sin_family = AF_INET ;            /* fill in addr family */

    在这里,服务器程序使用了gethostname()函数来获取本机的网络地址,然后用gethostbyname()函数转化为网络字节序地址。问题就出在gethostname()函数身上。该函数的作用是返回主机名,通常就是TCP/IP网络上主机的名字,也就是IP地址。

    这个函数在实现上,会先查找/etc/hosts文件的内容,然后查询DNS服务器。如果/etc/hosts文件没有配置,返回的主机名就是localhost也就是127.0.0.1
。所以,使用bind函数绑定的网络地址其实是一个127.0.0.1 。客户端想connect的话,就找不到连接了。

    然后又查了本科时候学习网络编程时做的实验报告(很汗颜,要翻回去查本科时的资料)。借用了这样的处理方法,查询本地网络地址很好用。

    稍作一下修改,部分代码如下:

      /*
       * Step 2: bind address to socket.  Address is host,port
       */

 bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct     */

 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
 saddr.sin_port = htons(PORTNUM);        /* fill in socket port  */
 saddr.sin_family = AF_INET ;            /* fill in addr family  */

 if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )
        oops( "bind" );

     关键部分就是红色那行,直接设置网络地址为INADDR_ANY 。有的服务器是多宿主机可能有多个网卡,那么运行在这样的服务器上的服务程序在为其Socket绑定IP地址时可以把htonl(INADDR_ANY)置给s_addr,这样做的好处是不论哪个网段上的客户程序都能与该服务程序通信;如果只给运行在多宿主机上的服务程序的Socket绑定一个固定的IP地址,那么就只有与该IP地址处于同一个网段上的客户程序才能与该服务程序通信。

    就是如此这般!

转贴:http://hi.baidu.com/sky_space/blog/item/9fe37506d311647703088119.html

抱歉!评论已关闭.