Linux环境下通过网络编程实现一个C/S模式的员工管理系统,服务器端用多进程或多线程模型支持多个客户端同时连接、操作。具体要求如下: (1)员工信息包括三个字段:姓名,年龄,手机号,用结构体表示; (2)服务器功能: 添加新员工——接受客户端发送来的新员工结构体数据,追加到数据文件emp.db中; 获取员工列表——把emp.db中的每个员工结构体数据读出发回客户端; (3)客户端功能: 添加新员工——从终端读取新员工信息并构造为结构体变量,发到服务器请求添加新员工; 显示员工列表——向服务器请求员工列表,接收后显示到终端。 (4)客户端和服务器之间的协议应事先设计,如服务器返回给客户端的各类报告信息、错误信息都应事先统一设计好编号,
时间: 2023-05-25 12:02:51 浏览: 116
并在程序中进行处理。
以下是参考代码:
服务器端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 12345
#define MAXLINE 1024
#define MAXEMP 100
struct employee {
char name[20];
int age;
char phone[20];
};
struct protocol {
char type;
int length;
};
int listenfd, connfd[MAXEMP], empnum = 0;
void addemployee(struct employee emp) {
FILE *fp = fopen("emp.db", "a");
fprintf(fp, "%s %d %s\n", emp.name, emp.age, emp.phone);
fclose(fp);
}
void getemployeelist(int conn) {
FILE *fp = fopen("emp.db", "r");
char line[MAXLINE];
while (fgets(line, MAXLINE, fp) != NULL) {
struct employee emp;
sscanf(line, "%s %d %s", emp.name, &emp.age, emp.phone);
write(conn, &emp, sizeof(emp));
}
fclose(fp);
}
void handle(int conn) {
struct protocol pro;
read(conn, &pro, sizeof(pro));
if (pro.type == 'a') { // add employee
struct employee emp;
read(conn, &emp, pro.length);
addemployee(emp);
} else if (pro.type == 'g') { // get employee list
getemployeelist(conn);
}
}
void *thread(void *arg) {
int conn = (int)arg;
handle(conn);
close(conn);
pthread_exit(NULL);
}
int main() {
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, MAXEMP);
while (1) {
clilen = sizeof(cliaddr);
connfd[empnum++] = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
pthread_t tid;
pthread_create(&tid, NULL, thread, (void *)connfd[empnum-1]);
}
}
客户端:
#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 PORT 12345
#define MAXEMP 100
#define MAXLINE 1024
struct employee {
char name[20];
int age;
char phone[20];
};
struct protocol {
char type;
int length;
};
int sockfd;
void addemployee() {
struct employee emp;
printf("Input name: ");
scanf("%s", emp.name);
printf("Input age: ");
scanf("%d", &emp.age);
printf("Input phone: ");
scanf("%s", emp.phone);
struct protocol pro;
pro.type = 'a';
pro.length = sizeof(emp);
write(sockfd, &pro, sizeof(pro));
write(sockfd, &emp, sizeof(emp));
}
void getemployeelist() {
struct protocol pro;
pro.type = 'g';
pro.length = 0;
write(sockfd, &pro, sizeof(pro));
struct employee emp;
int num = 0;
while (read(sockfd, &emp, sizeof(emp)) > 0) {
printf("%d: %s %d %s\n", ++num, emp.name, emp.age, emp.phone);
}
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: %s <IPaddress>\n", argv[0]);
exit(1);
}
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
printf("1. Add new employee\n2. Get employee list\n3. Exit\n");
int choice;
while (1) {
printf("Please input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addemployee();
break;
case 2:
getemployeelist();
break;
case 3:
close(sockfd);
exit(0);
default:
printf("Invalid choice\n");
}
}
}
阅读全文