当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 1 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 随机端口的具体值怎样得到?
帖子发表于 : 2012-09-02 21:03 

注册: 2012-03-09 20:32
帖子: 40
送出感谢: 1
接收感谢: 0 次
请问刚开始给服务器程序端口号赋值0后,服务器程序 获得一个随机端口值,这个随机端口值的具体值怎样得到? 以下是一个简单的时间服务程序(当然这个程序的在正常情况下是使用固定端口的,但为了说明这个问题,我故意设置为随机端口),问题在我的中文注释里。
代码:
/* timeserv.c - a socket-based time of day server
 */

#include  <stdio.h>
#include  <stdlib.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  0   /* our time service random 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( AF_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   */
   }
}


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 1 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 0 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译