为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

jelasin 的学生作业:

#include #include #include #include #include #include #include #include #include #include #include #define MAX_EVENTS 1024 #define BUFFER_SIZE 4096 #define WORKER_NUM 4 int create_udp_socket(int port) { int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket creation failed"); exit(EXIT_FAILURE); } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("bind failed"); close(sockfd); exit(EXIT_FAILURE); } return sockfd; } void worker_process(int sockfd) { int epoll_fd = epoll_create1(0); if (epoll_fd < 0) { perror("epoll_create1 failed"); exit(EXIT_FAILURE); } struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = sockfd; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &ev) < 0) { perror("epoll_ctl failed"); close(epoll_fd); exit(EXIT_FAILURE); } struct epoll_event events[MAX_EVENTS]; char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); while (1) { int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if (nfds < 0) { if (errno == EINTR) continue; perror("epoll_wait failed"); break; } for (int i = 0; i < nfds; i++) { if (events[i].data.fd == sockfd) { ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len); if (recv_len < 0) { perror("recvfrom failed"); continue; } printf("Worker[%d] received %zd bytes from %s:%d\n", getpid(), recv_len, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); // Echo back sendto(sockfd, buffer, recv_len, 0, (struct sockaddr*)&client_addr, addr_len); } } } close(epoll_fd); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } int port = atoi(argv[1]); int sockfd = create_udp_socket(port); signal(SIGCHLD, SIG_IGN); for (int i = 0; i < WORKER_NUM; i++) { pid_t pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } else if (pid == 0) { // Child process worker_process(sockfd); exit(EXIT_SUCCESS); } } printf("Server started with %d workers on port %d\n", WORKER_NUM, port); printf("Master process PID: %d\n", getpid()); while (1) { sleep(1); } close(sockfd); return 0; }

得分 100
讨论题

Yam8632482 的学生作业:

seqstack.h #ifndef __SEQSTACK_H__ #define __SEQSTACK_H__ #include #include #include #define MAXSIZE 20 typedef char data_t; typedef struct { data_t data[MAXSIZE]; int top; } seqstack_t; extern seqstack_t *seqstack_create(); extern void seqstack_push(seqstack_t *s, data_t x); extern data_t seqstack_pop(seqstack_t *s); extern data_t seqstack_gettop(seqstack_t *s); extern int is_full_seqstack(seqstack_t *s); extern int is_empty_seqstack(seqstack_t *s); #endif //__SEQSTACK_H__ seqstack.c #include "seqstack.h" seqstack_t *seqstack_create() { seqstack_t *s = (seqstack_t *) malloc(sizeof(seqstack_t)); if (NULL == s) { printf("molloc fail!\n"); return NULL; } memset(s, 0, sizeof(seqstack_t)); s->top = -1; return s; } int is_empty_seqstack(seqstack_t *s) { return s->top==-1; } int is_full_seqstack(seqstack_t *s) { return s->top==MAXSIZE-1; } void seqstack_push(seqstack_t *s, data_t x) { if (is_full_seqstack(s)) { printf("stack is full!\n"); return; } s->data[++s->top] = x; } data_t seqstack_pop(seqstack_t *s) { if (is_empty_seqstack(s)) { printf("stack is empty!\n"); return -1; } return s->data[s->top--]; } data_t seqstack_gettop(seqstack_t *s) { if (is_empty_seqstack(s)) { printf("stack is empty!\n"); return -1; } return s->data[s->top]; } main.c #include "seqstack.h" int main(void) { seqstack_t *s = seqstack_create(); data_t data[] = {'a','n','i','h','c',' ','e','v','o','l',' ','I'}; for (int i = 0; i < sizeof(data)/sizeof(data[0]); i++) { seqstack_push(s, data[i]); } while (!is_empty_seqstack(s)) { printf("%d:%c \n",s->top, seqstack_pop(s)); } return 0; }

得分 100
学习任务

jelasin 的学生作业:

#include #include #include #include #include #include #include #include #define BUFFER_SIZE 1024 #define LOG_FILE "log.txt" #define RECV_IP "127.0.0.1" #define RECV_PORT 9090 void create_log_file() { FILE *fp = fopen(LOG_FILE, "w"); if (!fp) { perror("fopen failed"); exit(EXIT_FAILURE); } const char *test_data[] = { "==== UDP File Transfer Test ====\n", "Line 1: Hello UDP World!\n", "Line 2: This is a test message\n", "Line 3: 1234567890\n", "==== End of Transmission ====\n" }; for (size_t i = 0; i < sizeof(test_data)/sizeof(test_data[0]); i++) { fputs(test_data[i], fp); } fclose(fp); printf("[Sender] Created %s with test data\n", LOG_FILE); } char *read_log_file(size_t *file_size) { FILE *fp = fopen(LOG_FILE, "r"); if (!fp) { perror("fopen failed"); return NULL; } fseek(fp, 0, SEEK_END); *file_size = ftell(fp); fseek(fp, 0, SEEK_SET); char *buffer = malloc(*file_size + 1); if (!buffer) { perror("malloc failed"); fclose(fp); return NULL; } size_t read_size = fread(buffer, 1, *file_size, fp); if (read_size != *file_size) { perror("fread failed"); free(buffer); fclose(fp); return NULL; } buffer[*file_size] = '\0'; fclose(fp); return buffer; } int main() { // 1. 创建日志文件 create_log_file(); // 2. 读取文件内容 size_t file_size; char *file_content = read_log_file(&file_size); if (!file_content) { fprintf(stderr, "[Sender] Failed to read log file\n"); return EXIT_FAILURE; } printf("[Sender] File content (%zu bytes):\n%s\n", file_size, file_content); // 3. 创建UDP socket int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("[Sender] socket creation failed"); free(file_content); return EXIT_FAILURE; } // 设置接收方地址 struct sockaddr_in recv_addr = { .sin_family = AF_INET, .sin_port = htons(RECV_PORT) }; if (inet_pton(AF_INET, RECV_IP, &recv_addr.sin_addr)

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号