聊天项目----界面完整版
//main_client.cpp#include"udp_client.h"udp_client_cl;udp_dataud;string_name;string_school;void*draw_header(void*arg){chat_window*_win=(chat_window*)arg;_win->create_header();string_line="HELLOWORLD";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;//colstringoutmsg;//sendmsg:jsoncppmsgstringshow_msg;//output:reseralizedmsgstring_name;string_msg;string_school;string_cmd;int_max_fly;//friends_listint_max_flx;getmaxyx(_win->friends_list,_max_fly,_max_flx);int_fly=1;int_flx=2;stringlist;//friendslistwhile(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<string>::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="pleaseinput#";int_max_y;int_max_x;getmaxyx(_win->input,_max_y,_max_x);string_msg;string_cmd=ud.cmd;string_out;//sendmsgjsoncppstringlist;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);//endingproc}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);}}//intmain()//{//chat_window_wi;//pthread_tth,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);//return0;//}intmain(){_cl.init();cout<<"pleaseinputname:";cin>>_name;cout<<"pleaseinputschool:";cin>>_school;//_name="rz";chat_window_wi;//pthread_tth,to,tf,ti;pthread_tth,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="";}//intmain()//{//udp_client_cl;//_cl.init();//udp_dataud;//string_name;//cout<<"pleaseinputname:";//cin>>_name;//string_msg;//cout<<"pleaseinputmsg:";//cin>>_msg;//string_school;//cout<<"pleaseinputschool:";//cin>>_school;//string_cmd;//cout<<"pleaseinputcmd:";//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<<endl;////cout<<_name;//_out="";//_cl.recv_data(_out);//cout<<_out<<endl;//ud.data_to_value(_name,_msg,_school,_cmd,_out);////cout<<"name:"<<_name<<endl;//cout<<"msg:"<<_msg<<endl;//cout<<"school:"<<_school<<endl;//cout<<"cmd:"<<_cmd<<endl;//while(1)//{//cout<<"pleaseinputmsg";//cin>>_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<<endl;//cout<<"msg:"<<_msg<<endl;//cout<<"school:"<<_school<<endl;//cout<<"cmd:"<<_cmd<<endl;//}//}//main_server.cpp#include"udp_server.h"voidUsage(constchar*proc){cout<<"Usage:"<<proc<<"[ip][port]"<<endl;}void*recv(void*arg){udp_server*_ser=(udp_server*)arg;while(1){_ser->recv_data();}returnNULL;}void*broad_cast(void*arg){udp_server*_ser=(udp_server*)arg;while(1){_ser->broadcast();}returnNULL;}intmain(intargc,char*argv[]){if(argc!=3){Usage(argv[0]);exit(0);}stringip=argv[1];shortport=atoi(argv[2]);udp_server_se(ip,port);_se.init();//daemon(0,0);pthread_ttid1;pthread_ttid2;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();//}//}return0;}//window目录下//chat_window.h#pragmaonce#include<iostream>#include<string.h>#include<string>#include<ncurses.h>#include<pthread.h>#include<unistd.h>#include"comm.h"//#define_SIZE_1024usingnamespacestd;classchat_window{public:chat_window();voidput_str_to_win(WINDOW*_win,int_y,int_x,conststring&msg);stringget_str_from_win(WINDOW*_win);voidcreate_header();voidcreate_output();voidcreate_friends_list();voidcreate_input();~chat_window();//WINDOW*create_newwin(int_h,int_w,int_y,int_x);voidfflush_win(WINDOW*_win);voidclear_win_line(WINDOW*_win,intbegin,intnum);public:WINDOW*header;WINDOW*output;WINDOW*friends_list;WINDOW*input;pthread_mutex_tmutex;};//chat_window.cpp#include"chat_window.h"chat_window::chat_window(){pthread_mutex_init(&mutex,NULL);initscr();//initscreencurs_set(0);//hidemousepointer}voidchat_window::put_str_to_win(WINDOW*_win,int_y,int_x,conststring&msg){mvwaddstr(_win,_y,_x,msg.c_str());//mvwindowaddstr:Cfunction}stringchat_window::get_str_from_win(WINDOW*_win){charbuf[_SIZE_];memset(buf,'\0',sizeof(buf));wgetnstr(_win,buf,sizeof(buf)-1);returnbuf;}voidchat_window::create_header(){int_y=0;//rowint_x=0;//colint_h=LINES/5;int_w=COLS;header=newwin(_h,_w,_y,_x);//createnewwindow}voidchat_window::create_output(){int_y=LINES/5;//rowint_x=0;//colint_h=(LINES*3)/5;int_w=(COLS*4)/5;output=newwin(_h,_w,_y,_x);//createnewwindow}voidchat_window::create_friends_list(){int_y=LINES/5;//rowint_x=(COLS*4)/5;//colint_h=(LINES*3)/5;int_w=COLS/5;friends_list=newwin(_h,_w,_y,_x);//createnewwindow}voidchat_window::create_input(){int_y=(LINES*4)/5;//rowint_x=0;//colint_h=LINES/5;int_w=COLS;input=newwin(_h,_w,_y,_x);//createnewwindow}chat_window::~chat_window(){pthread_mutex_destroy(&mutex);delwin(header);//destroywindowdelwin(output);//destroywindowdelwin(friends_list);//destroywindowdelwin(input);//destroywindowendwin();}//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;//}voidchat_window::fflush_win(WINDOW*_win){pthread_mutex_lock(&mutex);box(_win,0,0);wrefresh(_win);pthread_mutex_unlock(&mutex);}voidchat_window::clear_win_line(WINDOW*_win,intbegin,intnum){while(num-->0){wmove(_win,begin++,0);//0iscolwclrtoeol(_win);//windowcleartoeoflast}}//void*draw_header(void*arg)//{//chat_window*_win=(chat_window*)arg;//_win->create_header();//string_line="HELLOWORLD";//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="pleaseinput#";//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);//}//}//intmain()//{//chat_window_wi;//pthread_tth,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);//return0;//}//Makefilechat_window:chat_window.cppg++-o$@$^-lncurses-lpthread.PHONY:cleanclean:rm-fchat_window
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。