-
大小: 263KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-26
- 语言: C/C++
- 标签:
资源简介
本实例,仿照qq,实现了点对对,1对多的聊天功能,具体解释见:http://blog.csdn.net/open520yin/article/details/8222279
相当适合新手学习,注释非常清楚。包含,服务端和客户端2套代码。
加载就可以运行成功。先运行服务端,再运行客户端。
代码片段和文件信息
// 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(“内存不足!“));
return;
}
memset(pBuff 0 sizeof(char)*head.nContentLen);
if(head.nContentLen!=Receive(pBuff head.nContentLen))
{
AfxMessageBox(_T(“收到数据有误!“));
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])
{
//生成协议头
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(“发送错误!“));
return FALSE;
};
if(Send(lpBuff nlen)==SOCKET_ERROR)
{
AfxMessageBox(_T(“发送错误!“));
return FALSE;
};
return TRUE;
}
//用户登陆
BOOL CClientSocket::LogoIn(LPSTR lpBuff int nlenchar from_user[20])
{
HEADER _head;
_head.type = MSG_LOGOIN; //头部类型
_head.nContentLen = nlen; //长度
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) //将头部发送过去
return false;
if((_nSnd = Send(lpBuff nlen))==SOCKET_ERROR) //头部内存空间,和长度发送过去
return false;
return TRUE;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 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个文件信息
- 上一篇:哈夫曼编码与译码附报告
- 下一篇:大恒相机动态库文件和头文件
评论
共有 条评论