重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

聊天项目----界面完整版

//main_client.cpp
#include"udp_client.h"
udp_client _cl;
udp_data ud;
string _name;
string _school;
void* draw_header(void* arg)
{
    chat_window* _win=(chat_window*)arg;
    _win->create_header();
    string _line="HELLO WORLD";
    int _max_y;
    int _max_x;
    getmaxyx(_win->header,_max_y,_max_x);

    int _y=_max_y/2;
    int _x=1;
    while(1)
    {
        _win->put_str_to_win(_win->header,_y,_x,_line);
        _win->fflush_win(_win->header);
        usleep(10000);
        _x++;
        _x%=_max_x;
        _win->clear_win_line(_win->header,_y,2);
        _win->fflush_win(_win->header);
        usleep(10000);
    }

}
void* draw_output(void* arg)
{
    chat_window* _win=(chat_window*)arg;
    _win->create_output();
    _win->create_friends_list();

    int _max_y;
    int _max_x;
    getmaxyx(_win->output,_max_y,_max_x);
    int _y=1;
    int _x=3;//col
    string outmsg;//send msg:jsoncpp msg
    string show_msg;//output :reseralized msg
    string _name;
    string _msg;
    string _school;
    string _cmd;
    
    
    int _max_fly;//friends_list
    int _max_flx;
    getmaxyx(_win->friends_list,_max_fly,_max_flx);
    int _fly=1;
    int _flx=2;
    string list;//friends list

    while(1)
    {
        usleep(10000);
        _win->fflush_win(_win->output);
        _win->fflush_win(_win->friends_list);

        _cl.recv_data(outmsg);
        ud.data_to_value(_name,_msg,_school,_cmd,outmsg);

        
        show_msg=_name+"_"+_school+":"+_msg;
        list=_name+"-"+_school;
        
        _cl.add_fri_list(list);
        if(_cmd=="QUIT")
        {
            _cl.del_fri_list(list);
        }
        _win->put_str_to_win(_win->output,_y,_x,show_msg);
        _y++;
        if(_y==_max_y)
        {
            _win->clear_win_line(_win->output,1,_max_y-1);
            _y=1;
        }


    
        vector::iterator _iter=_cl.fri_list.begin();
        for(;_iter!=_cl.fri_list.end()&&_fly<_max_fly;++_iter)
        {
            _win->put_str_to_win(_win->friends_list,_fly++,_flx,*_iter);
        }
        _fly=1;

        usleep(10000);
        _win->fflush_win(_win->output);
        _win->fflush_win(_win->friends_list);
    }
}
//void* draw_friends_list(void* arg)
//{
//    chat_window* _win=(chat_window*)arg;
//    _win->create_friends_list();
//
//    int _max_y;
//    int _max_x;
//    getmaxyx(_win->header,_max_y,_max_x);
//
//    int _y=1;
//    int _x=1;
//    while(1)
//    {
//        usleep(1000000);
//        _y=1;
//        _win->fflush_win(_win->friends_list);
//
//        _win->clear_win_line(_win->friends_list,1,_y);
//    }
//}
void* draw_input(void* arg)
{
    chat_window* _win=(chat_window*)arg;
    _win->create_input();
    string _line="please input#";
    int _max_y;
    int _max_x;
    getmaxyx(_win->input,_max_y,_max_x);
    
    string _msg;
    string _cmd=ud.cmd;
    string _out;//send msg jsoncpp
    string list;
    while(1)
    {
        usleep(1000000);
        _win->put_str_to_win(_win->input,1,1,_line);
        _win->fflush_win(_win->input);
        _msg=_win->get_str_from_win(_win->input);
        if(_msg=="quit"||_msg=="q")
        {
            _cmd="QUIT";
            ud.data_to_string(_name,_msg,_school,_cmd,_out);
            _cl.send_data(_out);
            exit(0);//ending proc
        }
        ud.data_to_string(_name,_msg,_school,_cmd,_out);
        _cl.send_data(_out);
        
        _win->clear_win_line(_win->input,1,2);
        usleep(1000);
        _win->put_str_to_win(_win->input,1,1,_line);
        _win->fflush_win(_win->input);
    }
}
//int main()
//{
//    chat_window _wi;
//    pthread_t th,to,tf,ti;
//    pthread_create(&th,NULL,draw_header,(void*)&_wi);
//    pthread_create(&to,NULL,draw_output,(void*)&_wi);
//    pthread_create(&tf,NULL,draw_friends_list,(void*)&_wi);
//    pthread_create(&ti,NULL,draw_input,(void*)&_wi);
//    pthread_join(th,NULL);
//    pthread_join(to,NULL);
//    pthread_join(tf,NULL);
//    pthread_join(ti,NULL);
//    return 0;
//}


int main()
{
    _cl.init();
    cout<<"please input name: ";
    cin>>_name;
    cout<<"please input school: ";
    cin>>_school;

//    _name="rz";
    chat_window _wi;
//    pthread_t th,to,tf,ti;
    pthread_t th,to,ti;
    pthread_create(&th,NULL,draw_header,(void*)&_wi);
    pthread_create(&to,NULL,draw_output,(void*)&_wi);
//    pthread_create(&tf,NULL,draw_friends_list,(void*)&_wi);
    pthread_create(&ti,NULL,draw_input,(void*)&_wi);
    pthread_join(th,NULL);
    pthread_join(to,NULL);
//    pthread_join(tf,NULL);
    pthread_join(ti,NULL);
//    _name="";

}
//int main()
//{
//    udp_client _cl;
//    _cl.init();
//    udp_data ud;
//    string _name;
//    cout<<"please input name: ";
//    cin>>_name;
//    string _msg;
//    cout<<"please input msg: ";
//    cin>>_msg;
//    string _school;
//    cout<<"please input school: ";
//    cin>>_school;
//    string _cmd;
//    cout<<"please input cmd: ";
//    cin>>_cmd;
//    string _out;
////    _name="rz";
//    ud.data_to_string(_name,_msg,_school,_cmd,_out);
//    _cl.send_data(_out);
////    _name="";
////    ud.data_to_value(_name,_msg,_school,_cmd,_out);
////    cout<<_out<>_msg;
//        ud.data_to_string(_name,_msg,_school,_cmd,_out);
//
//        _cl.send_data(_out);
//        _out="";
//        _cl.recv_data(_out);
//        ud.data_to_value(_name,_msg,_school,_cmd,_out);
//
//        cout<<"name: "<<_name<recv_data();
    }
    return NULL;
}
void* broad_cast(void* arg)
{
    udp_server* _ser=(udp_server*)arg;
    while(1)
    {
        _ser->broadcast();
    }
    return NULL;
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(0);
    }
    string ip=argv[1];
    short port=atoi(argv[2]);
    udp_server _se(ip,port);
    _se.init();

//    daemon(0,0);
        
    pthread_t tid1;
    pthread_t tid2;
    pthread_create(&tid1,NULL,recv,(void*)&_se);
    pthread_create(&tid2,NULL,broad_cast,(void*)&_se);
    
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    //while(1)
    //{
    //    if(_se.recv_data()>0)
    //    {
    //        _se.broadcast();
    //    }
    //}
    return 0;
}

//window目录下
//chat_window.h
#pragma once
#include
#include
#include
#include
#include
#include
#include"comm.h"
//#define _SIZE_ 1024
using namespace std;
class chat_window
{
    public:
        chat_window();
        void put_str_to_win(WINDOW* _win,int _y,int _x,const string& msg);
        string get_str_from_win(WINDOW* _win);

        void create_header();
        void create_output();
        void create_friends_list();
        void create_input();
        ~chat_window();
//        WINDOW* create_newwin(int _h,int _w,int _y,int _x);
        void fflush_win(WINDOW* _win);
        void clear_win_line(WINDOW* _win,int begin,int num);
    public:
        WINDOW* header;
        WINDOW* output;
        WINDOW* friends_list;
        WINDOW* input;
        pthread_mutex_t mutex;
};
//chat_window.cpp
#include"chat_window.h"
chat_window::chat_window()
{
    pthread_mutex_init(&mutex,NULL);
    initscr();//init screen
    curs_set(0);//hide mouse pointer
}
void chat_window::put_str_to_win(WINDOW* _win,int _y,int _x,const string& msg)
{
    mvwaddstr(_win,_y,_x,msg.c_str());//mv window  add str :C function
}
string chat_window::get_str_from_win(WINDOW* _win)
{
    char buf[_SIZE_];
    memset(buf,'\0',sizeof(buf));
    wgetnstr(_win,buf,sizeof(buf)-1);
    return buf;
}
void chat_window::create_header()
{
    int _y=0;//row
    int _x=0;//col
    int _h=LINES/5;
    int _w=COLS;
    header=newwin(_h,_w,_y,_x);//create new window
}
void chat_window::create_output()
{
    int _y=LINES/5;//row
    int _x=0;//col
    int _h=(LINES*3)/5;
    int _w=(COLS*4)/5;
    output=newwin(_h,_w,_y,_x);//create new window
}
void chat_window::create_friends_list()
{
    int _y=LINES/5;//row
    int _x=(COLS*4)/5;//col
    int _h=(LINES*3)/5;
    int _w=COLS/5;
    friends_list=newwin(_h,_w,_y,_x);//create new window
}
void chat_window::create_input()
{
    int _y=(LINES*4)/5;//row
    int _x=0;//col
    int _h=LINES/5;
    int _w=COLS;
    input=newwin(_h,_w,_y,_x);//create new window
}
chat_window::~chat_window()
{
    pthread_mutex_destroy(&mutex);
    delwin(header);//destroy window
    delwin(output);//destroy window
    delwin(friends_list);//destroy window
    delwin(input);//destroy window
    endwin();
}
//WINDOW* chat_window::create_newwin(int _h,int _w,int _y,int _x)
//{
//    WINDOW* _win=newwin(_h,_w,_y,_x);
//    box(_win,0,0u);
//    return _win;
//}
void chat_window::fflush_win(WINDOW* _win)
{
    pthread_mutex_lock(&mutex);
    box(_win,0,0);
    wrefresh(_win);
    pthread_mutex_unlock(&mutex);
}
void chat_window::clear_win_line(WINDOW* _win,int begin,int num)
{
    while(num-- > 0)
    {
        wmove(_win,begin++,0);//0 is col
        wclrtoeol(_win);//window clear to eof last
    }
}
//void* draw_header(void* arg)
//{
//    chat_window* _win=(chat_window*)arg;
//    _win->create_header();
//    string _line="HELLO WORLD";
//    int _max_y;
//    int _max_x;
//    getmaxyx(_win->header,_max_y,_max_x);
//
//    int _y=_max_y/2;
//    int _x=1;
//    while(1)
//    {
//        _win->put_str_to_win(_win->header,_y,_x,_line);
//        _win->fflush_win(_win->header);
//        usleep(10000);
//        _x++;
//        _x%=_max_x;
//        _win->clear_win_line(_win->header,_y,2);
//        _win->fflush_win(_win->header);
//        usleep(10000);
//    }
//
//}
//void* draw_output(void* arg)
//{
//    chat_window* _win=(chat_window*)arg;
//    _win->create_output();
//    while(1)
//    {
//        usleep(1000000);
//        _win->fflush_win(_win->output);
//    }
//}
//void* draw_friends_list(void* arg)
//{
//    chat_window* _win=(chat_window*)arg;
//    _win->create_friends_list();
//    while(1)
//    {
//        usleep(1000000);
//        _win->fflush_win(_win->friends_list);
//    }
//}
//void* draw_input(void* arg)
//{
//    chat_window* _win=(chat_window*)arg;
//    _win->create_input();
//    string _line="please input#";
//    int _max_y;
//    int _max_x;
//    getmaxyx(_win->input,_max_y,_max_x);
//    string _msg;
//    while(1)
//    {
//        usleep(1000000);
//        _win->put_str_to_win(_win->input,1,1,_line);
//        _win->fflush_win(_win->input);
//        _msg=_win->get_str_from_win(_win->input);
//        _win->clear_win_line(_win->input,1,1);
//        usleep(1000);
//        _win->put_str_to_win(_win->input,1,1,_line);
//        _win->fflush_win(_win->input);
//    }
//}
//int main()
//{
//    chat_window _wi;
//    pthread_t th,to,tf,ti;
//    pthread_create(&th,NULL,draw_header,(void*)&_wi);
//    pthread_create(&to,NULL,draw_output,(void*)&_wi);
//    pthread_create(&tf,NULL,draw_friends_list,(void*)&_wi);
//    pthread_create(&ti,NULL,draw_input,(void*)&_wi);
//    pthread_join(th,NULL);
//    pthread_join(to,NULL);
//    pthread_join(tf,NULL);
//    pthread_join(ti,NULL);
//    return 0;
//}
//Makefile
chat_window:chat_window.cpp
    g++ -o $@ $^ -lncurses -lpthread


.PHONY:clean
clean:
    rm -f chat_window

分享题目:聊天项目----界面完整版
分享地址:http://cqcxhl.com/article/ieiehg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP