基于Linux C上的TCP/IP协议完成的电子点餐系统

基于Linux C上的TCP/IP协议完成的电子点餐系统

运行环境:Ubuntu
客户端用来点餐,然后将点餐信息发送到服务器端,这里面客户端和服务器端都是同一台机器。
运行过程:
先运行服务器端MainServer,然后打开另一个终端运行客户端MainClient进行点菜,完成点菜后会将用户的点菜信息发送给服务器端显示。

//Consumer.h

#ifndef _CONSUMER_H_
#define _CONSUMER_H_
#define DISH_NUM 6
struct con_dish
{
	int dish_no;
	int dish_num;//点餐数量
	char dish_name[20];
	float dish_price;//单价
	float dish_sum_price;
};
struct con_ //存储消费信息
{
	struct con_dish dish[DISH_NUM];
	char *user;
	char *con_order_no;//订单号
	float sum_price;
}con;

#endif
///
//Dish.h

#ifndef _DISH_H_
#define _DISH_H_
#include<stdio.h>
#define NUM 20
struct dish_
{
	int dish_no;//菜品编号
	float dish_price;//菜品价格
	char dish_name[NUM];
	char dish_class[NUM];

}dish;
#endif
///
//DishClient.h

#include"Dish.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include"Consumer.h"
#include"TCPClient.h"
#define DISH_NUM 6
char *IP = "127.0.0.1";
struct dish_ Dish[DISH_NUM];
struct con_ Con;//
void dish_init()
{
	float price[DISH_NUM] = {100,99,55,87,55,65};
	char name[DISH_NUM][20]={"汉堡\0","炸鸡\0","可乐\0","拿铁\0","鸡肉卷\0","薯条\0"};
	for(int i =0;i<DISH_NUM;i++)
	{

	       Dish[i].dish_no = i+1;
       		strcpy(Dish[i].dish_name , name[i]);
      		 Dish[i].dish_price = price[i];
	}
}
void con_init()//仅仅初始化消费者中菜品信息
{
	float price[DISH_NUM] = {100,99,55,87,55,65};
char name[DISH_NUM][20]={"汉堡\0","炸鸡\0","可乐\0","拿铁\0","鸡肉卷\0","薯条\0"};
	
	Con.sum_price = 0;
	for(int i = 0;i<DISH_NUM;i++)
	{
		strcpy(Con.dish[i].dish_name,name[i]);
		Con.dish[i].dish_sum_price = 0;
		Con.dish[i].dish_no = i+1;
		Con.dish[i].dish_price =price[i];
	}
}

void con_sum_price()//计算消费者消费信息
{
	int i = 0;
	for(;i<DISH_NUM;i++)
	{

		Con.sum_price +=Con.dish[i].dish_num*Con.dish[i].dish_price;
		Con.dish[i].dish_sum_price=Con.dish[i].dish_num*Con.dish[i].dish_price;
	}


}
void print_con()
{
	printf("你的消费信息:\n--------------------------------------------------------\n");
	printf("序号\t菜名\t\t单价(元)\t数量\t总价\n");
	for(int j = 0;j<DISH_NUM;j++)
		if(Con.dish[j].dish_num!=0)
		 printf("%d\t%s\t\t%.2f\t\t%d\t%.2f\n",Con.dish[j].dish_no,Con.dish[j].dish_name,Con.dish[j].dish_price,Con.dish[j].dish_num,Con.dish[j].dish_sum_price);
	printf("--------------------------------------------------------\n");
	printf("总价:%.2f\n",Con.sum_price);
}

void print_menu()
{
		int j = 0;
	printf("序号\t菜名\t\t单价(元)\n-----------------------------------\n");
            for (; j < DISH_NUM; j++) {
		    printf("%d\t\%s\t\t%.2f\n",Dish[j].dish_no,Dish[j].dish_name,Dish[j].dish_price);
                }
	    
}


void order()
{
	dish_init();
	con_init();
	int order;

			system("reset");
		print_menu();
		printf("请点餐,按0退出结账>\n");
	while(1)
	{
		scanf("%d",&order);
		if(order ==0)
		{
			break;
		}
		else if(order>0&&order<=(DISH_NUM+1))
		{
			for(int i = 0;i<DISH_NUM;i++)
			{
				if(order == Con.dish[i].dish_no)
				{
					Con.dish[i].dish_num++;
					break;
				}
			}
		}
		else
			printf("请输入正确指令>\n");
	}
	con_sum_price();//计算价格
	print_con();//打印消费信息
	client(IP,Con);//
}
		

//TCPClient.h

#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/socket.h>
#include"Consumer.h"
#define SERVPORT 3333
#define MAXDATASIZE 100 /*每次最大数据传输量 */

void client(char *ip,struct con_ Con)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if((host=gethostbyname(ip))==NULL) {
herror("gethostbyname error!");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket create error!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
if (connect(sockfd, (struct sockaddr *)&serv_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect error!");
exit(1);
} 
if (send(sockfd, &Con, sizeof(struct con_), 0) == -1)
perror("send 出错!");
close(sockfd);
}

///
//TCPServer.h

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include<arpa/inet.h>
#include<unistd.h>
#include"Consumer.h"
#define SERVPORT 3333 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
void print_con();
struct con_ Con;
void server()
{
	int recvbytes;
	char *buf;
int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
struct sockaddr_in my_addr; /* 本机地址信息 */
struct sockaddr_in remote_addr; /* 客户端地址信息 */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ 
perror("socket 创建失败!"); exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);/* htons()函数把主机字节序转换成网络字节序*/
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8); /*保持与struct sockaddr 同样大小 */
if(bind(sockfd,(struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind 出错!");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen 出错!");
exit(1);
}
while(1)
{
	sin_size = sizeof(struct sockaddr_in);
	if((client_fd=accept(sockfd,
	(struct sockaddr*)&remote_addr,&sin_size))==-1)
	{
	perror("accept error");
	continue;
	}
	if (!fork())
	{
	recvbytes = recv(client_fd,&Con,sizeof(struct con_),0);
	
	if(recvbytes == -1){

		perror("recv出错");
		exit(1);
	}
	print_con();
	close(client_fd);
	
	exit(0);
	}
	close(client_fd);
	}	
}

void print_con()
{
	printf("你的消费信息:\n--------------------------------------------------------\n");
	printf("序号\t菜名\t\t单价(元)\t数量\t总价\n");
	for(int j = 0;j<DISH_NUM;j++)
		if(Con.dish[j].dish_num!=0)
		 printf("%d\t%s\t\t%.2f\t\t%d\t%.2f\n",Con.dish[j].dish_no,Con.dish[j].dish_name,Con.dish[j].dish_price,Con.dish[j].dish_num,Con.dish[j].dish_sum_price);
	printf("--------------------------------------------------------\n");
	printf("总价:%.2f\n",Con.sum_price);
}



//MainClient.c


#include<stdio.h>
#include"DishClient.h"
#include"Consumer.h"

int main()
{
	order();
	return 0;
}
///
//MainServer.c


#include"TCPServer.h"
int main()
{
	server();
}
	

有用请点赞哦

注:TCP/IP协议代码改编自金国庆,刘家海,季江民等《Linux程序设计》(第三版)

上一篇:利用JavaScript+CSS+DIV实现下拉菜单源代码


下一篇:12C pdb cdb常用命令