搭车系统 use端

usemain.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"message_type.h"
#include"struct_info.h"
#include"use.h"


using namespace std;

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;
    }

use.h

static char  useraccount[1024]={0};
static bool signd ;
static char driveraccount[1024]={0};
static  int sockfd;
void registration_srcreen();
void start_interface();
int registration_main();
void login_main();
void login_srcreen();
void info_srcreen();
void call_cab();
void driver_info();
void chat_main();
void* read_pthread(void *);

static AP loginap;

static sockaddr_in  driveraddr;

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 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;
    };

void add_show(ADDRESSINFO &);   


struct USERINFO{
    char account[1024]={0};
    struct sockaddr_in useraddr;
    struct ADDRESSINFO addressinfo;
    struct PERINFO perinfo;
    };

static struct USERINFO driverinfo;

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};
    };

#endif

use.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"message_type.h"
#include"struct_info.h"
#include"use.h"

using namespace std;

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;
    }


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 = 2000;
        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();

                  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 user_srcreen(){
    system("clear");
    cout <<"======================================"<<endl;
    cout << endl;
    cout <<"欢迎您--"<<useraccount<<"--使用SF搭车"<< endl;
    cout <<"1  叫车"<< endl;
    cout << endl;
    cout <<"2  下载司机信息"<< endl;
    cout << endl;
    cout <<"3   聊天 "<< 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 = 2001;
        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 useraddr;
        useraddr.sin_family = AF_INET;
        useraddr.sin_addr.s_addr = inet_addr("192.168.1.161");
        cout << inet_addr("10.0.0.48")<< endl;
        useraddr.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(useraccount , loginap.account);

            write(sockfd ,&useraddr , sizeof(useraddr));
            //聊天许可标志
            bool permitchat = false ;
            while(1){   
                user_srcreen();
                signd = false ;
                int i ;
                cin >>i ;
                switch(i){
                    case 1 :
                        {
                        //叫车    
                        call_cab();
                        getchar();
                        }
                        break;
                    case 2 : 
                        {
                        //下载司机信息
                        driver_info();      
                        }
                        getchar();
                        getchar();
                        break ;
                    case 3 :
                        {
                        //聊天
                        chat_main();
                        }
                        break ;
                    case 0 :
                        cout << " 退出" << endl;
                        signd = true ;
                        break ; 
                }
                if(signd == true )
                break ; 
            }

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

void call_cab(){
         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);
            }
    //int ret ;
    struct message message ;
    struct ADDRESSINFO addressinfo;
    message.type = 2003;
    message.type_len = strlen(useraccount);
    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();
    getchar();


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

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

    write(sockfd ,&addressinfo ,sizeof(ADDRESSINFO));
    read(sockfd , &message, sizeof(message));
    if(message.type == 1111){
        cout <<" 叫车成功,正在为您准备司机...... "<<endl;
        read(sockfd , driveraccount, sizeof(driveraccount));
        system("clear");
        cout <<"========================================="<<endl;
        cout <<endl;
        cout << "您的专用司机:"<<driveraccount<<endl;
        cout <<endl;
        cout << "========================================="<<endl;  
    }else{
        cout <<" 叫车未成功 ,请耐心等待" << endl ;    
    }

}
 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_info(){

    if(strlen(driveraccount)){

        int ret ;
        int sockfd ;

        struct message message ;
        struct ADDRESSINFO addressinfo;
        message.type = 2005;
        message.type_len = strlen(driveraccount);   

        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 ,driveraccount, message.type_len );    
        read(sockfd , &driverinfo, sizeof( struct USERINFO));

        driveraddr = driverinfo.useraddr;

        system("clear");
        cout <<"===================================="<< endl;
        cout << endl;
        cout <<"司机帐号:"<< driveraccount<< endl;
        cout << endl;
        cout <<"司机位置:"<<endl;
        cout << "当前经纬度 :" <<driverinfo.addressinfo.startjd<<":"
                               <<driverinfo.addressinfo.startwd << endl;
        cout << endl;
        cout << "======================================"<<endl;
        cout <<"司机个人信息为:"<<endl;
        cout <<"姓名:"<<driverinfo.perinfo.name<< endl;
        cout <<"性别:"<<driverinfo.perinfo.sex<< endl;
        cout <<"证件:"<<driverinfo.perinfo.IDcard<<endl;
        cout <<"住址:"<<driverinfo.perinfo.homeadd<<endl;
        cout <<"联系:"<<driverinfo.perinfo.phonenum<<endl;
        cout <<"车牌:"<<driverinfo.perinfo.carnum<< endl; 
        cout <<"=========================================="<<endl;
        }else{  
        cout << "没有联系的司机"<< endl;   
        }
    }


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 ,useraccount) ;  
    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 ;
            }
        }

    }

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 

struct_info.h

#include<iostream>
#include"struct_info.h"

 using namespace std;

void client_info_show(INFO & rh){
    cout << "clientIP  :" << rh.clientIP<<endl;
    cout << "longitude :" << rh.longitude<<endl;
    cout << "datitude  :" << rh.datitude<<endl;
    cout << "ID     :" << rh.ID<< endl;
}


void driver_info_show(INFO & rh){
    cout << "driverIP  :" << rh.clientIP<<endl;
    cout << "longitude :" << rh.longitude<<endl;
    cout << "datitude  :" << rh.datitude<<endl;
    cout << "ID        :" << rh.ID << endl;
}

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 23:13  夜色下的港湾  Views(156)  Comments(0Edit  收藏  举报