• 博客(0)
  • 资源 (1)

空空如也

TCP 服务器端聊天

基于服务器端聊天系统 //·þÎñÆ÷Ïß³Ì UINT thread(LPVOID p) { char buff[100]; CSize size; size.cx=0; size.cy=30; int s=1,msgcount,loop=1,flag=0; CCSocketDlg *dlg=(CCSocketDlg*)AfxGetApp()->GetMainWnd(); //»ñµÃ¿Í»§¶ËÊýÁ¿ msgcount=dlg->getcount(); if (msgcount==-1) loop=0; if(loop) { s=1; dlg->msgsock[msgcount]=accept(dlg->sock,(sockaddr*)&(dlg->serv),&(dlg->addlen)); if (dlg->msgsock[msgcount]==INVALID_SOCKET) { dlg->m_edit.SetWindowText("Error accept"); } else { //Æô¶¯Ïß³Ì AfxBeginThread(thread,0); dlg->SetForegroundWindow(); dlg->m_list.InsertItem(dlg->count++,"Á¬½Ó³É¹¦"); dlg->m_list.InsertItem(dlg->count++,inet_ntoa(dlg->serv.sin_addr)); dlg->m_list.Scroll(size); dlg->m_button.EnableWindow(TRUE); while(s!=SOCKET_ERROR) { //Ñ­»·½ÓÊÕÊý¾Ý s=recv(dlg->msgsock[msgcount],buff,100,0); dlg->SetForegroundWindow(); if (s!=SOCKET_ERROR) { dlg->m_list.InsertItem(dlg->count++,buff); dlg->m_list.Scroll(size); dlg->sendtoall(dlg->msgsock[msgcount],buff); } } send(dlg->msgsock[msgcount],"Disconnected",100,0); dlg->m_list.InsertItem(dlg->count++,"Disconnected"); dlg->m_list.Scroll(size); dlg->msgsock[msgcount]=NULL; for (int i=0;i<50;i++) if (dlg->msgsock[i]!=NULL) flag=1; if (flag!=1) dlg->m_button.EnableWindow(FALSE); closesocket(dlg->msgsock[msgcount]); } } //ÖÕÖ¹Ïß³Ì AfxEndThread(0); return 0; }

2013-06-18

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除