⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientsocket.cpp

📁 这源代码是《白领就业指南:visual c++ 6.0 设计师之路》的配套源代码
💻 CPP
字号:
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "ChatRoomServer.h"
#include "ClientSocket.h"
#include "ClientSocketList.h" //add 
#include "ChatRoomServerDlg.h" //add 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{  myDlg=0;
}

CClientSocket::~CClientSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
CClientSocket::CClientSocket(CClientSocketList *tmp)
{
	Front=0;
	Next=0;
	List=tmp;
}
void CClientSocket::OnAccept(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class    
 	CSocket::OnAccept(nErrorCode);
}

void CClientSocket::OnClose(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class	
	CSocket::OnClose(nErrorCode);
}

void CClientSocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	char buff[1000];
	char all_user[2000];
	char name[20];
    int n,m; 
    n=this->Receive(buff,1000);
    buff[n]=0;
	char Flag[10];
	for(int i=0;i<8;i++) Flag[i]=buff[i];
	Flag[8]=0;
	//下面两行代码用来获取对话框指针 
    CChatRoomServerApp* pApp=(CChatRoomServerApp*)AfxGetApp(); 
    CChatRoomServerDlg * pDlg=(CChatRoomServerDlg *)pApp->m_pMainWnd;   
	if(strcmp(Flag,"NEW_USER")==0)//新用户加入
    {
		CString sTemp;
		for(i=9;buff[i]!=0;i++)
			buff[i-9]=buff[i];
        buff[i-9]=0;
	    sTemp.Format("%s",buff);//新用户昵称sTemp
	    pDlg->m_USER.AddString(sTemp);
        //新的在线用户昵称名单发给所有用户	   
	    m=pDlg->m_USER.GetCount();
	    strcpy(all_user,"USERLIST");
		for(i=0;i<m;i++)
		{
		 pDlg->m_USER.GetText(i,name);
		 strcat(all_user,name);
		 strcat(all_user,"|");		 
		}
        strcat(buff,"进入聊天室");
        //List->Sends(this,buff,strlen(buff));  
		List->Sends(all_user,strlen(all_user)); 
	}
	else
        if(strcmp(Flag,"CLOSEUSE")==0)//用户退出
		{
		CString sTemp;
		for(i=9;buff[i]!=0;i++)
			buff[i-9]=buff[i];
        buff[i-9]=0;
	    sTemp.Format("%s",buff);//离线用户昵称
   	    m=pDlg->m_USER.GetCount();
        for(i=0;i<m;i++)//查找离线用户在列表框的索引号
		{
		 pDlg->m_USER.GetText(i,name);
		 if (strcmp(name,buff)==0)
		 pDlg->m_USER.DeleteString(i);
		}
		 //新的在线用户昵称名单发给所有用户	   
	    m=pDlg->m_USER.GetCount();
	    strcpy(all_user,"USERLIST");
		for(i=0;i<m;i++)
		{
		 pDlg->m_USER.GetText(i,name);
		 strcat(all_user,name);
		 strcat(all_user,"|");		 
		}
		strcat(buff,"离开聊天室");
		List->Sends(all_user,strlen(all_user)); 
		List->Del(this); 
        //List->Sends(this,buff,strlen(buff));  
		}
	    else //****	
        if(strcmp(Flag,"PrivChat")==0)//私聊
		{
		CString sTemp;
		for(i=9;buff[i]!='|';i++)
			buff[i-9]=buff[i];
        buff[i-9]=0;
		sTemp.Format("%s",buff);//私聊对象昵称
        for(int k=i+1;buff[k]!=0;k++)
			buff[k-i-1]=buff[k];
        buff[k-i-1]=0;
   	    m=pDlg->m_USER.GetCount();
        for(i=0;i<m;i++)//查找离线用户在列表框的索引号
		{
		 pDlg->m_USER.GetText(i,name);
		 if (strcmp(name,sTemp)==0)
		 {
		   List->OnlySend(buff,strlen(buff),i); 	 
		 }
		} 
		
		}
		else	//聊天信息群发给所有用户
		{
		List->Sends(buff,n);  
		}
	//Receive只能接受一次,除非下次发送触发后
	CString sTemp;
	sTemp.Format("收到:%s",buff);
	pDlg->m_ChatList.AddString(sTemp);
	pDlg->m_ChatList.SetTopIndex(pDlg->m_ChatList.GetCount()-1);
	CSocket::OnReceive(nErrorCode);
}

BOOL CClientSocket::SetDlg(CChatRoomServerDlg *tmp)
{

	myDlg=tmp;//// 用来获取对话框指针
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -