Skip to main content
 首页 » 编程设计

c之使用命名管道在 C 中实现聊天

2023年11月22日90webabcd

我使用 FIFO 命名管道为客户端和服务器创建了两个管道。然后我尝试执行客户端和服务器之间的通信。当从客户端向服务器发送消息时通信有效,反之亦然。请帮忙。

这是三个代码:

fifo.c(创建管道)

#include<stdio.h>  
#include<fcntl.h> 
#include<stdlib.h> 
main() 
{ 
int file1,file2; 
int fd; 
char str[256]; 
char temp[4]="how"; 
char temp1[4]; 
file1 = mkfifo("fifo_server",0666);  
if(file1<0) { 
 printf("Unable to create a fifo"); 
 exit(-1); 
 } 
 
file2 = mkfifo("fifo_client",0666); 
 
if(file2<0) { 
 printf("Unable to create a fifo"); 
 exit(-1); 
 } 
printf("fifos server and child created successfuly\n "); 
} 

server.c(从客户端发送和接收)

#include<stdio.h>  
#include<fcntl.h> 
#include<stdlib.h> 
main() 
{ 
FILE *file1; 
int fifo_server,fifo_client; 
char *choice; 
char *buf; 
if(fork() == 0) 
    { 
        while(1) 
        {    
        fifo_server = open("fifo_server",O_RDWR); 
        if(fifo_server<1) { 
         printf("Error opening file"); 
         } 
 
        read(fifo_server,choice,sizeof(choice)); 
        printf("%s\n",choice); 
        close(fifo_server); 
        } 
        //sleep(3); 
 
 
    } 
else 
{ 
    while(1) 
    {    
        buf = malloc(10*sizeof(char));   
        scanf("%s",buf); 
 
        fifo_client = open("fifo_client",O_RDWR); 
 
        if(fifo_client<1) { 
         printf("Error opening file"); 
         } 
 
 
 
         write(fifo_client,buf,sizeof(buf));  
        close(fifo_client); 
 
    } 
} 
 
close(fifo_server); 
close(fifo_client); 
} 

client.c(从服务器发送和接收)

#include<stdio.h>  
#include<fcntl.h> 
#include<stdlib.h> 
main() 
{ 
 FILE *file1; 
 int fifo_server,fifo_client; 
 char str[256]; 
 char *buf; 
 char *choice; 
 printf("Welcome to chat\n\n"); 
if(fork() == 0) 
    {    
 
    while(1) 
    { 
         choice = malloc(10*sizeof(char)); 
         scanf("%s",choice); 
 
         fifo_server=open("fifo_server",O_RDWR); 
         if(fifo_server < 1) { 
          printf("Error in opening file"); 
          exit(-1); 
          } 
 
         write(fifo_server,choice,sizeof(choice)); 
 
         //sleep(10);    
    }     
 
 
    } 
 else{ 
    while(1) 
    { 
 
 
        fifo_client = open("fifo_client",O_RDWR); 
        if(fifo_client<1) { 
         printf("Error opening file"); 
         exit(1); 
         } 
 
        read(fifo_client,choice,sizeof(choice)); 
        printf("%s\n",choice); 
 
        /*       
        fifo_client=open("fifo_client",O_RDWR); 
 
         if(fifo_client < 0) { 
          printf("Error in opening file"); 
          exit(-1); 
          } 
 
         buf=malloc(10*sizeof(char)); 
         read (fifo_client,buf,sizeof(buf)); 
         printf("\n %s***\n",buf); 
        */ 
    } 
 
} 
 
close(fifo_server);  
close(fifo_client);   
} 

请您参考如下方法:

一些观察:

  • 您读取选择,但您从未为其分配内存。
  • 不要在每次迭代中使用 malloc() 和 open()/close(),而是使用静态字符缓冲区,如 buf[512] 用于从网络读取 () 和写入 ()以及(write() strnlen() 字节/字符)。

例如,代替:

char *buf; 
char *choice; 
printf("Welcome to chat\n\n"); 
if(fork() == 0) {    
  while(1) { 
    choice = malloc(10*sizeof(char)); 
    scanf("%s",choice); 
 
    fifo_server=open("fifo_server",O_RDWR); 
    if(fifo_server < 1) { 
      printf("Error in opening file"); 
      exit(-1); 
    } 
   write(fifo_server,choice,sizeof(choice)); 
} 

做类似的事情:

char buf[512]; 
if(fork() == 0) { 
  fifo_server = open("fifo_server", O_RDWR); 
  if(fifo_server < 1) { 
      printf("Error in opening file"); 
      exit(-1); 
  } 
  while(1) { 
    fgets(buf, 512, stdin); 
    write(fifo_server, buf, strnlen(buf, 512)); 
    /* here should be checks for number of bytes/chars written */ 
  } 
}