搭车系统 Server端
servermain.cpp
#include<iostream>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<iomanip>
#include<string.h>
#include<stdio.h>
#include<pthread.h>
#include<fstream>
#include<map>
#include<set>
#include"message_type.h"
#include"server.h"
using namespace std;
int main()
{
udp_main();
tcp_main();
return 0;
}
server.h
#ifndef _SERVER_H_
#define _SERVER_H_
#include <map>
#include <set>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static int server_tcp_sockfd;
#define DRIVER_FILE "driver.txt"
#define USER_FILE "user.txt"
struct package{
int sockfd;
int len;
};
struct APINFO{
char account[1024]={0};
char password[1024]={0};
int balance=100;
};
struct NETINFO{
struct sockaddr_in sockaddr;
int tcp=0 ;
int udp=0 ;
};
struct ADDRESSINFO{
int startjd=0;
int startwd=0;
int goaljd=0 ;
int goalwd =0;
};
struct PERINFO{
char name[256]={0};
char sex[10]={0};
char IDcard[20]={0};
char homeadd[1024]={0};
char phonenum[20]={0};
char carnum[256]="0000";
};
struct ALLINFO{
struct APINFO apinfo;
struct NETINFO netinfo;
struct ADDRESSINFO addressinfo;
struct PERINFO perinfo;
};
struct USERINFO{
char account[1024]={0};
struct sockaddr_in useraddr;
struct ADDRESSINFO addressinfo;
struct PERINFO perinfo;
};
struct CHATINFO{
char recvaccount[1024]={0};
char sendtoaccount[1024]={0};
char buff[1024]={0};
};
static std::set<std::string> request_list;
static std::map<std::string , struct ALLINFO> clientmap;
static void create_thread_function( int , size_t ,void*(*fp)(void*));
void*save_user_accountpassword(void * );
void tcp_main();
void* user_tcp_login(void* ) ;
void* user_order(void* );
void add_show(ADDRESSINFO &);
void*save_driver_accountpassword(void * );
void*driver_tcp_login(void*);
void* save_driver_GPS(void* );
void* driver_task(void* );
void* user_task_driverinfo(void* );
void* chat_main(void* );
void udp_main();
#endif
server.cpp
#include<iostream>
#include<sys/un.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<iomanip>
#include<string.h>
#include<string>
#include<stdio.h>
#include<pthread.h>
#include<fstream>
#include<set>
#include<map>
#include"message_type.h"
#include"server.h"
using namespace std;
void tcp_main()
{
int ret ;
int sockfd;
struct sockaddr_in serveraddr;
struct message message ;
sockfd = socket(AF_INET , SOCK_STREAM, 0);
//设置服务器地址
serveraddr.sin_family = AF_INET ;
serveraddr.sin_port = htons(8000);
serveraddr.sin_addr.s_addr = INADDR_ANY ;
//绑定----tcp ----udp----
ret = bind (sockfd , (struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in));
if(ret == -1){
cout <<"tcp bind fail" << endl;
exit(1);
}
listen(sockfd , 5);
cout <<" //等待与客服端建立链接" <<endl;
while(1){
server_tcp_sockfd = accept(sockfd , 0 , 0 );
cout << " server TCP 响应请求" << endl;
read(server_tcp_sockfd ,&message , sizeof(message) );
//进行消息分类处理
switch(message.type)
{
case 1000:
{
cout <<" 司机上报GPS" << endl;
create_thread_function(server_tcp_sockfd , message.type_len,save_driver_GPS );
break ;
}
case 1002 :
{
cout << "司机注册"<< endl;
create_thread_function(server_tcp_sockfd , message.type_len,save_driver_accountpassword ) ;
}
break ;
case 1003 :
{
cout << "司机登录" << endl;
create_thread_function(server_tcp_sockfd , message.type_len, driver_tcp_login );
}
break ;
case 1004 :
{ //司机退出
// int driver_tcp_sockfd = server_tcp_sockfd ;
// close(server_tcp_sockfd);
break ;
}
case 1006 :
{
cout <<"进入聊天服务"<<endl;
}
getchar();
break ;
case 1007 :
break ;
case 1008 :
{
cout << "司机接任务,确定了乘客" << endl;
create_thread_function(server_tcp_sockfd , message.type_len ,driver_task );
}
break ;
case 1009 :
//关单
//太多了 省略
break ;
case 2000 :
{
cout <<"乘客注册"<< endl;
create_thread_function(server_tcp_sockfd , message.type_len,save_user_accountpassword );
}
break ;
case 2001 :
{
cout << "乘客登录"<< endl;
create_thread_function(server_tcp_sockfd , message.type_len ,user_tcp_login);
}
break ;
case 2002 :
{ //乘客退出
// int client_tcp_sockfd = server_tcp_sockfd ;
// close(client_tcp_sockfd);
break ;
}
case 2003 :
{
cout<<" 乘客下单 要求订车"<< endl;
create_thread_function(server_tcp_sockfd , message.type_len ,user_order);
}
break ;
case 2004 :
break ;
case 2005 :
{
cout <<"下载司机信息 "<< endl;
create_thread_function(server_tcp_sockfd , message.type_len ,user_task_driverinfo);
}
break ;
default :
break ;
}
}
}
//-------create_thread_all_function-------
void create_thread_function( int sockfd , size_t len ,void*(*fp)(void*)){
int ret ;
pthread_t thread_t ;
struct package* p = new package;
p->sockfd = sockfd;
p->len = len;
ret = pthread_create(&thread_t , NULL , fp ,(void*)p);
if(ret != 0){
cout <<"pthread_all_create fail"<< endl;
exit(1);
}
}
void*save_user_accountpassword(void * arg){
int ret ;
struct package* p = (struct package*)arg;
int sockfd = p->sockfd;
int len = p->len;
struct AP ap;
ret=read(sockfd ,&ap, len);
ap_show(ap);
string str = ap.account;
map<string ,ALLINFO>::iterator its = clientmap.find(str) ;
if(its == clientmap.end()){
ofstream file_fd;
file_fd.open(USER_FILE,ios::app);
//写入文件保存
file_fd<<ap.account<<endl;
file_fd<<ap.password<<endl;
file_fd.close();
ALLINFO allinfo;
strcpy(allinfo.apinfo.account,ap.account);
strcpy(allinfo.apinfo.password,ap.password);
clientmap.insert(make_pair(str , allinfo));
cout <<"map==="<<endl;
map<string ,ALLINFO>::iterator itss = clientmap.find(str);
cout << itss->first<< endl;
cout << (itss->second).apinfo.password<< endl;
cout << (itss->second).apinfo.balance<< endl;
//返回1111成功标志
struct message message;
message.type = 1111;
write (sockfd , &message , sizeof(struct message));
read(sockfd , &(itss->second).perinfo , sizeof(PERINFO));
cout << "ID CEAR=="<<(itss->second).perinfo.IDcard<< endl;
}else{
struct message message;
message.type= 0;
write (sockfd , &message , sizeof(struct message));
}
if(p){
delete p;
p = NULL ;
}
close(sockfd);
pthread_exit ( (void*)0 );
}
void*save_driver_accountpassword(void * arg){
int ret ;
struct package* p = (struct package*)arg;
int sockfd = p->sockfd;
int len = p->len;
struct AP ap;
ret=read(sockfd ,&ap, len);
ap_show(ap);
string str = ap.account;
map<string ,ALLINFO>::iterator its = clientmap.find(str) ;
if(its == clientmap.end()){
ofstream file_fd;
file_fd.open(DRIVER_FILE,ios::app);
//写入文件保存
file_fd<<ap.account<<endl;
file_fd<<ap.password<<endl;
file_fd.close();
ALLINFO allinfo;
strcpy(allinfo.apinfo.account,ap.account);
strcpy(allinfo.apinfo.password,ap.password);
clientmap.insert(make_pair(str , allinfo));
//返回1111成功标志
struct message message;
message.type = 1111;
write (sockfd , &message , sizeof(struct message));
map<string ,ALLINFO>::iterator itss = clientmap.find(str) ;
read(sockfd , &(itss->second).perinfo , sizeof(PERINFO));
}else{
struct message message;
message.type= 0;
write (sockfd , &message , sizeof(struct message));
}
if(p){
delete p;
p = NULL ;
}
close(sockfd);
pthread_exit ( (void*)0 );
}
void* user_tcp_login(void*arg){
struct package* p = (struct package*)arg;
int sockfd = p->sockfd;
int len = p->len;
AP ap;
read(sockfd ,&ap,len);
cout << "登录的信息为"<< endl;
ap_show(ap);
cout << endl;
struct message message;
string str1 = ap.account;
string str2 = ap.password;
string str3;
map<string , ALLINFO>::iterator its =clientmap.find(str1);
if((its != clientmap.end())&&(str2 == (str3=(its->second).apinfo.password))){
message.type = 1111;
write(sockfd ,&message , sizeof(message));
//写入IP
read(sockfd , &(its->second).netinfo.sockaddr , sizeof(sockaddr_in));
}else{
message.type = 0;
write(sockfd ,&message , sizeof(message));
}
if(p){
delete p;
p = NULL ;
}
pthread_exit ( (void*)0 );
}
void* driver_tcp_login(void*arg){
struct package* p = (struct package*)arg;
int sockfd = p->sockfd;
int len = p->len;
AP ap;
read(sockfd ,&ap,len);
cout <<"司机登录的信息为"<< endl;
ap_show(ap);
cout << endl;
struct message message;
string str1 = ap.account;
string str2 = ap.password;
string str3;
map<string , ALLINFO>::iterator its =clientmap.find(str1);
if((its != clientmap.end())&&(str2 == (str3=(its->second).apinfo.password))){
message.type = 1111;
write(sockfd ,&message , sizeof(message));
//写入IP
read(sockfd , &(its->second).netinfo.sockaddr , sizeof(sockaddr_in));
}else{
message.type = 0;
write(sockfd ,&message , sizeof(message));
}
if(p){
delete p;
p = NULL ;
}
pthread_exit ( (void*)0 );
}
void* user_order(void* arg){
char clientaccount[1024]={0};
struct package* p = (struct package*) arg;
int sockfd = p->sockfd;
int len = p->len;
ADDRESSINFO addressinfo;
read(sockfd ,&clientaccount, len);
string str = clientaccount;
read(sockfd ,&addressinfo, sizeof(addressinfo));
map<string , ALLINFO >::iterator its = clientmap.find(str);
if(its != clientmap.end() ){
struct message message ;
message.type = 1111;
write(sockfd , &message , sizeof(message));
//写入TCP
(its->second).netinfo.tcp = sockfd;
cout << "suer tcp "<< sockfd << endl;
cout << (its->second.netinfo.tcp)<< endl;
//添加到请求链表
request_list.insert(str);
//完成地址更新
(its->second).addressinfo=addressinfo;
}
add_show(its->second.addressinfo);
if(p){
delete p;
p = NULL ;
}
return (void*) 0;
}
void* save_driver_GPS(void* arg){
char driveraccount[1024]={0};
struct package* p = (struct package*) arg;
int sockfd = p->sockfd;
int len = p->len;
ADDRESSINFO addressinfo;
read(sockfd ,&driveraccount, len);
string str = driveraccount;
read(sockfd ,&addressinfo, sizeof(addressinfo));
map<string , ALLINFO >::iterator its = clientmap.find(str);
if(its != clientmap.end() ){
struct message message ;
USERINFO userinfo;
int i=0;
message.type = 1111;
//写入TCP
(its->second).netinfo.tcp = sockfd;
//完成地址更新
(its->second).addressinfo=addressinfo;
add_show(its->second.addressinfo);
write(sockfd , &message , sizeof(message));
map<string ,ALLINFO>::iterator it ;
set<string>:: iterator itss = request_list.begin();
while(itss != request_list.end()){
strcpy(userinfo.account , (*itss).c_str());
it = clientmap.find(*(itss));
userinfo.useraddr= (it->second).netinfo.sockaddr;
userinfo.addressinfo = (it->second).addressinfo;
strcpy(userinfo.perinfo.name , (it->second).perinfo.name);
strcpy(userinfo.perinfo.sex , (it->second).perinfo.sex);
strcpy(userinfo.perinfo.IDcard , (it->second).perinfo.IDcard);
strcpy(userinfo.perinfo.homeadd , (it->second).perinfo.homeadd);
strcpy(userinfo.perinfo.phonenum , (it->second).perinfo.phonenum);
write(sockfd , &userinfo , sizeof(userinfo));
++itss;
}
close(sockfd);
}
if(p){
delete p;
p = NULL ;
}
return (void*) 0;
}
void add_show(ADDRESSINFO &rh){
system("clear");
std::cout <<"=========您输入的行程============"<< std::endl;
std::cout << std::endl;
std::cout << "出发地经纬度 :" << rh.startjd<<":"<< rh.startwd<<std::endl;
std::cout << "目的地经纬度 :" << rh.goaljd<< ":"<<rh.goalwd << std::endl;
std::cout << std::endl;
std::cout << "======================================="<<std::endl;
}
void* driver_task(void* arg){
int ret ;
struct package* p = (struct package*)arg;
int sockfd = p->sockfd;
int len = p->len;
char useraccount[1024]={0};
char driveraccount[1024]={0};
read(sockfd ,useraccount , len);
string str= useraccount;
read(sockfd ,driveraccount, sizeof(driveraccount));
map<string ,ALLINFO>::iterator its = clientmap.find(str);
set<string>::iterator itss = request_list.find(str);
struct message message;
if(itss != request_list.end()){
int user_tcp_sockfd = (its->second).netinfo.tcp;
message.type= 1111;
cout << "use_tcp_sockfd"<< user_tcp_sockfd << endl;
write(user_tcp_sockfd , driveraccount , sizeof(driveraccount));
cout << "=========================="<<endl;
write(sockfd ,&message, sizeof(message));
request_list.erase(str);
sleep(1);
close(user_tcp_sockfd);
}else{
message.type= 0;
write(sockfd , &message , sizeof(message));
}
if(p){
delete p;
p = NULL ;
}
close(sockfd);
return (void*) 0;
}
void* user_task_driverinfo(void* arg){
char driveraccount[1024]={0};
struct package* p = (struct package*) arg;
int sockfd = p->sockfd;
int len = p->len;
read(sockfd ,driveraccount, len);
string str = driveraccount;
map<string , ALLINFO >::iterator its = clientmap.find(str);
if(its != clientmap.end() ){
USERINFO userinfo;
strcpy(userinfo.account , (its->first).c_str());
userinfo.useraddr= (its->second).netinfo.sockaddr;
userinfo.addressinfo = (its->second).addressinfo;
strcpy(userinfo.perinfo.name , (its->second).perinfo.name);
strcpy(userinfo.perinfo.sex , (its->second).perinfo.sex);
strcpy(userinfo.perinfo.IDcard , (its->second).perinfo.IDcard);
strcpy(userinfo.perinfo.homeadd , (its->second).perinfo.homeadd);
strcpy(userinfo.perinfo.phonenum , (its->second).perinfo.phonenum);
strcpy(userinfo.perinfo.carnum , (its->second).perinfo.carnum );
write(sockfd , &userinfo , sizeof(userinfo));
sleep(1);
close(sockfd);
}else{
USERINFO userinfo;
write(sockfd , &userinfo , sizeof(userinfo));
sleep(1);
close(sockfd);
}
if(p){
delete p;
p = NULL ;
}
return (void*) 0;
}
void udp_main(){
pthread_t thread;
pthread_create(&thread , NULL , chat_main , (void*)0);
}
void* chat_main(void* arg){
int ret ;
int sockfd;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
socklen_t clientlen = sizeof(clientaddr);
CHATINFO chatinfo;
map<string , ALLINFO>::iterator its ;
string str;
sockfd = socket(AF_INET , SOCK_DGRAM, 0);
//设置服务器地址
serveraddr.sin_family = AF_INET ;
serveraddr.sin_port = htons(8000);
serveraddr.sin_addr.s_addr = INADDR_ANY ;
socklen_t serverlen = sizeof(sockaddr_in);
//绑定- ---udp----
ret = bind (sockfd , (struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in));
if(ret == -1){
cout <<"udp bind fail" << endl;
exit(1);
}
cout<<"udp 相应"<< endl;
while(1){
recvfrom(sockfd , (void*)&chatinfo , (size_t)sizeof(CHATINFO) ,0 , (struct sockaddr*)&serveraddr ,&serverlen);
str= chatinfo.recvaccount;
its= clientmap.find(str);
if(its != clientmap.end()){
clientaddr = (its->second).netinfo.sockaddr;
sendto(sockfd , (void*)&chatinfo , sizeof(CHATINFO) ,0 , (struct sockaddr*)&clientaddr ,clientlen );
}
}
return (void*) 0;
}
struct_info.h
#ifndef _STRUCT_INFO_
#define _STRUCT_INFO_
struct INFO {
int ID ;
unsigned long int clientIP ; // 客户IP
unsigned long int driverIP ; //司机IP
long int longitude ; //维度
long int datitude ; //经度
int client_tcp_sockfd ; //客户与服务器TCP接口
int driver_tcp_sockfd ; //司机与服务器TCP接口
};
void driver_info_show(struct INFO & );
void client_info_show(struct INFO & );
static int INFO_LEN = sizeof(struct INFO);
#endif
message_type.h
#ifndef _MESSAGE_TYPE_
#define _MESSAGE_TYPE_
#define MAXBUFF 1024
static bool pth;
struct message{
int type ;
size_t type_len ;
};
static int message = sizeof(struct message);
struct AP{
char account[1024]={0};
char password[1024]={0};
};
static void ap_show(AP &rh){
std::cout <<"========================================="<< std::endl;
std::cout << "account :" << rh.account<<std::endl;
std::cout << std::endl;
std::cout << "password :"<< rh.password<< std::endl;
std::cout << std::endl;
std::cout << "======================================="<<std::endl;
}
#endif