实现阿里云服务器上的文字聊天程序以及C语言写的进程间通信(IPC)程序

实现阿里云服务器上的文字聊天程序以及C语言写的进程间通信(IPC)程序

1. 基于 Linux 中的管道进行进程间通信

我们首先使用管道进行进程间通信,这对于简单的聊天程序来说是一个比较简单且实用的方法。

步骤:
  1. 创建管道:管道用于两个进程间的单向通信。我们需要用 pipe() 函数来创建管道。
  2. 进程间通信:父进程和子进程通过管道交换信息。
  3. 实现聊天功能:父进程可以通过管道向子进程发送消息,子进程可以通过管道接收并显示消息,反之亦然。
代码实现:
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX_MESSAGE_LENGTH 256

void chat_process(int pipe_fd[2]) {
    char message[MAX_MESSAGE_LENGTH];
    close(pipe_fd[1]);  // 关闭写端,父进程将消息写入管道,子进程从管道中读取
    
    while (1) {
        read(pipe_fd[0], message, MAX_MESSAGE_LENGTH);
        if (strcmp(message, "exit\n") == 0) {
            break;
        }
        printf("Received message: %s", message);
    }
    
    close(pipe_fd[0]);  // 关闭读端
}

void chat_parent(int pipe_fd[2]) {
    char message[MAX_MESSAGE_LENGTH];
    close(pipe_fd[0]);  // 关闭读端
    
    while (1) {
        printf("Enter message to send: ");
        fgets(message, MAX_MESSAGE_LENGTH, stdin);
        
        if (strcmp(message, "exit\n") == 0) {
            write(pipe_fd[1], message, strlen(message) + 1);
            break;
        }
        
        write(pipe_fd[1], message, strlen(message) + 1);
    }
    
    close(pipe_fd[1]);  // 关闭写端
}

int main() {
    pid_t pid;
    int pipe_fd[2];
    
    if (pipe(pipe_fd) == -1) {
        perror("pipe");
        exit(1);
    }

    pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {
        // 子进程执行chat_process
        chat_process(pipe_fd);
    } else {
        // 父进程执行chat_parent
        chat_parent(pipe_fd);
        wait(NULL);  // 等待子进程结束
    }
    
    return 0;
}
解释:
  • 该程序使用管道(pipe_fd)进行进程间通信。
  • 父进程通过管道写入消息,子进程通过管道读取消息并显示。
  • 当输入"exit"时,父进程和子进程都会退出聊天程序。
  • 父进程和子进程之间通过管道进行简单的文字通信。

2. 在阿里云服务器上进行聊天

  1. 登录阿里云服务器

    • 你需要通过 SSH 登录到阿里云的服务器。使用终端命令:

      ssh username@your_server_ip
      
  2. 编译和运行程序

    • 先将上面的 C 语言代码保存到文件中(例如 chat.c)。

    • 编译 C 程序:

      gcc -o chat chat.c
      
    • 运行编译后的程序:

      ./chat
      
  3. 多个用户之间的聊天

    • 你可以在阿里云上启动多个终端,并分别运行不同的进程,通过 IPC 机制(例如管道)进行进程间的通信。
    • 每个终端都运行上述程序并模拟聊天,虽然它们是不同的进程,但通过管道可以实现相互间的消息交换。

3. 使用消息队列(另一种 IPC 方式)

如果你想尝试另一种进程间通信的方式,可以使用消息队列(Message Queue)。这是 Linux 中另一种常见的 IPC 方法。

代码实现使用消息队列:
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX_MESSAGE_LENGTH 256
#define MSG_KEY 1234

struct msg_buffer {
    long msg_type;
    char msg_text[MAX_MESSAGE_LENGTH];
};

void chat_process(int msgid) {
    struct msg_buffer message;
    while (1) {
        msgrcv(msgid, &message, sizeof(message), 1, 0);
        printf("Received message: %s", message.msg_text);
        if (strcmp(message.msg_text, "exit\n") == 0) {
            break;
        }
    }
}

void chat_parent(int msgid) {
    struct msg_buffer message;
    while (1) {
        printf("Enter message to send: ");
        fgets(message.msg_text, MAX_MESSAGE_LENGTH, stdin);
        message.msg_type = 1;
        msgsnd(msgid, &message, sizeof(message), 0);
        
        if (strcmp(message.msg_text, "exit\n") == 0) {
            break;
        }
    }
}

int main() {
    pid_t pid;
    int msgid;
    
    msgid = msgget(MSG_KEY, 0666 | IPC_CREAT);
    if (msgid == -1) {
        perror("msgget");
        exit(1);
    }

    pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {
        // 子进程执行chat_process
        chat_process(msgid);
    } else {
        // 父进程执行chat_parent
        chat_parent(msgid);
        wait(NULL);  // 等待子进程结束
    }

    msgctl(msgid, IPC_RMID, NULL);  // 删除消息队列
    return 0;
}
解释:
  • 消息队列msgget用于创建一个消息队列。
  • 父进程通过msgsnd发送消息,子进程通过msgrcv接收消息。
  • 当输入"exit"时,程序会退出。

总结:

  1. 使用管道和消息队列都是 Linux 中常见的进程间通信方法。根据需要选择适合的方式。
  2. 可以在阿里云服务器上运行这些程序来模拟进程间的聊天。
  3. 在此基础上,可以进一步扩展聊天程序,比如加入多用户支持、网络通信等功能。

你可能感兴趣的:(服务器,c语言,运维)