你能用c语言写出以下要求的代码吗 系统分为服务端和客户端两个程序,服务端和客户端可在局域网内网络通信; 服务端负责报名数据维护管理(数据保存在服务端),报名数据具体包含的内容自行设计确定; 客户端负责与用户交互、向服务端提交用户的操作(输入)、接收服务端发来的数据并呈现(显示)给用户。 用户通过客户端输入报名具体信息并提交给服务端。服务端收到报名信息并核查无误(比如报名信息不可与已有报名信息重复)后,向客户端发回一个唯一的(不重复的)“报名号”即确认报名成功; 用户可通过客户端向服务端发送请求,要求服务端查找并发回自己的报名信息供查看; 服务端程序退出之前自动将当前报名数据导出成文本文件保存于.exe程序所在目录下; 报名数据存储采用链表或数组均可,建议用链表。
时间: 2024-03-24 16:41:19 浏览: 63
以下是一个基于C语言的简单实现,包括服务端和客户端两个程序。
服务端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX_NAME_LEN 50
#define MAX_ID_LEN 10
#define MAX_DATA_LEN 1000
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
struct student *next;
} Student;
Student *head = NULL;
int num_of_students = 0;
int add_student(char *name, char *id) {
Student *ptr = head;
while (ptr != NULL) {
if (strcmp(ptr->id, id) == 0) {
return -1; // ID already exists
}
ptr = ptr->next;
}
Student *new_student = (Student *) malloc(sizeof(Student));
strcpy(new_student->name, name);
strcpy(new_student->id, id);
new_student->next = NULL;
if (head == NULL) {
head = new_student;
} else {
ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = new_student;
}
num_of_students++;
return num_of_students;
}
char *get_student_info(char *id) {
Student *ptr = head;
while (ptr != NULL) {
if (strcmp(ptr->id, id) == 0) {
char *info = (char *) malloc(MAX_DATA_LEN);
sprintf(info, "Name: %s\nID: %s\n", ptr->name, ptr->id);
return info;
}
ptr = ptr->next;
}
return NULL; // ID not found
}
void save_data() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Failed to save data.\n");
return;
}
Student *ptr = head;
while (ptr != NULL) {
fprintf(fp, "%s,%s\n", ptr->name, ptr->id);
ptr = ptr->next;
}
fclose(fp);
}
int main() {
int server_fd, new_socket, val_read;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[MAX_DATA_LEN] = {0};
char response[MAX_DATA_LEN] = {0};
// Create server socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed.");
exit(EXIT_FAILURE);
}
// Set socket options
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("Setsockopt failed.");
exit(EXIT_FAILURE);
}
// Bind socket to address and port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("Bind failed.");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("Listen failed.");
exit(EXIT_FAILURE);
}
while (1) {
printf("Waiting for connection...\n");
// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *) &address, (socklen_t *) &addrlen)) < 0) {
perror("Accept failed.");
exit(EXIT_FAILURE);
}
printf("Connection accepted.\n");
// Receive data from client
val_read = read(new_socket, buffer, MAX_DATA_LEN);
if (val_read < 0) {
perror("Read failed.");
exit(EXIT_FAILURE);
}
printf("Received data: %s\n", buffer);
// Parse client request and respond accordingly
char *token = strtok(buffer, ",");
if (strcmp(token, "add") == 0) {
char *name = strtok(NULL, ",");
char *id = strtok(NULL, ",");
int result = add_student(name, id);
if (result == -1) {
strcpy(response, "Error: ID already exists.");
} else {
sprintf(response, "Success: Your unique ID is %d.", result);
}
} else if (strcmp(token, "get") == 0) {
char *id = strtok(NULL, ",");
char *info = get_student_info(id);
if (info == NULL) {
strcpy(response, "Error: ID not found.");
} else {
strcpy(response, info);
free(info);
}
} else {
strcpy(response, "Error: Invalid request.");
}
// Send response to client
send(new_socket, response, strlen(response), 0);
printf("Response sent: %s\n", response);
close(new_socket);
}
save_data();
return 0;
}
```
客户端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX_DATA_LEN 1000
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[MAX_DATA_LEN] = {0};
char request[MAX_DATA_LEN] = {0};
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\nSocket creation error.\n");
return -1;
}
// Set server address and port
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address / address not supported.\n");
return -1;
}
// Connect to server
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection failed.\n");
return -1;
}
// Send request to server and receive response
printf("Enter command: ");
fgets(request, MAX_DATA_LEN, stdin);
send(sock, request, strlen(request), 0);
valread = read(sock, buffer, MAX_DATA_LEN);
printf("%s\n", buffer);
return 0;
}
```
注意:以上代码仅为简单实现,还有很多细节需要考虑和完善。
阅读全文