linux编程,利用有名管道编写简单的聊天程序,聊天双方在线才能说话,一方说话 后需另一方应答才能继续说话,即一来一往的聊天模式,如果输入quit则退出聊 天程序。
时间: 2024-03-06 11:48:57 浏览: 40
以下是一个利用有名管道实现简单聊天程序的示例代码:
server.c:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#define FIFO_NAME "/tmp/chat_fifo"
int main()
{
int fd;
char input[1024];
char output[1024];
// 创建有名管道
if (mkfifo(FIFO_NAME, 0666) == -1) {
perror("mkfifo error");
exit(1);
}
// 打开有名管道
if ((fd = open(FIFO_NAME, O_RDONLY)) == -1) {
perror("fifo open error");
exit(1);
}
// 循环读取客户端输入,并回复
while (1) {
memset(input, 0, sizeof(input));
memset(output, 0, sizeof(output));
if (read(fd, input, sizeof(input)) > 0) {
printf("Client: %s\n", input);
if (strcmp(input, "quit") == 0) {
printf("Chat finished.\n");
break;
}
printf("Server: ");
fgets(output, sizeof(output), stdin);
output[strlen(output)-1] = '\0';
if (strcmp(output, "quit") == 0) {
printf("Chat finished.\n");
break;
}
printf("Waiting for client's response...\n");
// 打开有名管道,向客户端发送消息
int clientfd;
if ((clientfd = open(FIFO_NAME, O_WRONLY)) == -1) {
perror("fifo open error");
exit(1);
}
write(clientfd, output, strlen(output));
close(clientfd);
}
}
close(fd);
unlink(FIFO_NAME);
exit(0);
}
```
client.c:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#define FIFO_NAME "/tmp/chat_fifo"
int main()
{
int fd;
char input[1024];
char output[1024];
// 打开有名管道
if ((fd = open(FIFO_NAME, O_WRONLY)) == -1) {
perror("fifo open error");
exit(1);
}
// 循环读取用户输入,并发送到服务器
while (1) {
printf("Client: ");
fgets(output, sizeof(output), stdin);
output[strlen(output)-1] = '\0';
if (strcmp(output, "quit") == 0) {
printf("Chat finished.\n");
break;
}
// 向服务器发送消息
write(fd, output, strlen(output));
printf("Waiting for server's response...\n");
// 打开有名管道,等待服务器回复
int serverfd;
if ((serverfd = open(FIFO_NAME, O_RDONLY)) == -1) {
perror("fifo open error");
exit(1);
}
memset(input, 0, sizeof(input));
read(serverfd, input, sizeof(input));
printf("Server: %s\n", input);
close(serverfd);
}
close(fd);
exit(0);
}
```
该程序通过有名管道实现了简单的聊天功能,其中server.c为服务器端程序,client.c为客户端程序。在聊天过程中,一方说话后需等待另一方应答才能继续说话,即一来一往的聊天模式。如果输入quit则退出聊天程序。程序使用mkfifo函数创建了一个有名管道,客户端和服务器端都通过open函数打开同一个有名管道进行通信。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)