-
大小: 263KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-26
- 語(yǔ)言: C/C++
- 標(biāo)簽:
資源簡(jiǎn)介
本實(shí)例,仿照qq,實(shí)現(xiàn)了點(diǎn)對(duì)對(duì),1對(duì)多的聊天功能,具體解釋見(jiàn):http://blog.csdn.net/open520yin/article/details/8222279
相當(dāng)適合新手學(xué)習(xí),注釋非常清楚。包含,服務(wù)端和客戶端2套代碼。
加載就可以運(yùn)行成功。先運(yùn)行服務(wù)端,再運(yùn)行客戶端。

代碼片段和文件信息
//?ClientSocket.cpp?:?implementation?file
//
#include?“stdafx.h“
#include?“NetChatClient.h“
#include?“ClientSocket.h“
#include?“Header.h“
#include?“NetChatClientDlg.h“
//?CClientSocket
CClientSocket::CClientSocket()
:m_strUserName(_T(““))
{
}
CClientSocket::~CClientSocket()
{
}
void?CClientSocket::OnReceive(int?nErrorCode)
{
//首先接受head頭
HEADER?head?;
char*?pHead?=?NULL;
pHead?=??new?char[sizeof(head)];
memset(pHead?0?sizeof(head));
Receive(pHead?sizeof(head));
head.type?=((LPHEADER)pHead)->type;
head.nContentLen?=?((LPHEADER)pHead)->nContentLen;
strcpy(head.from_user((LPHEADER)pHead)->from_user);
delete?pHead;
pHead?=?NULL;
char*?pBuff?=?NULL;
pBuff?=?new?char[head.nContentLen];
if(!pBuff)
{
AfxMessageBox(_T(“內(nèi)存不足!“));
return;
}
memset(pBuff?0??sizeof(char)*head.nContentLen);
if(head.nContentLen!=Receive(pBuff?head.nContentLen))
{
AfxMessageBox(_T(“收到數(shù)據(jù)有誤!“));
delete?pBuff;
return;
}
CString?strText(pBuff);
switch(head.type)
{
case?MSG_UPDATE:?
{
CString?strText(pBuff);
((CNetChatClientDlg*)(AfxGetApp()->GetMainWnd()))->UpdateUserInfo(strText);
}
break;
case?MSG_SEND:
{?
//顯示接收到的消息
CString?str(pBuff);
((CNetChatClientDlg*)(AfxGetApp()->GetMainWnd()))->UpdateText(str);
break;
}
default:?break;
}
delete?pBuff;
CSocket::OnReceive(nErrorCode);
}
BOOL?CClientSocket::SendMSG(LPSTR?lpBuff?int?nlenchar?to_user[20]char?from_user[20])
{
//生成協(xié)議頭
HEADER?head;
head.type?=?MSG_SEND;
head.nContentLen?=?nlen;
strcpy(head.to_userto_user);
strcpy(head.from_userfrom_user);
int?i?=Send(&head?sizeof(HEADER));
if(i==SOCKET_ERROR)
{
AfxMessageBox(_T(“發(fā)送錯(cuò)誤!“));
return?FALSE;
};
if(Send(lpBuff?nlen)==SOCKET_ERROR)
{
AfxMessageBox(_T(“發(fā)送錯(cuò)誤!“));
return?FALSE;
};
?
return??TRUE;
}
//用戶登陸
BOOL?CClientSocket::LogoIn(LPSTR?lpBuff?int?nlenchar?from_user[20])
{
HEADER?_head;
_head.type?=?MSG_LOGOIN;??//頭部類型
_head.nContentLen?=?nlen;?//長(zhǎng)度
memset(_head.to_user020);
memset(_head.from_user020);
strcpy(_head.from_userfrom_user);
//_head.to_user?=?““;
int?_nSnd=?0;
if((_nSnd?=?Send((char*)&_head?sizeof(_head)))==SOCKET_ERROR)??//將頭部發(fā)送過(guò)去
return?false;
if((_nSnd?=?Send(lpBuff?nlen))==SOCKET_ERROR)??//頭部?jī)?nèi)存空間,和長(zhǎng)度發(fā)送過(guò)去
return?false;
return?TRUE;
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????2467??2012-11-24?01:28??NetChatServer\NetChatClient\ClientSocket.cpp
?????文件????????385??2012-11-24?00:40??NetChatServer\NetChatClient\ClientSocket.h
?????文件????????669??2012-11-24?00:10??NetChatServer\NetChatClient\Header.h
?????文件???????2667??2012-11-25?16:08??NetChatServer\NetChatClient\LogoInDlg.cpp
?????文件????????562??2011-08-10?21:00??NetChatServer\NetChatClient\LogoInDlg.h
?????文件?????107264??2012-11-24?16:35??NetChatServer\NetChatClient\NetChatClient.aps
?????文件???????2792??2012-11-24?17:38??NetChatServer\NetChatClient\NetChatClient.cpp
?????文件????????591??2011-08-10?19:32??NetChatServer\NetChatClient\NetChatClient.h
?????文件??????12572??2012-11-24?16:35??NetChatServer\NetChatClient\NetChatClient.rc
?????文件???????6144??2012-11-25?02:11??NetChatServer\NetChatClient\NetChatClient.suo
?????文件???????6184??2012-11-19?19:16??NetChatServer\NetChatClient\NetChatClient.vcxproj
?????文件???????2545??2011-08-09?17:33??NetChatServer\NetChatClient\NetChatClient.vcxproj.filters
?????文件????????143??2011-08-09?14:34??NetChatServer\NetChatClient\NetChatClient.vcxproj.user
?????文件???????5884??2012-11-25?02:08??NetChatServer\NetChatClient\NetChatClientDlg.cpp
?????文件????????887??2012-11-24?17:01??NetChatServer\NetChatClient\NetChatClientDlg.h
?????文件???????3127??2011-08-09?14:34??NetChatServer\NetChatClient\ReadMe.txt
?????文件??????67777??2009-08-31?02:31??NetChatServer\NetChatClient\res\NetChatClient.ico
?????文件????????682??2011-08-09?14:34??NetChatServer\NetChatClient\res\NetChatClient.rc2
?????文件???????2196??2011-08-09?15:10??NetChatServer\NetChatClient\resource.h
?????文件????????146??2011-08-09?14:34??NetChatServer\NetChatClient\stdafx.cpp
?????文件???????1652??2011-08-09?15:52??NetChatServer\NetChatClient\stdafx.h
?????文件????????234??2011-08-09?14:34??NetChatServer\NetChatClient\targetver.h
?????文件???????6399??2012-11-25?15:52??NetChatServer\NetChatServer\ClientSocket.cpp
?????文件????????908??2012-11-24?00:25??NetChatServer\NetChatServer\ClientSocket.h
?????文件????????649??2012-11-24?00:16??NetChatServer\NetChatServer\Header.h
?????文件??????89976??2012-11-19?19:41??NetChatServer\NetChatServer\NetChatServer.aps
?????文件???????2565??2012-11-25?15:11??NetChatServer\NetChatServer\NetChatServer.cpp
?????文件????????629??2011-08-08?15:56??NetChatServer\NetChatServer\NetChatServer.h
?????文件??????10746??2011-08-11?11:30??NetChatServer\NetChatServer\NetChatServer.rc
?????文件???????6144??2012-11-25?15:52??NetChatServer\NetChatServer\NetChatServer.suo
............此處省略21個(gè)文件信息
評(píng)論
共有 條評(píng)論