Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
我想寫一段udp server, 使用fork, 並且parent process與child process都要寫log檔
- User
我想寫一段udp server, 使用fork, 並且parent process與child process都要寫log檔
- Assistant
"我想寫一段udp server, 使用fork, 並且parent process與child process都要寫log檔"
CODAI:
scss#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define BUF_SIZE 1024 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in addr; char buffer[BUF_SIZE]; int len, ret; // Create a socket sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket()"); exit(EXIT_FAILURE); } // Set the socket address and port number memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(1234); addr.sin_addr.s_addr = htonl(INADDR_ANY); // Bind the socket if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { perror("bind()"); exit(EXIT_FAILURE); } // Fork a child process pid_t pid = fork(); if (pid == -1) { perror("fork()"); exit(EXIT_FAILURE); } // In parent process, log the incoming message if (pid > 0) { while (1) { memset(buffer, 0, BUF_SIZE); len = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL); if (len == -1) { perror("recvfrom()"); exit(EXIT_FAILURE); } // Log the incoming message FILE *fp = fopen("parent.log", "a"); if (fp == NULL) { perror("fopen()"); exit(EXIT_FAILURE); } fprintf(fp, "Received message: %s\n", buffer); fclose(fp); } } // In child process, log a message and send it back to the client else { while (1) { memset(buffer, 0, BUF_SIZE); len = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL); if (len == -1) { perror("recvfrom()"); exit(EXIT_FAILURE); } // Log the incoming message FILE *fp = fopen("child.log", "a"); if (fp == NULL) { perror("fopen()"); exit(EXIT_FAILURE); } fprintf(fp, "Received message: %s\n", buffer); fclose(fp); // Send the message back to the client ret = sendto(sockfd, buffer, len, 0, (struct sockaddr *)&addr, sizeof(addr)); if (ret == -1) { perror("sendto()"); exit(EXIT_FAILURE); } } } return 0; } - User
請將以上code改寫成C++版本
- Assistant
Sure, here's the C++ version of the code:
c - User
請將以上code加入semaphore機制
- Assistant