用C语言TCP套接字编写关于员工管理系统的输入、修改、浏览、查询、删除员工信息功能服务端的具体代码
时间: 2023-07-10 15:09:24 浏览: 117
以下是一个简单的员工管理系统服务端的代码示例,包括输入、修改、浏览、查询和删除员工信息的功能。本示例使用 TCP 套接字进行通信,使用了 MySQL 数据库进行数据存储。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 1024
#define PORT 8888
void error_handling(const char *message);
void handle_input(int client_sock, MYSQL *conn);
void handle_view(int client_sock, MYSQL *conn);
void handle_modify(int client_sock, MYSQL *conn);
void handle_search(int client_sock, MYSQL *conn);
void handle_delete(int client_sock, MYSQL *conn);
int main(int argc, char *argv[])
{
int serv_sock, client_sock;
struct sockaddr_in serv_addr, client_addr;
socklen_t client_addr_size;
// 初始化 MySQL 连接
MYSQL *conn = mysql_init(NULL);
if (!conn)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
// 连接到 MySQL 数据库
if (!mysql_real_connect(conn, "localhost", "root", "password", "employee", 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// 创建套接字
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == -1)
error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
// 绑定套接字
if (bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind() error");
// 监听套接字
if (listen(serv_sock, 5) == -1)
error_handling("listen() error");
// 接受客户端请求
while (1)
{
client_addr_size = sizeof(client_addr);
client_sock = accept(serv_sock, (struct sockaddr *)&client_addr, &client_addr_size);
if (client_sock == -1)
error_handling("accept() error");
printf("Connected client IP : %s\n", inet_ntoa(client_addr.sin_addr));
handle_input(client_sock, conn);
close(client_sock);
}
mysql_close(conn);
return 0;
}
void error_handling(const char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
void handle_input(int client_sock, MYSQL *conn)
{
char buffer[MAX_BUFFER_SIZE];
int str_len;
while ((str_len = read(client_sock, buffer, MAX_BUFFER_SIZE)) != 0)
{
buffer[str_len] = 0;
printf("Received message from client : %s\n", buffer);
if (strncmp(buffer, "add", 3) == 0)
{
// 处理添加员工信息请求
char name[50];
int age;
char department[50];
sscanf(buffer + 4, "%s %d %s", name, &age, department);
char query[100];
sprintf(query, "INSERT INTO employees (name, age, department) VALUES ('%s', %d, '%s');", name, age, department);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
write(client_sock, "success", 7);
}
else if (strncmp(buffer, "view", 4) == 0)
{
// 处理浏览员工信息请求
handle_view(client_sock, conn);
}
else if (strncmp(buffer, "modify", 6) == 0)
{
// 处理修改员工信息请求
handle_modify(client_sock, conn, buffer);
}
else if (strncmp(buffer, "search", 6) == 0)
{
// 处理查询员工信息请求
handle_search(client_sock, conn, buffer);
}
else if (strncmp(buffer, "delete", 6) == 0)
{
// 处理删除员工信息请求
handle_delete(client_sock, conn, buffer);
}
else
{
write(client_sock, "invalid command", 16);
}
}
}
void handle_view(int client_sock, MYSQL *conn)
{
char query[100] = "SELECT * FROM employees;";
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
MYSQL_RES *result = mysql_store_result(conn);
if (!result)
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for (int i = 0; i < num_fields; i++)
write(client_sock, row[i], strlen(row[i]));
write(client_sock, "\n", 1);
}
mysql_free_result(result);
write(client_sock, "success", 7);
}
void handle_modify(int client_sock, MYSQL *conn, char *buffer)
{
char name[50];
int age;
char department[50];
sscanf(buffer + 7, "%s %d %s", name, &age, department);
char query[100];
sprintf(query, "UPDATE employees SET age=%d, department='%s' WHERE name='%s'", age, department, name);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
if (mysql_affected_rows(conn) == 0)
{
write(client_sock, "no such employee", 17);
return;
}
write(client_sock, "success", 7);
}
void handle_search(int client_sock, MYSQL *conn, char *buffer)
{
char name[50];
sscanf(buffer + 7, "%s", name);
char query[100];
sprintf(query, "SELECT * FROM employees WHERE name='%s'", name);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
MYSQL_RES *result = mysql_store_result(conn);
if (!result)
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
int num_rows = mysql_num_rows(result);
if (num_rows == 0)
{
write(client_sock, "no such employee", 17);
return;
}
MYSQL_ROW row = mysql_fetch_row(result);
for (int i = 0; i < mysql_num_fields(result); i++)
write(client_sock, row[i], strlen(row[i]));
write(client_sock, "\n", 1);
mysql_free_result(result);
write(client_sock, "success", 7);
}
void handle_delete(int client_sock, MYSQL *conn, char *buffer)
{
char name[50];
sscanf(buffer + 7, "%s", name);
char query[100];
sprintf(query, "DELETE FROM employees WHERE name='%s'", name);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
write(client_sock, "error", 5);
return;
}
if (mysql_affected_rows(conn) == 0)
{
write(client_sock, "no such employee", 17);
return;
}
write(client_sock, "success", 7);
}
```
注意:此示例仅供参考,不可直接用于生产环境。在实际应用中,需要对代码进行适当修改和优化,以确保安全性和可靠性。
阅读全文