问题描述
用socket实现电话本联系人管理程序的远程访问描述:在题目二的程序基础上,增加远程访问功能,采用客户端/服务器的方式。客户端:提供字符操作界面作为客户端;服务器:提供增删改查功能,提供文件存储功能。要求:必须在题目二的代码基础上来修改;必须使用Socket作为远程通信技术,可以使用更高层次的Socket封装类;下面是我写的题目二的代码,由于完全没有接触过socket,而且时间紧迫,明天就要验收了,迫不得已只好求高人帮忙,感激不尽。#include<iostream>#include<fstream>#include<map>#include<string>#include<exception>usingnamespacestd;classPhonetext{public:Phonetext(){}Phonetext(Phonetext&ph){phone=ph.phone;NAME=ph.NAME;NUM=ph.NUM;}voidFirst_Add();voidSearch();voidAdd();voiddeletenum();voidmodify();voidReadContacts();voidOutput();voidprintContacts();friendvoidoperator<<(ostream&os,constPhonetext&Ph);friendvoidoperator>>(istream&is,Phonetext&Ph);map<string,string>phone;stringNAME;stringNUM;};voidoperator<<(ostream&os,Phonetext&Ph)//重载操作符<<{map<string,string>::iteratorpos;for(pos=Ph.phone.begin();pos!=Ph.phone.end();++pos){os<<pos->first<<","<<pos->second<<"n";}return;//返回.}voidoperator>>(istream&is,Phonetext&Ph)//重载操作符>>{stringstrName,strNum;charch1;while((ch1=is.get())!=EOF){do{strName+=ch1;}while((ch1=is.get())!=',');while((ch1=is.get())!='n'){strNum+=ch1;}Ph.phone.insert(make_pair(strName,strNum));strName.clear();strNum.clear();}return;}classFileException//文件异常处理类{public:FileException():message("该指定文件不存在!"){}constchar*what()const{returnmessage;}private:constchar*message;};voidPhonetext::First_Add()//电话本信息初始化{phone.insert(make_pair("lisi","62282910"));phone.insert(make_pair("lili","62282911"));phone.insert(make_pair("mili","62282912"));phone.insert(make_pair("wangwu","62282913"));phone.insert(make_pair("zhangsan","62282914"));}voidPhonetext::ReadContacts(){ifstreamfile;file.open("phoneBook.txt");//打开自定义文件file>>*this;file.close();}voidPhonetext::Search(){if(phone.size()==0)cout<<"对不起,目前电话本为空!"<<endl;else{cout<<"请输入联系人的名字:"<<endl;cin>>NAME;if(phone.find(NAME)==phone.end())cout<<"该联系人不存在!"<<endl;elsecout<<"该联系人的电话号码是:"<<endl<<phone.find(NAME)->second<<endl;}}voidPhonetext::Add(){cout<<"请输入联系人的名字及其电话号码:"<<endl;cin>>NAME>>NUM;phone.insert(make_pair(NAME,NUM));cout<<"操作已成功!"<<endl;}voidPhonetext::deletenum(){if(phone.size()==0)cout<<"对不起,目前没有数据可以删除."<<endl;else{cout<<"请输入要删除的联系人的名字:"<<endl;cin>>NAME;if(phone.find(NAME)==phone.end())cout<<"该联系人不存在!"<<endl;else{phone.erase(NAME);cout<<"操作已成功!"<<endl;}}}voidPhonetext::modify(){if(phone.size()==0)cout<<"对不起,目前没有数据可以修改."<<endl;else{cout<<"请输入要修改的联系人的名字:"<<endl;cin>>NAME;if(phone.find(NAME)==phone.end())cout<<"该联系人不存在!"<<endl;else{cout<<"该联系人的号码是:";cout<<phone.find(NAME)->second<<endl;cout<<"修改联系人名字请按1,修改号码请按2:"<<endl;intch;cin>>ch;if(ch!=1&&ch!=2){cout<<"输入有错误!"<<endl;cin.get();}else{if(ch==1){cout<<"请输入该联系人新的名字:"<<endl;stringnewname,num;num=phone.find(NAME)->second;//记录下该联系人的号码cin>>newname;phone.erase(NAME);//删除原来的记录NAME=newname;NUM=num;//重新赋值phone.insert(make_pair(NAME,NUM));//重新插入记录}else{cout<<"请输入该联系人新的号码:"<<endl;stringname,newnum;name=NAME;cin>>newnum;phone.erase(NAME);//删除原来的记录NAME=name;NUM=newnum;//重新赋值phone.insert(make_pair(NAME,NUM));//重新插入记录}cout<<"操作已成功!"<<endl;}}}}voidPhonetext::printContacts(){map<string,string>::iteratorpos;for(pos=phone.begin();pos!=phone.end();++pos){cout<<pos->first<<","<<pos->second<<"n";}}voidPhonetext::Output(){ofstreamoFile;oFile.open("phoneBook.txt");oFile<<*this;oFile.close();}voidprint_information(){cout<<"退出对电话本的操作程序请按0:"<<endl;cout<<"查找电话本请按1:"<<endl;cout<<"添加姓名和电话号码请按2:"<<endl;cout<<"删除姓名和电话号码请按3:"<<endl;cout<<"修改姓名和电话号码请按4:"<<endl;}intmain(){PhonetextPh;boolbFileExist=true;//判断文件是否存在ifstreamfile;file.open("phoneBook.txt");//打开自定义文件try{if(!file){bFileExist=false;throwFileException();//抛出异常}else{file.close();}}catch(FileExceptionFE)//捕获异常{cout<<FE.what()<<endl;cout<<endl;}if(bFileExist==false){Ph.First_Add();//如果文件不存在,则初始化电话本信息}else{Ph.ReadContacts();//从文件中读取电话本信息}cout<<"电话本内已有的信息如下:"<<endl<<endl;cout<<"×××××××××××××××××××××××××××××"<<endl;Ph.printContacts();cout<<"×××××××××××××××××××××××××××××"<<endl<<endl;print_information();charch;while(cin>>ch,ch!='0'){if(ch!='1'&&ch!='2'&&ch!='3'&&ch!='4'){cout<<"输入有错误!"<<endl;cin.get();}else{switch(ch){case'1':Ph.Search();break;case'2':Ph.Add();break;case'3':Ph.deletenum();break;case'4':Ph.modify();break;}cout<<"请输入操作:"<<endl;}}Ph.Output();//增加、修改、删除联系人后向文件中保存更新后的数据system("pause");return0;}
解决方案
解决方案二:
求题目一的代码,后天要验收题目一!感激感激