Linux下虚拟机服务器:
main.c
C语言``>
#include <stdio.h>
#include “PLot.h”
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include “db.h”
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define LEDON _IO(‘L’, 0)
#define LEDOFF _IO(‘L’, 2)
DB database;
PE Personal;
DB database1;
int main()
{
int fd_4 = open("/dev/led3", O_RDWR);
int fd_3 = open("/dev/led2", O_RDWR);
// 初始化
s.top = -1;
b.top = 0;
p.rear = 0;
p.count = 0;
p.front = 0;
//1. 向操作系统申请网卡功能
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror(“socket”);
return -1;
}
db_init(&database);
db_load(&database, "my.db");//2. 被动准备被客户端连接struct sockaddr_in serveraddr = {0};serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr("0.0.0.0");serveraddr.sin_port = htons(8888);int len = sizeof(serveraddr);if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, len)){perror("connect");return -1;}listen(sockfd, 10); //开启监听//3. 提取一路客户端的接入int clientfd = accept(sockfd, NULL, NULL);char bufa[200] = {0};sprintf(bufa, "欢迎来到长江智星智能停车场!\n");write(clientfd, bufa, sizeof(bufa));char buf[200] = {0};int i = 0;bzero(buf,sizeof(buf));while (sockfd != -1){sprintf(buf,"停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)% MAX_PAVE);sleep(1);write(clientfd,buf,sizeof(buf));bzero(buf,sizeof(buf));welcome();//4.接收请求read(clientfd, buf, 100);printf("recv: %s\n", buf);char buf1[100] = {0};//5处理请求if (0 == strncmp(buf, "1", 1)){strcpy(buf1, "请输入即将停车的车牌号:");write(clientfd, buf1, sizeof(buf1));bzero(buf, sizeof(buf));read(clientfd, buf, 100);car_come(buf);bzero(buf1, sizeof(buf1));strcpy(buf1, "停车成功!");write(clientfd, buf1, sizeof(buf1));ioctl(fd_4, LEDON);sleep(3);ioctl(fd_4, LEDOFF);//continue;}if (0 == strncmp(buf, "2", 1)){strcpy(buf1, "请输入即将离开的车牌号:");write(clientfd, buf1, sizeof(buf1)); // 输入车牌号bzero(buf, sizeof(buf));read(clientfd, buf, 100);car_leave(buf);bzero(buf1, sizeof(buf1));write(clientfd, buf, sizeof(buf));strcpy(buf1, "请扫码缴费");write(clientfd, buf1, sizeof(buf1));//continue;ioctl(fd_3, LEDON);sleep(3);ioctl(fd_3, LEDOFF);}if (0 == strncmp(buf, "3", 1)){//char bufb[100] = {0};//bzero(bufb,sizeof(bufb));Display();continue;}if (0 == strcmp(buf, "show")){bzero(buf, sizeof(buf));char buft[100] = {"请输入您的车牌号:"};write(clientfd, buft, sizeof(buft));read(clientfd, buf, 100);Display(buf);bzero(buft, sizeof(buft));//bzero(buf1,sizeof(buf1));db_show(&database1, buf1);strcat(buft, buf1);write(clientfd, buft, 100);}if( 0 == strncmp(buf,"save",4)){db_save(&database,"my.db");printf("====%s===\n",buf);}if( 0 == strncmp(buf,"6",1)){char bufi[100] = {0};sprintf(bufi,"缴费成功!");write(clientfd,bufi,sizeof(bufi));}if (0 == strncmp(buf, "4", 1))break;//5处理请求char a[30] = {"如需其他操作请先返回"};write(clientfd, a, 30);bzero(buf, sizeof(buf));read(clientfd, buf, 100);if (0 == strncmp(buf, "0", 1)){bzero(buf1, sizeof(buf1));strcpy(buf1, "返回成功!");write(clientfd, buf1, sizeof(buf1));continue;}else{char bufz[100] = {0};printf("您的输入有误,请重新输入\n");bzero(buf, sizeof(buf));sprintf(bufz, "您的输入有误,请重新输入\n");write(clientfd, bufz, sizeof(bufz));// read(clientfd, buf, 100);continue;}//6. 反馈结果write(clientfd, buf1, sizeof(buf1));}//7. 关闭通道,归还网卡close(sockfd);close(clientfd);return 0;
}
db.c:
#include “db.h”
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int db_load(DB *database, const char *dbfile)
{
//1. 只读方式打开文件,没有就创建
int fd = open(dbfile, O_RDONLY|O_CREAT, 0666);
if(-1 == fd)
{
perror(“open”);
return -1;
}
//2. 读出文件内容到数据库内存(dbfile---->database)read(fd, database, sizeof(DB));//3. 关闭文件close(fd);
}
int db_save(DB *database, const char *dbfile)
{
//1. 只写方式打开文件
int fd = open(dbfile, O_WRONLY, 0666);
if(-1 == fd)
{
perror(“open”);
return -1;
}
//2. 将数据库内存写入文件(database---->dbfile)write(fd, database, sizeof(DB));//3. 关闭文件close(fd);
}
int db_init(DB *database)
{
database->num = 0;return 0;
}
/*int db_insert(DB *database, const char *car)
{
if(MAXSIZE == database->num)
return DBFULL;
strcpy(database->users[database->num].car,car);database->num++;return 0;
}*/
int db_insert1(PE *Personal, const char *carnum)
{
if(MAXSIZE == Personal->num)
return DBFULL;
strcpy(Personal->Person[Personal->num].carnum,carnum);Personal->num++;return 0;
}
int db_delete(DB *database, const char *car)
{
//find it
int i = 0;
for(;inum; i++)
{
if( 0 == strcmp(database->users[i].car, car))
{//delete//move and coverwhile(i < database->num-1){database->users[i] = database->users[i+1];i++;}database->num--;return 0;}}return -1;
}
void db_show(DB *database, char *buf)
{
printf("车牌号\t\t\t停车时间\t\t当前所需金额\n");int i = 0;for(;i<database->num; i++){char bufx[100] = {0};sprintf(bufx, "%s\t\n", database->users[i].car);printf("%s\t\n",bufx);strcat(buf, bufx);}printf("\n");
}
db.h:
#ifndef DB_
#define DB_
#define MAXSIZE 100
//err code
#define DBFULL 1 /* database is full */
typedef struct{
struct{
char car[1000];
}users[MAXSIZE];
int num;
}DB;
typedef struct{
struct{
char car1[6];
}users[MAXSIZE];
int num;
}DB1;
typedef struct{
struct{
char carnum[6];
}Person[MAXSIZE];
int num;
}PE;
int db_load(DB *database, const char *dbfile);
int db_save(DB *database, const char *dbfile);
int db_init(DB *database);
int db_insert(DB *database, char *car);
int db_load1(PE *Persona, const char *dbfile);
int db_save1(PE *Persona, const char *dbfile);
int db_init1(PE *Persona);
//int db_insert1(PE *Personal, char *carnum);
//int db_delete(DB *database, const char *name, const char *pass);
//int db_update(DB *database, const char *name, const char *pass, const char *passnew);
//int db_select(DB *database, const char *name, const char *pass);
void db_show(DB *database, char *buf);
#endif
Plot.c:
//
// Created by PC-Saw on /12/17.
//
#include “PLot.h”
#include<string.h>
#include"db.h"
extern DB database;
extern PE Personal;
extern DB database1;
int db_insert(DB *database, char *car)
{
char *car1 = car;
if(MAXSIZE == database->num)
return DBFULL;
strcpy(database->users[database->num].car,car);database->num++;return 0;
}
void stop_to_pave() // 车停入便道
{
// 判断队满
if (p.count > 0 && (p.front == (p.rear + 1) % MAX_PAVE))
{
printf (“便道已满,请下次再来\n”);
}
else
{
strcpy(p.Pave[p.rear].plate, C); // 车进入便道
p.rear = (p.rear + 1) % MAX_PAVE; // 队尾指示器加1
p.count++; // 计数器加1
printf (“牌照为%s的汽车停入便道上的%d的位置\n”, C, p.rear);
}
}
void car_come(char *buf) // 车停入停车位
{
char *bufx = buf;if (s.top >= MAX_STOP - 1) // 如果停车位已满,停入便道{stop_to_pave(); // 停入便道}else{s.top++; // 停车位栈顶指针加1time_t t1;long int t = time(&t1); // 记录进入停车场的时间char* t2;char bufz[100] = {0};char buf1[10] = {};t2 = ctime (&t1); // 将当前时间转换为字符串s.Stop[s.top].timeIn = t;strcpy(s.Stop[s.top].plate, bufx);// 将车牌号登记printf ("牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", bufx, s.top + 1, t2);//sprintf(bufz,"牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", bufx, (char)s.top + 1, t2);db_insert1(&Personal,bufx);}
}
void stop_to_buff(char *buf) // 车进入让路栈
{
char *bufy = buf;
//char *bufa[200] = {0};
// 停车位栈压入临时栈,为需要出栈的车辆让出道
while (s.top >= 0)
{
if (0 == strcmp(s.Stop[s.top].plate, bufy))
{
break;
}
// 让出的车进入让路栈strcpy(b.Help[b.top++].plate, s.Stop[s.top].plate);printf ("牌照为%s的汽车暂时退出停车场\n", s.Stop[s.top--].plate);}// 如果停车位中的车都让了道,说明停车位中无车辆需要出行if (s.top < 0){printf ("停车位上无此车消息\n");}else{printf ("牌照为%s的汽车从停车场开走\n", s.Stop[s.top].plate);time_t t1;long int t = time (&t1);c.timeOut = t; // 标记离开停车场的时间char* t2;t2 = ctime (&t1); // 获取当前时间printf ("离开时间%s\n需付%d元\n", t2, (Price * (c.timeOut - s.Stop[s.top].timeIn))/10 );bzero(bufy,sizeof(bufy));sprintf(bufy,"牌照为%s的汽车从停车场开走\n离开时间%s\n需付%d元\n",s.Stop[s.top].plate, t2, (Price * (c.timeOut - s.Stop[s.top].timeIn))/10);s.top--;}// 将让路栈中的车辆信息压入停车位栈while (b.top > 0){strcpy(s.Stop[++s.top].plate, b.Help[--b.top].plate);printf ("牌照为%s的汽车停回停车位%d车位\n", b.Help[b.top].plate, s.top);}// 从便道中 -> 停车位while (s.top < MAX_STOP-1){if (0 == p.count)// 判断队列是否为空{break;} // 不为空,将便道中优先级高的车停入停车位else{strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);printf ("牌照为%s的汽车从便道中进入停车位的%d车位\n", p.Pave[p.front].plate, s.top);p.front = (p.front + 1) % MAX_PAVE;p.count--;}}
}
void car_leave(char *buf) // 车离开
{
char *bufx = buf ;
if (s.top < 0) // 判断停车位是否有车辆信息
{
printf (“车位已空,无车辆信息!\n”);
}
else
{ db_delete(&database,bufx);
stop_to_buff(bufx);
//db_delete(&database,bufx);
}
//db_delete(&database,bufx);
}
void Display(char *buf)
{
char *bufx = buf;
char *bufy = buf;
char buft[300] = {0};int i = s.top;if (-1 == i){printf ("停车场为空\n");}time_t t1;long int t = time(&t1); // 标记显示时的时间while (i != -1){// printf ("%s\t\t%ld秒\t\t\t%ld元\n", bufx, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);sprintf(buft,"%s\t\t\t%ld秒\t\t\t%ld元\n", bufx, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);//db_insert(&database,buft);i--;//s.Stop[i].plate}db_insert(&database1,buft);
}
void welcome()
{
printf ("\t******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)
% MAX_PAVE);
printf ("\t********************************************************\n");
printf ("\t---------------Welcome to our Car Parking---------------\n");
printf ("\t1.Parking\n");
printf ("\t2.leaving\n");
printf ("\t3.situation\n");
printf ("\t4.exit\n");
printf ("\t5.show *\n");
printf ("\t--------------------------------------------------------\n");
}
Plot.h:
//
// Created by PC-Saw on /12/17.
//
#ifndefPLOT_H
#definePLOT_H
#define Price 1 // 单价可以自己定义n
#define MAX_STOP 10
#define MAX_PAVE 10
#include <stdlib.h>
#include <stdio.h>
#include <time.h> // 包含时间函数的头文件
#include <string.h>
// 汽车信息
typedef struct
{
int timeIn; // 进入停车场时间int timeOut; // 离开停车场时间char plate[10];// 汽车牌照号码,定义一个字符指针类型
}Car;
// 停放栈(用于停放车辆)
typedef struct
{
Car Stop[MAX_STOP]; // 用于停放车辆的栈
int top; // 标记栈顶位置
}Stopping;
// 等候队列
typedef struct
{
int count; // 用来指示队中的数据个数
Car Pave[MAX_PAVE]; // 等候停车的队列
int front, rear; // 标记队头和队尾位置
}Pavement;
// 让路栈
typedef struct
{
Car Help[MAX_STOP]; // 用于让路的队列
int top; // 标记站定位置
}Buffer;
Stopping s;
Pavement p;
Buffer b;
Car c;
char C[10];
void stop_to_pave(); // 车停入便道
void car_come (); // 车停入停车位
void stop_to_buff(); // 车进入让路栈
void car_leave (); // 车离开
void welcome (); // 主界面函数
void Display (); // 显示车辆信息
#endif //PLOT_H
db.h:
#ifndef DB_
#define DB_
#define MAXSIZE 100
//err code
#define DBFULL 1 /* database is full */
typedef struct{
struct{
char car[1000];
}users[MAXSIZE];
int num;
}DB;
typedef struct{
struct{
char car1[6];
}users[MAXSIZE];
int num;
}DB1;
typedef struct{
struct{
char carnum[6];
}Person[MAXSIZE];
int num;
}PE;
int db_load(DB *database, const char *dbfile);
int db_save(DB *database, const char *dbfile);
int db_init(DB *database);
int db_insert(DB *database, char *car);
int db_load1(PE *Persona, const char *dbfile);
int db_save1(PE *Persona, const char *dbfile);
int db_init1(PE *Persona);
//int db_insert1(PE *Personal, char *carnum);
//int db_delete(DB *database, const char *name, const char *pass);
//int db_update(DB *database, const char *name, const char *pass, const char *passnew);
//int db_select(DB *database, const char *name, const char *pass);
void db_show(DB *database, char *buf);
#endif