用c语言写一个tcp传输的仓库管理系统
时间: 2023-11-19 17:06:32 浏览: 87
实现一个完整的TCP传输的仓库管理系统需要涉及到很多知识点和技术,包括网络编程、数据库设计、数据结构与算法等。以下是一个简单的C语言代码示例,仅供参考。
首先,我们需要用C语言实现一个基本的TCP服务器,代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_PORT 8888
#define MAX_PENDING 5
#define BUFFER_SIZE 1024
int main(int argc, char* argv[]) {
// 创建 socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
printf("Error creating socket!\n");
exit(EXIT_FAILURE);
}
// 绑定端口号
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
printf("Error binding socket!\n");
exit(EXIT_FAILURE);
}
// 监听端口
if (listen(server_fd, MAX_PENDING) == -1) {
printf("Error listening on socket!\n");
exit(EXIT_FAILURE);
}
// 循环接受客户端请求
while (1) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd == -1) {
printf("Error accepting client connection!\n");
continue;
}
// 接受客户端消息
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int len = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (len == -1) {
printf("Error receiving from client!\n");
close(client_fd);
continue;
}
printf("Received message from client: %s\n", buffer);
// 发送响应消息
char response[] = "Hello, client!";
len = send(client_fd, response, strlen(response), 0);
if (len == -1) {
printf("Error sending response to client!\n");
close(client_fd);
continue;
}
// 关闭客户端连接
close(client_fd);
}
// 关闭服务器 socket
close(server_fd);
return 0;
}
```
上述代码实现了一个简单的TCP服务器,它会监听指定的端口,接受客户端的连接请求,并处理客户端发送的消息。这里使用了常量 `SERVER_PORT` 来指定服务器监听的端口号,也可以通过命令行参数来传入。
接下来,我们需要设计一个仓库管理系统的数据库,并使用C语言实现相关的数据结构和算法。这里假设我们的仓库管理系统需要管理商品的信息,包括商品编号、名称、价格、库存等。我们可以使用MySQL数据库来存储商品信息,使用C语言的MySQL API来访问数据库。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#define DB_HOST "localhost"
#define DB_USER "root"
#define DB_PASS "password"
#define DB_NAME "warehouse"
typedef struct {
int id;
char name[100];
float price;
int stock;
} Product;
int main(int argc, char* argv[]) {
// 连接数据库
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 0, NULL, 0)) {
printf("Error connecting to database: %s\n", mysql_error(conn));
exit(EXIT_FAILURE);
}
// 查询商品列表
MYSQL_RES* result = NULL;
MYSQL_ROW row;
char query[1024];
sprintf(query, "SELECT id, name, price, stock FROM product");
if (mysql_query(conn, query)) {
printf("Error querying database: %s\n", mysql_error(conn));
mysql_close(conn);
exit(EXIT_FAILURE);
}
result = mysql_store_result(conn);
if (!result) {
printf("Error storing result from database: %s\n", mysql_error(conn));
mysql_close(conn);
exit(EXIT_FAILURE);
}
// 遍历查询结果
printf("Product list:\n");
while ((row = mysql_fetch_row(result))) {
Product product;
product.id = atoi(row[0]);
strcpy(product.name, row[1]);
product.price = atof(row[2]);
product.stock = atoi(row[3]);
printf("%d\t%s\t%.2f\t%d\n", product.id, product.name, product.price, product.stock);
}
// 释放结果集
mysql_free_result(result);
// 关闭数据库连接
mysql_close(conn);
return 0;
}
```
上述代码实现了一个简单的查询商品列表的功能,它会连接到指定的MySQL数据库,查询商品信息,并将结果打印出来。这里使用了常量 `DB_HOST`、`DB_USER`、`DB_PASS`、`DB_NAME` 来指定数据库的连接信息,也可以通过命令行参数来传入。
最后,我们需要将TCP服务器和数据库操作结合起来,实现一个完整的仓库管理系统。具体实现方式和细节因应用场景而异,这里只提供一个简单的示例。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <mysql/mysql.h>
#define SERVER_PORT 8888
#define MAX_PENDING 5
#define BUFFER_SIZE 1024
#define DB_HOST "localhost"
#define DB_USER "root"
#define DB_PASS "password"
#define DB_NAME "warehouse"
typedef struct {
int id;
char name[100];
float price;
int stock;
} Product;
int main(int argc, char* argv[]) {
// 创建 socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
printf("Error creating socket!\n");
exit(EXIT_FAILURE);
}
// 绑定端口号
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
printf("Error binding socket!\n");
exit(EXIT_FAILURE);
}
// 监听端口
if (listen(server_fd, MAX_PENDING) == -1) {
printf("Error listening on socket!\n");
exit(EXIT_FAILURE);
}
// 连接数据库
MYSQL* conn = mysql_init(NULL);
if (!mysql_real_connect(conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 0, NULL, 0)) {
printf("Error connecting to database: %s\n", mysql_error(conn));
exit(EXIT_FAILURE);
}
// 循环接受客户端请求
while (1) {
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd == -1) {
printf("Error accepting client connection!\n");
continue;
}
// 接受客户端消息
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
int len = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (len == -1) {
printf("Error receiving from client!\n");
close(client_fd);
continue;
}
printf("Received message from client: %s\n", buffer);
// 处理客户端请求
if (strcmp(buffer, "list") == 0) {
// 查询商品列表
MYSQL_RES* result = NULL;
MYSQL_ROW row;
char query[1024];
sprintf(query, "SELECT id, name, price, stock FROM product");
if (mysql_query(conn, query)) {
printf("Error querying database: %s\n", mysql_error(conn));
close(client_fd);
continue;
}
result = mysql_store_result(conn);
if (!result) {
printf("Error storing result from database: %s\n", mysql_error(conn));
close(client_fd);
continue;
}
// 发送响应消息
char response[BUFFER_SIZE];
memset(response, 0, BUFFER_SIZE);
while ((row = mysql_fetch_row(result))) {
Product product;
product.id = atoi(row[0]);
strcpy(product.name, row[1]);
product.price = atof(row[2]);
product.stock = atoi(row[3]);
sprintf(response + strlen(response), "%d\t%s\t%.2f\t%d\n", product.id, product.name, product.price, product.stock);
}
len = send(client_fd, response, strlen(response), 0);
if (len == -1) {
printf("Error sending response to client!\n");
close(client_fd);
continue;
}
// 释放结果集
mysql_free_result(result);
} else if (strcmp(buffer, "add") == 0) {
// 添加商品
char product_info[BUFFER_SIZE];
memset(product_info, 0, BUFFER_SIZE);
len = recv(client_fd, product_info, BUFFER_SIZE, 0);
if (len == -1) {
printf("Error receiving product info from client!\n");
close(client_fd);
continue;
}
printf("Received product info from client: %s\n", product_info);
Product product;
sscanf(product_info, "%d %s %f %d", &product.id, product.name, &product.price, &product.stock);
char query[1024];
sprintf(query, "INSERT INTO product (id, name, price, stock) VALUES (%d, '%s', %.2f, %d)", product.id, product.name, product.price, product.stock);
if (mysql_query(conn, query)) {
printf("Error adding product to database: %s\n", mysql_error(conn));
close(client_fd);
continue;
}
// 发送响应消息
char response[] = "Product added!";
len = send(client_fd, response, strlen(response), 0);
if (len == -1) {
printf("Error sending response to client!\n");
close(client_fd);
continue;
}
} else {
// 未知请求
char response[] = "Unknown request!";
len = send(client_fd, response, strlen(response), 0);
if (len == -1) {
printf("Error sending response to client!\n");
close(client_fd);
continue;
}
}
// 关闭客户端连接
close(client_fd);
}
// 关闭服务器 socket
close(server_fd);
// 关闭数据库连接
mysql_close(conn);
return 0;
}
```
上述代码实现了一个简单的仓库管理系统,它会利用TCP服务器和MySQL数据库来管理商品信息。当客户端发送 `list` 请求时,服务器会查询商品列表,并将结果发送给客户端;当客户端发送 `add` 请求时,服务器会解析客户端发送的商品信息,并将其添加到数据库中。这里使用了结构体 `Product` 来表示商品信息,也可以使用其他数据结构。
阅读全文