免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2396 | 回复: 2
打印 上一主题 下一主题

[C] 关于bind的用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-22 21:27 |只看该作者 |倒序浏览
下面的例子选自bruce molay的《understanding unix and linux programming》
/* 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   */
        }
}

我在debian testing的环境下编译:
cc timeserv.c -o timeserv
程序运行:
./timeserv&
bind总是有错。
有人能帮解释一下吗? 谢谢!

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
2 [报告]
发表于 2013-11-22 22:05 |只看该作者
使用perror把绑定错误打印出来  即可看到错误原因

论坛徽章:
7
摩羯座
日期:2013-12-05 10:42:57辰龙
日期:2013-12-27 13:40:49亥猪
日期:2014-01-15 09:10:37天秤座
日期:2014-01-20 11:22:20辰龙
日期:2014-01-26 17:02:25午马
日期:2014-01-27 14:22:34水瓶座
日期:2014-02-19 09:36:40
3 [报告]
发表于 2013-11-22 22:27 |只看该作者
有可能端口已经被占用了, netstat -nalp | grep 13000 看看。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP