免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3167 | 回复: 7

[C] 急求一个write系统调用出错的原因 [复制链接]

论坛徽章:
0
发表于 2014-02-28 20:43 |显示全部楼层
使用数据报socket进行网络通信,服务器端无问题,但客服端接受服务端信息后,用write打印信息出现“bad address”错误,而用puts打印不会,为啥?代码如下:
#include        <stdio.h>
#include        <stdlib.h>
#include        <unistd.h>
#include        <sys/types.h>
#include         <netinet/in.h>
#include        <sys/socket.h>
#include        <errno.h>
#include        <string.h>

#define oops(s,x)  { perror(s); exit(x); }
#define        BUFLEN         128
#define MY_PORT 80


int main(int ac, char *av[])
{
        int                            sock;        /* receive requests here        */
        int                        fd;
        struct sockaddr_in        myaddr;        /* get caller id here                */
        socklen_t                 addrlen;
        char                        buf[BUFLEN];
        int                        msglen;
        int                         ret;
        if ( ac != 1 ){
                fprintf(stderr,"usage: %s sockname\n", *av);
                exit(1);
        }

        /* ----------------*/
        /*  get a socket   */
        /* ----------------*/
        sock = socket(PF_INET, SOCK_DGRAM, 0);        /* note PF_UNIX  */
        if ( sock == -1 )
                oops("socket",2);
        printf("have a socket: %d\n", sock);

        /* ------------------------------------*/
        /* make an address for server          */
        /* ------------------------------------*/
        myaddr.sin_family = AF_INET;
        inet_pton(AF_INET,"127.0.0.1",&myaddr.sin_addr);
        myaddr.sin_port=htons(MY_PORT);                /* note AF_UNIX                */


        /* ----------------- *
         * connect to server *
         * ----------------- */

        ret = sendto(sock,NULL,0,0,(struct sockaddr*)&myaddr,sizeof(struct sockaddr_in));

        if ( ret != -1 ){
                printf("Connectd at sock %d\n", sock);
                if(recvfrom(sock,buf,msglen,0,NULL,NULL)==-1)
                        oops("recvfrom",4);
                //after executing the system call write,error "write bad address" would be printed.However,while using puts in replace of write,not any error would be printed.why?
                //if (write(1, buf, msglen)==-1)
                //        oops("write",9);
                puts(buf);
        }
        else oops("sendto",3);
        return 0;
}

论坛徽章:
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
发表于 2014-02-28 23:53 |显示全部楼层
不能直接write标准输出,,好像还要fcntl设置标准输出后,才能write

论坛徽章:
2
技术图书徽章
日期:2014-02-24 10:31:04技术图书徽章
日期:2014-03-20 16:11:43
发表于 2014-03-02 20:55 |显示全部楼层
错误在msglen只是声明了,还没有初始化,

论坛徽章:
2
技术图书徽章
日期:2014-02-24 10:31:04技术图书徽章
日期:2014-03-20 16:11:43
发表于 2014-03-02 20:56 |显示全部楼层
puts不需要buf的长度。所以不出错

论坛徽章:
2
技术图书徽章
日期:2014-02-24 10:31:04技术图书徽章
日期:2014-03-20 16:11:43
发表于 2014-03-02 20:57 |显示全部楼层
/* note PF_UNIX  */

这注释是什么意思?

论坛徽章:
2
技术图书徽章
日期:2014-02-24 10:31:04技术图书徽章
日期:2014-03-20 16:11:43
发表于 2014-03-02 20:59 |显示全部楼层
printf("Connectd at sock %d\n", sock);

UDP是无连接的,你说这句话是不是错了。

论坛徽章:
2
技术图书徽章
日期:2014-02-24 10:31:04技术图书徽章
日期:2014-03-20 16:11:43
发表于 2014-03-02 21:02 |显示全部楼层
/* note PF_UNIX  */

你明明用的是UDP套接字,为什么要注释成UNIX套接字呢?

论坛徽章:
1
巨蟹座
日期:2014-03-18 23:44:30
发表于 2014-03-03 13:21 |显示全部楼层
3~4 楼正解啊,对于write 的长度man有如下说明
If  count  is  zero  and  fd refers to a regular file, then write() may
       return a failure status if one of the errors below is detected.  If  no
       errors  are  detected,  0  will  be  returned without causing any other
       effect.  If count is zero and fd refers to a file other than a  regular
       file, the results are not specified.
而你在传参的时候 msglen 是一个随机值
结果 你懂的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP