免费注册 查看新帖 |

Chinaunix

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

大家帮忙看看这write函数和send函数到底怎么了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-16 15:59 |只看该作者 |倒序浏览
void Recieve_com();
void Send_com();
void Recieve_port();
void Send_port();
void InitCom();
void InitPort();
void close_com_and_port();

int tcpipok =0;
struct sockaddr_in serv;
int com_fd;
struct sockaddr_in serv1;
int port_fd;
int ret;
int port_com = 10002;
int port_ctrl = 10000;
unsigned char buff11[50] ;
unsigned char buf12[50];
int len =0;
int i =0;
char cmd[6][16]={
  {"0629600\n"},
  {"19i 5"},
  {"19o 0 00"},
};
char ipname[18][16]={
  {"192.168.255.1"},
  {"192.168.255.2"},
};
unsigned short RcvCount;
unsigned char SendBuf2[50];
unsigned short SendSize2;
main(argc,argv)
int argc;
char *argv[];
{
  unsigned short i;
  memset(&serv,0,sizeof(serv));
  memset(&serv1,0,sizeof(serv1));
  SendBuf2[0]='#';
  SendBuf2[1]=0x01;
  SendBuf2[2]=0x00;
  SendBuf2[3]='#';
  SendBuf2[4]=0x01;
  SendBuf2[5]=0x00;         
  SendSize2=6;
  RcvCount=10;         
  main_loop();
}

main_loop()
{
   int j,k;
   int error_flag,error_num;
   while(1)
   {
     InitCom(1);               //初始化两个端口
     if (tcpipok ==1)            //连接成功
     {
       if(error_flag==1)           
       {
         printf("tongxunhuifu\n");     
       }
       error_flag=0;error_num=0;
       ms_sleep(150);
       Send_com(SendBuf2,SendSize2);      //向10002端口发送数据
       ms_sleep(150);
       Recieve_com();                                  //收数据
       close_com_and_port();
     }
     else
     {  
       if(error_flag==0) error_num++;
       if(error_num==5&&error_flag==0)
       {
         error_flag=1;
       }  
     }
   }
}
void Recieve_com()
{
       if((ret=recv(com_fd,buf12,2,MSG_DONTWAIT))<0) {
                perror("read error");
       } else {
                printf("  read %d bytes:  ",ret);
                for (i=0; i<ret; i++)
                    printf("%x ",buf12);   
                printf("\n");
      }     
}
void alarm_handler()
{
fprintf(stderr,"Send Alarm TimeOut\n");
}
void Send_com(str,MsgSize)
unsigned char str[];
unsigned short MsgSize;
{
     signal(SIGALRM,alarm_handler);
     alarm(2);
    //len=write(com_fd, str,MsgSize);  //出错程序退出的地方
     len=send(com_fd,str,MsgSize,0);   //出错程序退出的地方  
     
     alarm(0);
     signal(SIGALRM,SIG_DFL);
     if (len<0)
       printf("comtest send fail\n");
}

void Recieve_port()
{
       ms_sleep(100);
       if((len=recv(port_fd,buff11,10,MSG_DONTWAIT))<0){
           perror("port error");
       }else{
           printf("port_read %d byte:  ",len);
       }
}

void Send_port(int cmdno)
{
       len =0;
       printf("SEND_port : cmdno =%d \n" , cmdno);
       memset(buff11,0,sizeof(buff11));
       len += sprintf(buff11+len, cmd[cmdno]);
       //len = write(port_fd,buff11,len);               
       len=send(port_fd,buff11,len,0);                  
       printf("len=%d\n",len);
       if(cmdno==1&&len==5) Recieve_port();
       else{
            if(len==8&&cmdno==0||cmdno==2||cmdno==3||cmdno==4||cmdno==5)
            Recieve_port();
       }
}

void InitCom(int ipno)
{
   int i;
   com_fd=socket(AF_INET,SOCK_STREAM,0);
   serv.sin_family=AF_INET;
   serv.sin_addr.s_addr=inet_addr(ipname[ipno]);
   serv.sin_port=htons(port_com);
   ret=connect(com_fd,(struct sockaddr *)&serv,sizeof(serv));
   if(ret<0)
   {
     tcpipok = 0;
   }
   else tcpipok=1;
   InitPort(ipno);
   if (tcpipok ==1){
   Send_port(0);
   } else close_com_and_port();
}
void InitPort(int ipno)
{
   
   port_fd=socket(AF_INET,SOCK_STREAM,0);
   serv1.sin_family=AF_INET;
   serv1.sin_addr.s_addr=inet_addr(ipname[ipno]);
   serv1.sin_port=htons(port_ctrl);
   ret=connect(port_fd,(struct sockaddr *)&serv1,sizeof(serv1));
   if(ret<0)
   {
            tcpipok = 0;
   } else tcpipok=1;
}
void close_com_and_port()
{
       shutdown (com_fd,2);
       close (com_fd);
       shutdown(port_fd,2);
       close(port_fd);
}

平时通讯都正常,当我把网络断掉后再把网络连上有点程序在write()函数那退出,(本程序的红色部分)系统报broken pipe。但是不是每次都这样,大部分会自动恢复,只是偶尔会退出,小弟也看不懂是为什么,希望大家能帮帮忙

[ 本帖最后由 zhoujiechong 于 2007-3-20 16:03 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-03-16 19:57 |只看该作者
你需要对write的返回值做判断..看看出错的原因.

论坛徽章:
0
3 [报告]
发表于 2007-03-19 08:41 |只看该作者
查不到write()的返回值,程序一运行到write(com_fd,str,Msgsize);就退出了,系统报Broken pipe,看不懂它在说什么,谁能帮忙解释一下这句英文,我查了金山词霸是说“坏管道”

论坛徽章:
0
4 [报告]
发表于 2007-03-20 00:14 |只看该作者
客户端(client),
while(1)
{
    connect();
    if()
         ....
    else
        ......
}

关于connect,那即使连接失败了,程序也没有退出啊.
没看懂

论坛徽章:
0
5 [报告]
发表于 2007-03-20 15:57 |只看该作者
是啊,连接失败是不会退出的,只有连接成攻后再往端口上写东西才会有退出的情况,会退出的是程序的红色那一行,昨天又试了好几次,有一次程序又退出了,其它时候又好好的,真是气死我了。还有谁能告诉我"broken pipe"这个是什么意思
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP