搭车系统 driver端

drivermain.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<map>
#include"message_type.h"
#include"driver.h"

using namespace std;
extern map<int , USERINFO> usermap;
int main(void)
{
    int i ;
    while(1){
        start_interface();

        cout << "输入你要选择的项目的数字";
        cin >> i ;
        getchar();
        switch(i){
            case 1 :
                {
                    registration_main();

                    }
                break ;         
            case 2 :
                {
                    login_main();
                    }
                break ; 
            case 0 :
                {
                    signd = true ;

                    }
                break ;
            default :
                break;
            }
            if(signd){
                break ;
                }
    }       
    return 0;
    }

driver.h

#ifndef  _DRIVER_H_
#define  _DRIVER_H_


#include<map>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include"message_type.h"

static AP loginap;
static bool signd ;
static char driveraccount[1024]={0}; 
static char useraccount[1024]={0};
static int  usernum=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]={0};
 };     

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 USERINFO{
    char account[1024]={0};
    struct sockaddr_in useraddr;
    struct ADDRESSINFO addressinfo;
    struct PERINFO perinfo;
    };

static void user_show(USERINFO &rh){
    std::cout <<"========================================="<< std::endl;
    std::cout << "account :" << rh.account<<std::endl;  
    std::cout << std::endl;
    std::cout << "name :"<< rh.perinfo.name<< std::endl;
    std::cout << std::endl;
    std::cout << "======================================="<<std::endl;
    }

struct CHATINFO{
    char recvaccount[1024]={0};
    char sendtoaccount[1024]={0};
    char buff[1024]={0};
    };

void registration_srcreen();
void  start_interface();
void info_srcreen();
int registration_main();
void login_main();
void add_show(ADDRESSINFO &);
void driver_GPS();
void view();
void select();
void user_info();
void* read_pthread(void *);
void chat_main();
#endif 

driver.h

#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<map>
#include"message_type.h"
#include"driver.h"

using namespace std;


map<int , USERINFO> usermap;

void registration_srcreen(){
    system("clear");
    cout <<"======================================"<<endl;
    cout << endl;
    cout <<"欢迎使用SF搭车司机客服端"<< endl;
    cout <<"请输入您的新帐号"<< endl;
    cout <<"和您的新密码 "<< endl;
    cout << endl;
    cout <<"======================================"<<endl;
    }

void  start_interface(){
    system("clear");
    cout <<"======================================"<<endl;
    cout << endl;
    cout <<"欢迎使用SF搭车司机客服端"<< endl;
    cout <<"1    注册"<< endl;
    cout <<"2    登录"<< endl;
    cout <<"0    退出"<< endl;
    cout << endl;
    cout << "======================================"<<endl;
    }


void info_srcreen(){        
    system("clear");
    cout << "注册成功"<< endl;
    cout << "=========================================="<<endl;
    cout << "请填写您的个人信息"<< endl;
    cout << endl ;
    cout << "1  姓名"<< endl;
    cout << endl;
    cout << "2  性别"<< endl;
    cout << endl;
    cout << "3  身份证号码"<< endl;
    cout << endl;
    cout << "4  家庭住址"<< endl;
    cout << endl;
    cout << "5  手机号码"<< endl;
    cout << endl;
    cout << "6  车牌号码"<< endl;
    cout << "============================================"<< endl;
    }


int registration_main(){
        registration_srcreen();
        struct AP ap;       
        while(1){
            cout<<"您的帐号:";
            cin>> ap.account ;
            cout <<"你输入的帐号为:"<<ap.account<<endl;
            cout <<"确认请按1  返回修改请按0  退出请按2"<<endl;
             int i;
             cin>>i;
            if(i == 1)
                break ;
            else if(i == 0)
                continue ;
            }
            getchar();
        while(1){
            cout<<"您的密码:";
            cin>> ap.password ;
            cout <<"你输入的密码为:"<<ap.password<<endl;
            cout <<"确认请按1  返回修改请按0"<<endl;
             int i;
             cin>>i;
            if(i)
                break ;
            else 
                continue ;
            }
        getchar();

        int ret ;
        int sockfd ;
        struct message  message;
        message.type = 1002;
        message.type_len = sizeof(struct AP);

        sockfd = socket(AF_INET , SOCK_STREAM , 0);

        struct sockaddr_in serveraddr;
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serveraddr.sin_port = htons(8000);

        ret = connect(sockfd , (const struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in) );
        if(ret  != 0){
            cout << "connect fail "<< endl;
            exit(1);
            }

        write(sockfd , &message, sizeof(message));

        ap_show(ap);

        write(sockfd , &ap , sizeof(struct AP));

        struct  message messagesignd ;
        read(sockfd  , &messagesignd ,sizeof(message) );

        if(messagesignd.type == 1111){

                PERINFO perinfo;    
                info_srcreen();
                while(1){
                    cout<<"您的姓名:";
                    cin>> perinfo.name;
                    cout <<"你的姓名为:"<<perinfo.name<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                  while(1){
                    cout<<"您的性别:";
                    cin>> perinfo.sex;
                    cout <<"性别为:"<<perinfo.sex<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                  while(1){

                    cout<<"您的身份证号码:";
                    cin>> perinfo.IDcard;
                    cout <<"身份证号码为"<<perinfo.IDcard<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                  while(1){

                    cout<<"您的住址:";
                    cin>> perinfo.homeadd;
                    cout <<"住址为:"<<perinfo.homeadd<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                  while(1){

                    cout<<"您的手机号码:";
                    cin>> perinfo.phonenum;
                    cout <<"您的手机号码:"<<perinfo.phonenum<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                   while(1){

                    cout<<"您的车牌号码:";
                    cin>> perinfo.carnum;
                    cout <<"您的车牌号码:"<<perinfo.carnum<<endl;
                    cout <<"确认请按1  返回修改请按0"<<endl;
                    int i;
                    cin>>i;
                    if(i)
                    break ;
                    else 
                    continue ;
                  }
                  getchar();

                  write(sockfd , &perinfo , sizeof (perinfo));          
            }
        else{
                cout << "注册失败  请重新选取帐号"<< endl;
            }
            getchar();
        return 0;
            }   


void login_srcreen(){
    system("clear");
    cout <<"======================================"<<endl;
    cout << endl;
    cout <<"欢迎使用SF搭车机客服端"<< endl;
    cout <<"请输入您的帐号"<< endl;
    cout <<"和您的 密码 "<< endl;
    cout << endl;
    cout <<"======================================"<<endl;
    }

void driver_srcreen(){
    system("clear");
    cout <<"======================================"<<endl;
    cout << endl;
    cout <<"欢迎您--"<<driveraccount<<"--使用SF搭车机客服端"<< endl;
    cout <<"1   上传GPS获取请求列表"<< endl;
    cout << endl;
    cout <<"2   查看"<< endl;
    cout << endl;
    cout <<"3   提交任务"<< endl;
    cout <<endl;
    cout <<"4   下载乘客相关信息"<< endl;
    cout << endl;
    cout <<"5   聊天"<< endl;
    cout << endl;
    cout <<"6   结束任务"<< endl;
    cout << endl;
    cout <<"0   退出"<< endl;
    cout << endl;
    cout << "======================================"<<endl;
    }





//登录
void login_main(){
    login_srcreen();        
        while(1){
            cout<<"您的帐号:";
            cin>> loginap.account ;
            cout <<"你输入的帐号为:"<<loginap.account<<endl;
            cout <<"确认请按1  返回修改请按0  退出请按2"<<endl;
             int i;
             cin>>i;
            if(i == 1)
                break ;
            else if(i == 0)
                continue ;
            }
            getchar();
        while(1){
            cout<<"您的密码:";
            cin>> loginap.password ;
            cout <<"你输入的密码为:"<<loginap.password<<endl;
            cout <<"确认请按1  返回修改请按0"<<endl;
             int i;
             cin>>i;
            if(i)
                break ;
            else 
                continue ;
            }
        getchar();

        int ret ;
        int sockfd ;
        struct message  message;
        message.type = 1003;
        message.type_len = sizeof(struct AP);

        sockfd = socket(AF_INET , SOCK_STREAM , 0);

        struct sockaddr_in serveraddr;
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serveraddr.sin_port = htons(8000);


        struct sockaddr_in driveraddr;
        driveraddr.sin_family = AF_INET;
        driveraddr.sin_addr.s_addr = inet_addr("192.168.1.161");
        driveraddr.sin_port = htons(8000);

        ret = connect(sockfd , (const struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in) );
        if(ret  != 0){
            cout << "connect fail "<< endl;
            exit(1);
            }

        write(sockfd , &message, sizeof(message));

        ap_show(loginap);

        write(sockfd , &loginap , sizeof(struct AP));

        struct  message messagesignd ;
        read(sockfd  , &messagesignd ,sizeof(message) );

        if(messagesignd.type == 1111){
            cout << "登录成功"<< endl;

            //保存帐号      
            strcpy(driveraccount , loginap.account);

            write(sockfd ,&driveraddr , sizeof(driveraddr));
            //聊天许可标志
            bool permitchat = false ;
            while(1){   
                driver_srcreen();

                signd = false ;
                int w;
                cin>> w;

                switch(w){
                    case 1 :
                        {
                        //上报GPS获取请求列表   
                        driver_GPS();
                        }
                        break;
                    case 2 :
                        {
                        //查看
                        view(); 
                        }
                        break ;
                    case 3 :
                        {
                        //提交任务  
                        select();   
                        }
                        getchar();
                        break ; 
                    case 4 :
                        {
                        cout <<"下载乘客相关信息"<<endl;

                        user_info();
                        }
                        getchar();
                        break ;
                    case 5 :
                        {
                        //聊天    
                        chat_main() ;   
                        }
                        getchar();
                        break ;
                    case 6 :
                        {
                        //结束任务  
                        }
                        break ;
                    case 0 :
                        cout << " 退出" << endl;
                        signd = true ;
                        break ; 
                }
                if(signd == true )
                break ; 
            }

    }else {
        cout << "登录失败 " << endl;
        }

        getchar();
    }

void driver_GPS(){
        int ret ;
        int sockfd ;

        sockfd = socket(AF_INET , SOCK_STREAM , 0);

        struct sockaddr_in serveraddr;
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serveraddr.sin_port = htons(8000);

        ret = connect(sockfd , (const struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in) );
        if(ret  != 0){
            cout << "connect fail "<< endl;
            exit(1);
            }
    struct message message ;
    struct ADDRESSINFO addressinfo;
    message.type = 1000;
    message.type_len = strlen(driveraccount);
    while(1){
        cout <<"====================================="<<endl;
        cout << endl;
        cout << "请输入您出发的位置"<< endl;
        cout << "开始经度";
        cin>> addressinfo.startjd ;
        getchar();
        cout << "开始纬度";
        cin>>addressinfo.startwd;
        getchar();
//      cout << "目的经度";
//      cin>>addressinfo.goaljd ;
//      getchar();
//      cout << "目的纬度" ;
//      cin>>addressinfo.goalwd; 
//      getchar(); 
        cout << "======================================"<<endl;
        add_show(addressinfo);

        cout <<"确认请按1  返回修改请按0"<<endl;
        int i;
        cin>>i;
        if(i)
            break ;
        else 
            continue ;  
    }
    getchar();

    write(sockfd, &message, sizeof(message));

    write(sockfd, &driveraccount , message.type_len );

    write(sockfd ,&addressinfo ,sizeof(ADDRESSINFO));
    read(sockfd , &message, sizeof(message));
    if(message.type == 1111){

        cout <<"提取成功,正在为您准备乘客列表...... "<<endl;
        int i=0;
        USERINFO userinfo;
        usermap.erase(usermap.begin(), usermap.end());
        while(1){

            ret = read(sockfd , &userinfo , sizeof(struct USERINFO));
            if(ret == 0)
                break ;

            usermap.insert(make_pair(++i , userinfo));

            }

            cout << "提取完毕"<< endl;  
        }else{
            cout <<" 提取未成功 ,请耐心等待" << endl ;    
            }

    getchar();
}

 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 select(){

    int ret ;
    struct message message;
    message.type = 1008;
    map<int , USERINFO>:: iterator its ;
    cout <<"请输入您选取的客户号码"<< endl;
    cin >>usernum;
    getchar();
    getchar();
    its = usermap.find(usernum);

    if( its != usermap.end()){

        strcpy(useraccount , (its->second).account);
        message.type_len = strlen(useraccount);


        int sockfd ;

        sockfd = socket(AF_INET , SOCK_STREAM , 0);

        struct sockaddr_in serveraddr;
        serveraddr.sin_family = AF_INET;
        serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        serveraddr.sin_port = htons(8000);


        ret = connect(sockfd , (const struct sockaddr*)&serveraddr , sizeof(struct sockaddr_in) );
        if(ret  != 0){
            cout << "connect fail "<< endl;
            exit(1);
            }

        write (sockfd ,&message, sizeof(message));

        write(sockfd, &useraccount ,strlen(useraccount));

        write(sockfd ,&driveraccount, sizeof(driveraccount));

        read(sockfd , &message , sizeof(struct message));
        if(message.type == 1111){
            cout << "获取任务 成功"<< endl;
          }else{
          cout << "获取任务失败"<< endl;
          }
    }else{
        cout << "输入选号 无效"<< endl;
    }
}

//查看
void view(){
    int i =0;
    int j= 1;
    bool chang = false;
    map<int ,USERINFO>::iterator its = usermap.begin();
    system("clear");
    cout <<"===================================="<< endl;
    cout << endl;
    cout <<"             编号"<<j<<endl;
    cout <<"乘客帐号:"<< (its->second).account<< endl;
    cout << endl;
    cout <<"乘客行程为:"<<endl;
    cout << "当前经纬度 :" <<(its->second).addressinfo.startjd<<":"
                           <<(its->second).addressinfo.startwd << endl;
    cout << "目的地经纬度 :" <<(its->second).addressinfo.goaljd<< ":"
                             <<(its->second).addressinfo.goalwd <<endl;
    cout << endl;
    cout << "======================================="<<endl;
    cout << endl;
    cout << "0 退出  1 查看下一个  2 查看上一个"<<endl;
    cout << endl;
    cout << "========================================"<< endl;
    cin>>i;
    getchar();
    while(1){
        switch(i){
            case 1: 
                {
                if( (++its != usermap.end() )){
                    ++j;
                    system("clear");
                    cout <<"===================================="<< endl;
                    cout << endl;
                    cout <<"             编号"<<j<<endl;
                    cout <<"乘客帐号:"<< (its->second).account<< endl;
                    cout << endl;
                    cout <<"乘客行程为:"<<endl;
                    cout << "当前经纬度 :" <<(its->second).addressinfo.startjd<<":"
                                           <<(its->second).addressinfo.startwd << endl;
                    cout << "目的地经纬度 :"<<(its->second).addressinfo.goaljd<< ":"
                                            <<(its->second).addressinfo.goalwd <<endl;
                    cout << endl;
                    cout << "======================================"<<endl;
                    }
                }
                getchar();
                break ;
            case 2:
                {
                    if((--its != usermap.end()) && (--j)){
                        system("clear");
                        cout <<"===================================="<< endl;
                        cout << endl;
                        cout <<"             编号"<<j<<endl;
                        cout <<"乘客帐号:"<< (its->second).account<< endl;
                        cout << endl;
                        cout <<"乘客行程为:"<<endl;
                        cout << "当前经纬度 :" <<(its->second).addressinfo.startjd<<":"
                                               <<(its->second).addressinfo.startwd << endl;
                        cout << "目的地经纬度 :"<<(its->second).addressinfo.goaljd<< ":"
                                                <<(its->second).addressinfo.goalwd <<endl;
                        cout << endl;
                        cout << "======================================"<<endl;
                        }   
                }
                getchar();
                break ;
            case 0:
                chang= true ;
                break ;
            default :
                break ; 
            }
            if(chang){
                break ;
                }

        cout << "======================================="<<endl;
        cout << endl;
        cout << "0 退出  1 查看下一个  2 查看上一个"<<endl;
        cout << endl;
        cout << "========================================"<< endl;
        cin>>i;
        getchar();          
        }       
}

void user_info(){
    cout << "user_info========================"<<endl;
    map<int ,USERINFO>::iterator its = usermap.find(usernum);
    if(its != usermap.end()){
        system("clear");
        cout <<"===================================="<< endl;
        cout << endl;
        cout <<"             编号"<<usernum<<endl;
        cout <<"乘客帐号:"<< (its->second).account<< endl;
        cout << endl;
        cout <<"乘客行程为:"<<endl;
        cout << "当前经纬度 :" <<(its->second).addressinfo.startjd<<":"
                               <<(its->second).addressinfo.startwd << endl;
        cout << "目的地经纬度 :"<<(its->second).addressinfo.goaljd<< ":"
                                <<(its->second).addressinfo.goalwd <<endl;
        cout << endl;
        cout << "======================================"<<endl;
        cout <<"乘客个人信息为:"<<endl;
        cout <<"姓名:"<<(its->second).perinfo.name<< endl;
        cout <<"性别:"<<(its->second).perinfo.sex<< endl;
        cout <<"证件:"<<(its->second).perinfo.IDcard<<endl;
        cout <<"住址:"<<(its->second).perinfo.homeadd<<endl;
        cout <<"联系:"<<(its->second).perinfo.phonenum<<endl; 
        }
        getchar();
    }


void* read_pthread(void *arg){
    int ret ;
    int sockfd ;
    struct CHATINFO chatinfo;
    sockfd = socket(AF_INET , SOCK_DGRAM , 0);

    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serveraddr.sin_port = htons(8000);  

    while(1){

            recv(sockfd , (void*)&chatinfo , sizeof(CHATINFO), 0);
            while(!strcmp(chatinfo.recvaccount, useraccount)){

                cout << "========================="<<endl;
                cout << endl;
                cout <<"收到"<<chatinfo.sendtoaccount <<"发来消息:"<< endl;
                cout << endl;
                cout <<"-->"<<chatinfo.buff<<endl;
                cout << "========================="<<endl;
                }

            }

    return (void*) 0;       
    }



void chat_main(){
    pthread_t  thread_t;
    pthread_create(&thread_t , NULL , read_pthread , (void*)0); 

    int ret ;
    int sockfd ;
    CHATINFO chat_info;
    strcpy(chat_info.sendtoaccount ,driveraccount) ;    
    sockfd = socket(AF_INET , SOCK_DGRAM , 0);

    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serveraddr.sin_port = htons(8000);

    while(1){
        int i=0;
        bool chang=false;
        system("clear");
        cout << "_________-----------____________"<<endl;
        cout << "1  写入消息    0 退出"<< endl;
        cout <<"----------___________------------"<<endl;
        cin>>i;
        switch(i){
            case 1 :
                {
                    cout << "你需要发送的给谁消息:";
                    cin>>chat_info.recvaccount;
                    cout << "消息内容:";
                    cin>>chat_info.buff;
                    getchar();
                    getchar();
                    sendto( sockfd , (void*)&chat_info ,(size_t)sizeof(CHATINFO), NULL,
                                            (struct sockaddr*)& serveraddr , (socklen_t)sizeof(serveraddr));
                    cout <<"发送完成"<<endl;                            
                }
                getchar();
                break ;
            case 0 :
                chang = true ;
                break ;
            default :
                break ;
            }
        if(chang){
            break ;
            }
        }

    }

message_type.h

#ifndef _MESSAGE_TYPE_
#define _MESSAGE_TYPE_

#define  MAXBUFF   1024

struct message{
    int type  ;
    size_t type_len ; 
};

static int  message_len = 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
posted @ 2016-03-27 22:58  夜色下的港湾  Views(213)  Comments(0Edit  收藏  举报