基于winsock的局域网通信系统的开发(共37页).doc

上传人:飞****2 文档编号:9096001 上传时间:2022-03-30 格式:DOC 页数:37 大小:443.50KB
返回 下载 相关 举报
基于winsock的局域网通信系统的开发(共37页).doc_第1页
第1页 / 共37页
基于winsock的局域网通信系统的开发(共37页).doc_第2页
第2页 / 共37页
点击查看更多>>
资源描述

《基于winsock的局域网通信系统的开发(共37页).doc》由会员分享,可在线阅读,更多相关《基于winsock的局域网通信系统的开发(共37页).doc(37页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上重 庆 理 工 大 学课程设计课程 面向对象程序设计I 题目 基于WinSock的局域 网通信系统的开发 专心-专注-专业1 系统分析 1.1 模块一这个模块的功能是启动登录对话框,并且在启动后有登入,取消,申请帐号三个可以选择,若要登录则输入正确的帐号和密码即可,这是系统将传密码和帐号去验证是否正确,正确的话就可以登入到主对话框来(模块三),不正确的话会有重新输入的提示.如果选择申请帐号则会弹出申请帐号对话框(模块二).另外还可以点击取消(退出)按钮,则程序将关闭.1.2 模块二申请帐号对话框弹出后,用户要输入长度范围内的昵称,以及长度方位内的密码,并且第二次输入的

2、密码要与第一次输入的密码相同,如果不输入点确定也会报错,在确定完输入信息后,点击确定数据库将存放输入的信息,点击取消将回到登录界面.1.3 模块三这个模块中我做的是窗口界面,也就是接口.进入主界面后,可以有更改状态,发送消息,删除好友,查找好友,个人设置,好友消息,退出这几个选项.点击更改状态可以选择自己的状态,一共有三种可供选择,点击查找好友后会弹出一个选择查找方式的窗口,选择方式查到好友后可查看好友资料并加好友,接下来可以与好友聊天,但必须选中好友后再点击发送信息,否则将弹出提示语,这点在点击好友信息按钮和删除好友按钮时也是如此,必须选中后才能操作.另外可以点击个人设置对自己的资料进行填写

3、修改.点击退出会弹出一个确认退出对话框,再次确认后就将退出程序,点取消则返回主对话框.2 系统实现2.1模块一CP2PClientApp theApp;unsigned int CP2PClientApp:_seq_count = 0; /消息号计数器IM_Config_Parser g_fileobj(imclient.ini); /操作配置文件全局对象,配置文件名称imclient.iniMessage_Queue g_rcv_msg_queue;/全局接收消息队列Message_Queue g_send_msg_queue;/全局发送消息队列/全局udp通信对象Udp_Daemon g_

4、udp_daemon;#endif/从配置文件读取配置信息if(!g_fileobj.open()AfxMessageBox(打开配置文件imclient.ini 失败!);return 0;char strValueMAX_STRING_LEN;if(g_fileobj.get_string(im_appclient,im_server_ip,strValue,MAX_STRING_LEN)m_ServerInfo.server_ip = ntohl(inet_addr(strValue);/服务器ipif(g_fileobj.get_string(im_appclient,im_serve

5、r_udp_port,strValue,MAX_STRING_LEN)m_ServerInfo.server_port = atoi(strValue);/服务器端口号if(g_fileobj.get_string(im_appclient,recv_producer_num,strValue,MAX_STRING_LEN)m_ClientInfo.recv_producer_num = atoi(strValue);/接收消息线程数if(g_fileobj.get_string(im_appclient,send_consumer_num,strValue,MAX_STRING_LEN)m_

6、ClientInfo.send_consumer_num = atoi(strValue);/发送消息线程数if(g_fileobj.get_string(im_appclient,process_consumer_num,strValue,MAX_STRING_LEN)m_ClientInfo.process_consumer_num = atoi(strValue);/处理消息线程数if(g_fileobj.get_string(im_appclient,recent_ID,strValue,MAX_STRING_LEN)m_ClientInfo.uid = atoi(strValue);

7、/最近登录的用户帐号m_ClientInfo.client_port = create_client_port(); /获取客户端端口号void CLoginDlg:OnLogin() UpdateData(TRUE);/将界面内容传送给控件成员变量if(m_nUID write_header(header);/构造消息头char szPasswordMAX_PASSWORD_LEN = 0;strcpy(szPassword,m_strPassword.GetBuffer(0);m_strPassword.ReleaseBuffer();send_blk-write_buffer(szPas

8、sword,MAX_PASSWORD_LEN+1);send_blk-src_ip_ = theApp.m_ServerInfo.server_ip;/填充该消息的目的地send_blk-src_port_ = theApp.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CLoginDlg:OnApplyUid() CRegisterDlg dlg;/定义注册对话框对象if(dlg.DoMo

9、dal() = IDOK)/弹出注册对话框,如果用户在注册对话框中选择了确定按钮/向服务器发送注册帐号消息IM_Header header;header.cmd = IM_ACCOUNT_REGISTER;header.id = 0;/现在还没有用户号header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Block* send_blk = new IM_Message_Block;send_blk-write_header(header);/构造消息头char szNickMAX_N

10、ICK_LEN+1 = 0;strcpy(szNick,dlg.m_strNick.GetBuffer(0);dlg.m_strNick.ReleaseBuffer();send_blk-write_buffer(szNick,MAX_NICK_LEN+1);/char szPasswordMAX_PASSWORD_LEN+1 = 0;strcpy(szPassword,dlg.m_strPassword.GetBuffer(0);dlg.m_strPassword.ReleaseBuffer();send_blk-write_buffer(szPassword,MAX_PASSWORD_LE

11、N+1);send_blk-src_ip_ = theApp.m_ServerInfo.server_ip;/填充该消息的目的地send_blk-src_port_ = theApp.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CLoginDlg:OnCancel() CDialog:OnCancel(); /调用基类的OnCancel函数,关闭对话框,并返回IDCANCELLRESULT

12、 CLoginDlg:OnAccountRegister(WPARAM wParam, LPARAM lParam)IM_Message_Block* blk = (IM_Message_Block*)lParam;/转化传过来的参数if(!blk)m_nUID = 0;elseUINT32 uid = 0;blk-skip(sizeof(IM_Header);/跳过消息头blk-read_UINT8(&m_ErrorCode);/读取错误码blk-read_UINT32(&uid);/读取注册上的idm_nUID = uid;if(m_ErrorCode=IM_REGISTER_SUCESS

13、 & m_nUID!=0)CString strTemp;strTemp.Format(注册新帐号成功,新帐号为:%u,m_nUID);AfxMessageBox(strTemp);else if(m_ErrorCode=IM_REGISTER_ERROR | m_nUID=0)AfxMessageBox(CString(注册新帐号失败!);else if(m_ErrorCode = IM_REGISTER_ERROR_DB)AfxMessageBox(CString(注册帐号失败-数据库错误!);delete blk;UpdateData(FALSE);/将成员变量值传给界面GotoDlgC

14、trl(CWnd*)GetDlgItem(IDC_UID);/将输入焦点定位到id输入框return 1;LRESULT CLoginDlg:OnSessionLogin(WPARAM wParam, LPARAM lParam)IM_Message_Block* blk = (IM_Message_Block*)lParam;/转化传过来的参数if(blk)blk-skip(sizeof(IM_Header);/跳过消息头blk-read_UINT8(&m_ErrorCode);/读取错误码if(m_ErrorCode = IM_LOGIN_ERROR_PSW)AfxMessageBox(C

15、String(密码错误!);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_PASSWORD);return 0;else if(m_ErrorCode = IM_LOGIN_ERROR_UID_UNVALID)AfxMessageBox(CString(该用户帐号不存在!);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_UID);return 0;else if(m_ErrorCode = IM_LOGIN_ERROR_DB)AfxMessageBox(CString(数据库错误,登录失败!);GotoDlgCtrl(CWnd*)GetDlgItem(I

16、DC_UID);return 0;else if(m_ErrorCode = IM_LOGIN_SUCCESS)theApp.m_ClientInfo.uid = m_nUID;OnOK();/如果登录成功则调用基类的OnOK函数关闭对话框,并返回IDOKreturn 1;2.2模块二void CRegisterDlg:OnOK() UpdateData(TRUE); /从界面向控件变量传送数据if(m_strNick.IsEmpty() /昵称不能为空AfxMessageBox(CString(请输入昵称!); /提醒用户GotoDlgCtrl(CWnd*)GetDlgItem(IDC_NI

17、CK); /定位输入焦点return;if(m_strPassword.IsEmpty()AfxMessageBox(CString(请输入密码!);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_PASSWORD);return;if(m_strPasswordAgain.IsEmpty()AfxMessageBox(CString(请再次输入密码!);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_PASSWORD_AGAIN);return;if(m_strNick.GetLength() MAX_NICK_LEN) /昵称不能超长CString st

18、rTemp;strTemp.Format(昵称不能超过%d个字符!,MAX_NICK_LEN);AfxMessageBox(strTemp);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_NICK);return;if(m_strPassword.GetLength() MAX_PASSWORD_LEN) /密码不能超长CString strTemp;strTemp.Format(密码不能超过%d个字符!,MAX_PASSWORD_LEN);AfxMessageBox(strTemp);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_PASSWORD);r

19、eturn;if(m_strPassword != m_strPasswordAgain)AfxMessageBox(CString(两次密码输入不一致!);GotoDlgCtrl(CWnd*)GetDlgItem(IDC_PASSWORD);return;CDialog:OnOK();/调用基类的OnOk函数,关闭对话框,并返回IDOKvoid CRegisterDlg:OnCancel() / TODO: Add extra cleanup hereCDialog:OnCancel();2.3模块三void CP2PClientDlg:OnSelchangeOnlineStatus() i

20、nt nCurSel = m_ctrlOnlineStatus.GetCurSel();if(nCurSel!=theApp.m_ClientInfo.status) | (!m_bIsLogin)/开始发送改变在线状态信息IM_Header header;header.cmd = IM_SESSION_STATUS_CHANGE;header.id = theApp.m_ClientInfo.uid;header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Block* send_bl

21、k = new IM_Message_Block;send_blk-write_header(header);/构造消息头send_blk-write_UINT32(theApp.m_ClientInfo.uid);send_blk-write_UINT32(theApp.m_ClientInfo.client_ip);send_blk-write_UINT16(theApp.m_ClientInfo.client_port);if(nCurSel = 0)send_blk-write_UINT8(IM_STATUS_ONLINE);/本地记录的状态也作相应的改动theApp.m_Client

22、Info.status = IM_STATUS_ONLINE;else if(nCurSel = 1)send_blk-write_UINT8(IM_STATUS_HIDE);theApp.m_ClientInfo.status = IM_STATUS_HIDE;else if(nCurSel = 2)send_blk-write_UINT8(IM_STATUS_OUTWAY);theApp.m_ClientInfo.status = IM_STATUS_OUTWAY;else if(nCurSel = 3)send_blk-write_UINT8(IM_STATUS_OFFLINE);the

23、App.m_ClientInfo.status = IM_STATUS_OFFLINE;send_blk-src_ip_ = theApp.m_ServerInfo.server_ip;/填充该消息的目的地send_blk-src_port_ = theApp.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CP2PClientDlg:OnSendMsg() /查看是否选中了好友,如果选中了则

24、顺便取出跟他的聊天记录POSITION pos = m_ctrlFriendList.GetFirstSelectedItemPosition();if(!pos)AfxMessageBox(CString(请选择好友!);return;int nItem = m_ctrlFriendList.GetNextSelectedItem(pos);/界面上显示的新消息数恢复为m_ctrlFriendList.SetItemText(nItem,3,0);vector* history_msg = (vector*)m_ctrlFriendList.GetItemData(nItem);CStrin

25、g strValue = m_ctrlFriendList.GetItemText(nItem,0);unsigned int uid = atoi(strValue.GetBuffer(0);strValue.ReleaseBuffer();/查看该好友是否在线,如果在线顺便记住该好友的在线信息BOOL bIsOnline = FALSE;usr_info_online* friend_online_info = NULL;vector:iterator it = m_friend_online_list.begin();for(; it!=m_friend_online_list.end(

26、); it+)friend_online_info = (usr_info_online*)*it;if(friend_online_info-user_id = uid)if(friend_online_info-status=IM_STATUS_ONLINE | friend_online_info-status=IM_STATUS_OUTWAY)bIsOnline = TRUE;break;/弹出聊天对话框CSMsgDlg dlg;m_pChildWnd = &dlg;m_nChatFriendID = uid;/查找该好友的基本信息vector:iterator it_a = m_fr

27、iend_list.begin();for(; it_a!=m_friend_list.end(); it_a+)usr_info_base* friend_base_info = (usr_info_base*)*it_a;if(friend_base_info-user_id = uid)memcpy(char*)&dlg.m_friend_info,(char*)(friend_base_info),sizeof(usr_info_base);break;if(bIsOnline)/如果在线则将好友的在线信息拷贝给聊天对话框memcpy(char*)&dlg.m_friend_onlin

28、e,(char*)(friend_online_info),sizeof(usr_info_online);dlg.m_history_msg = history_msg;/将该好友的历史聊天记录传给聊天对话框dlg.m_bIsOffline = !bIsOnline;/将该好友是否在线传递给聊天对话框dlg.DoModal();/显示聊天对话框m_nChatFriendID = 0;/聊天结束后将记录当前聊天好友id变量重新置void CP2PClientDlg:OnDelFriend() POSITION pos = m_ctrlFriendList.GetFirstSelectedIte

29、mPosition();if(!pos)AfxMessageBox(CString(请选择好友!);return;int nItem = m_ctrlFriendList.GetNextSelectedItem(pos);CString strValue = m_ctrlFriendList.GetItemText(nItem,0);unsigned int uid = atoi(strValue.GetBuffer(0);strValue.ReleaseBuffer();/开始发送删除好友数据包IM_Header header;header.cmd = IM_CONTACT_DEL;head

30、er.id = theApp.m_ClientInfo.uid;header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Block* send_blk = new IM_Message_Block;send_blk-write_header(header);/构造消息头send_blk-write_UINT32(uid);send_blk-src_ip_ = theApp.m_ServerInfo.server_ip;/填充该消息的目的地send_blk-src_port_ = the

31、App.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CP2PClientDlg:OnFindContact() CSetSearchTypeDlg set_type_dlg;/定义设置查找类型对话框类对象if(set_type_dlg.DoModal() = IDOK)/弹出对话框,如果点击了确定按钮CQueryContactResultDlg result_dlg;/定义查找联系人结果对

32、话框类对象result_dlg.m_nUID = set_type_dlg.m_nUID;/传递查找类型信息result_dlg.m_strNick = set_type_dlg.m_strNick;result_dlg.m_nSearchType = set_type_dlg.m_nSearchType;m_pChildWnd = &result_dlg;/记录当前子对话框,便于后面转发消息result_dlg.DoModal();/显示查找联系人结果对话框void CP2PClientDlg:OnPersonalSet() CContactInfoDlg dlg;/定义联系人信息对话框类对

33、象dlg.m_nOpenMode = 1;/对话框打开模式为修改信息/将当前用户的详细信息传给联系人信息对话框中相应的成员变量dlg.m_nUID = m_myself_info.user_id;dlg.m_strNick = CString(m_myself_info.nick);dlg.m_strName = CString(m_myself_info.name);dlg.m_strEmail = CString(m_myself_info.email);dlg.m_strTelephone = CString(m_myself_info.telephone);dlg.m_strProfe

34、ssion = CString(m_myself_info.profession);dlg.m_strCollege = CString(m_myself_info.college);dlg.m_strNote = CString(m_myself_info.note);dlg.m_nGender = m_myself_info.gengder;dlg.m_nAge = m_myself_info.age;dlg.m_nPic = m_myself_info.picture;m_pChildWnd = &dlg;/记录弹出的子窗口句柄,方便转发消息dlg.DoModal();/弹出对话框voi

35、d CP2PClientDlg:OnCancel() CDialoge D_dlg; /定义一个Dialoge类对象,用于防止意外关闭if(D_dlg.DoModal()=IDOK)Quit();CDialog:OnCancel();void CP2PClientDlg:InitialInterface()/初始化界面/初始化界面m_ctrlFriendList.SetExtendedStyle (LVS_EX_FULLROWSELECT);/设置整行选中模式m_ctrlFriendList.InsertColumn(0,用户帐号,LVCFMT_CENTER,80);m_ctrlFriendL

36、ist.InsertColumn(1,昵称,LVCFMT_LEFT,80);m_ctrlFriendList.InsertColumn(2,状态,LVCFMT_LEFT,50);m_ctrlFriendList.InsertColumn(3,新消息数,LVCFMT_LEFT,80);m_ctrlOnlineStatus.AddString(上线);m_ctrlOnlineStatus.AddString(隐身);m_ctrlOnlineStatus.AddString(离开);m_ctrlOnlineStatus.SetCurSel(0);void CP2PClientDlg:GetFrien

37、dList()/发送获取好友列表消息IM_Header header;header.cmd = IM_CONTACT_GET_LIST;header.id = theApp.m_ClientInfo.uid;header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Block* send_blk = new IM_Message_Block;send_blk-write_header(header);/构造消息头send_blk-src_ip_ = theApp.m_ServerInfo

38、.server_ip;/填充该消息的目的地send_blk-src_port_ = theApp.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CP2PClientDlg:GetMySelfInfo()/发送获取自己信息的消息包IM_Header header;header.cmd = IM_CONTACT_SEARCH;header.id = theApp.m_ClientInfo.uid

39、;header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Block* send_blk = new IM_Message_Block;send_blk-write_header(header);/构造消息头send_blk-write_UINT8(IM_SEARCH_ID);send_blk-write_UINT32(theApp.m_ClientInfo.uid);send_blk-write_UINT16(0);/只取一次send_blk-src_ip_ = theApp.m_S

40、erverInfo.server_ip;/填充该消息的目的地send_blk-src_port_ = theApp.m_ServerInfo.server_port;send_blk-length_ = send_blk-get_cursor();/填充消息长度g_send_msg_queue.push_msg(send_blk);/将消息放入发送消息队列void CP2PClientDlg:GetFriendOnlineInfo()/发送获取好友在线信息消息vector:iterator it_a = m_friend_list.begin();for(; it_a!=m_friend_list.end(); it_a+)usr_info_base* temp = (usr_info_base*)*it_a;/开始发送改变在线状态信息IM_Header header;header.cmd = IM_GET_ONLINE_INFO;header.id = theApp.m_ClientInfo.uid;header.seq = CP2PClientApp:create_blk_seq();header.reserved = 0;/开始构造数据包IM_Message_Blo

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁