首页 > 解决方案 > Linux消息发送方和接收方执行错误(重复错误)

问题描述

我在 Linux 工作过。我只想在 Sanders 发送时在接收器上打印消息。但是,接收方继续输出相同的消息。

我确实想在接收者中获取消息,只有当发送者发送消息时。这段代码是我的代码。我尝试有条件的,但这并不容易。帮助

//Sender
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
  int fd; 
  char message[100];
  printf("Enter \"exit\"  to stop: \n");

  while(1)
  {
    fd=open("/home/control/work/io_dev",O_RDWR); //open file
    if (fd ==-1) {
        printf("Device open error"); 
        exit(1);
    }
    sleep(1);
    write(fd,message,sizeof(message));
    printf("Sender>>"); 
    fgets(message,sizeof(message),stdin);// input message
    write(fd,message, sizeof(message));//write on file
    if(strcmp(message,"exit\n")==0)
        break;
    write(fd,message,sizeof(message));

    close(fd); //file close
  }
}
//Receiver
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main()
{
  int fd;
  char message[100];

  while(1){
    sleep(1);
    fd=open("/home/control/work/io_dev",O_RDWR|O_CREAT); // open or create file
    if (fd == -1) { //when file open error occurs
      printf("Device open error");
      exit(1);
    }
    read(fd, message, sizeof(message));//read file
    sleep(1);//wait
    if(strcmp(message,"exit\n") ==0)//loop off
        break;
    if(message[0]!='\0'){
    printf("Received>> %s",message);//print
    }
    write(fd,message,sizeof(message));
    close(fd);//file close
    sleep(3);
  }
}

标签: clinux

解决方案


以下建议的代码,用于receiver

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查错误
  4. 避免必须不断打开/关闭 I/O 设备
  5. 回显实际收到的内容,而不是整个接收缓冲区,其中大部分可能包含垃圾
  6. 避免在代码中使用“魔术”数字

现在,建议的代码:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#define MAX_MSG_LEN 100

int main( void )
{
    int fd;
    char message[ MAX_MSG_LEN ];

    fd=open("/home/control/work/io_dev",O_RDWR|O_CREAT, 0666 ); // open or create file
    if (fd == -1) 
    { //when file open error occurs
         printf("Device open error");
         exit(1);
    }

    while(1)
    {   
        ssize_t bytesRead = read(fd, message, sizeof(message));//read file
        if( bytesRead <0 )
        {
            // handle error and exit
            perror( "read failed" );
            close( fd );
            break;
        }

        if( bytesRead == 0 )
        { // sender closed the connection
            // clean up and exit
            close( fd );
            break;
        }

        message[ bytesRead ] = '\0';

        if(strcmp(message,"exit\n") ==0)//loop off
            break;

        printf("Received>> %s",message);//print

        if( write(fd,message,strlen(message)) != strlen( message ) )
        {
            // handle incomplete message write
        }
    }
    close(fd);//file close
}

但是,如果您尝试使用多个读取器和写入器来“调整”读/写操作;然后强烈建议使用互斥锁来避免任何“竞争”条件,甚至可能mkfifo()用于创建文件。记得(在退出程序之前)销毁先进先出。

如果决定使用 FIFO,那么一定要调用unlink( filename );,这样程序退出时文件将被销毁

如果您想在 while() 循环中保持重复的打开/关闭,请使用 O_EXCL 而不是 O_CREAT,这样单独的程序就不会相互影响。


推荐阅读