一个服务器对多个客户端的MFC Socket编程实现简单的即时通讯功能
标签:
•
文件类型: .rar
•
文件大小: 3.44MB
•
下载次数: 1
•
环境:Windows XP SP3、 VC++ 6.0、 Windows 2003 SDK
使用步骤:
1、下载解压之后,使用VC++ 6.0打开两个工程:一个是SocketServer和一个ClientSocket工程。
2、首先运行服务器端工程,选默认的端口1008
3、然后运行客户端工程,选默认的端口1008和默认的服务器地址
4、再运行多个客户端进程
5、如果一切正常,可以每个客户端的消息发送,我们可以在服务端和各个客户端同步看到消息
实现一个服务器对多个客户端的关键是,在服务端的使用集合CPtrList类用保存客户端的socket对象,思想与Java中的编程思想一样,只不过Java中会使用多线程技术,在Vector集合保存客户端的socket对象,而MFC框架提供了CSocket类,它是一个异步通信的类,所以看上去代码比较Java的多线程代码简单的实现了一个对多的即时通讯功能。另外,MFC提供了CSocketFile类和CArchive类与CSocket类实现了C++的网络通讯编程功能。
本示例注释非常详细,所有的辅助类都放一个util目录中,然后在工程中分了一个目录来管理这些辅助类,使用代码非常清晰。手动书写部分的代码是按Java的规范书写,当然其它代码由IDE生成的,所以是MS的风格,所以当你看代码时,只要是使用“骆驮命名法”的方法都是本人书写的功能性代码。
参看的思路:在服务端要从回调方法onAccept读起;而客户端代码主要从OnSendButton方法读起,即可理解整个代码的意思。
阅读对象:具有Java的Socket编程经验的人员,并且希望能够书写出比Java效率更高的即时通讯程序的人员
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1341 2009-11-06 22:07 SocketServerMyPort.cpp
文件 1315 2009-11-06 22:04 SocketServerMyPort.h
文件 3687 2009-11-06 21:41 SocketServerReadMe.txt
文件 6935 2009-11-11 20:28 SocketServerReleaseMessage.obj
文件 11404 2009-11-11 20:28 SocketServerReleaseMyPort.obj
文件 9004 2009-11-11 20:28 SocketServerReleasesocketclient.obj
文件 5931 2009-11-11 20:28 SocketServerReleasesocketlistener.obj
文件 28672 2009-11-11 20:28 SocketServerReleaseSocketServer.exe
文件 10637 2009-11-11 20:28 SocketServerReleaseSocketServer.obj
文件 5756388 2009-11-11 20:28 SocketServerReleaseSocketServer.pch
文件 3192 2009-11-11 20:28 SocketServerReleaseSocketServer.res
文件 33980 2009-11-11 20:28 SocketServerReleaseSocketServerDlg.obj
文件 810 2009-11-11 20:28 SocketServerReleaseStdAfx.obj
文件 58368 2009-11-11 20:28 SocketServerReleasevc60.idb
目录 0 2009-11-11 20:28 SocketServerRelease
文件 1078 2009-11-06 21:41 SocketServer
esSocketServer.ico
文件 404 2009-11-06 21:41 SocketServer
esSocketServer.rc2
目录 0 2009-11-09 21:44 SocketServer
es
文件 1102 2009-11-06 22:01 SocketServer
esource.h
文件 36956 2009-11-08 20:52 SocketServerSocketServer.aps
文件 2383 2009-11-09 16:55 SocketServerSocketServer.clw
文件 2243 2009-11-07 09:01 SocketServerSocketServer.cpp
文件 4820 2009-11-07 10:25 SocketServerSocketServer.dsp
文件 549 2009-11-06 21:41 SocketServerSocketServer.dsw
文件 1388 2009-11-07 09:00 SocketServerSocketServer.h
文件 82944 2009-11-11 20:37 SocketServerSocketServer.ncb
文件 61952 2009-11-11 20:37 SocketServerSocketServer.opt
文件 2349 2009-11-11 20:28 SocketServerSocketServer.plg
文件 6544 2009-11-08 20:52 SocketServerSocketServer.rc
文件 10665 2009-11-11 17:30 SocketServerSocketServerDlg.cpp
............此处省略55个文件信息
// ClientSocket.cpp : Defines the class behaviors for the application.
//
#include “stdafx.h“
#include “ClientSocket.h“
#include “ClientSocketDlg.h“
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocketApp
BEGIN_MESSAGE_MAP(CClientSocketApp CWinApp)
//{{AFX_MSG_MAP(CClientSocketApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP CWinApp::onhelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientSocketApp construction
CClientSocketApp::CClientSocketApp()
{
// TODO: add construction code here
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CClientSocketApp object
CClientSocketApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CClientSocketApp initialization
BOOL CClientSocketApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CClientSocketDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed return FALSE so that we exit the
// application rather than start the application‘s message pump.
return FALSE;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1341 2009-11-06 22:07 SocketServerMyPort.cpp
文件 1315 2009-11-06 22:04 SocketServerMyPort.h
文件 3687 2009-11-06 21:41 SocketServerReadMe.txt
文件 6935 2009-11-11 20:28 SocketServerReleaseMessage.obj
文件 11404 2009-11-11 20:28 SocketServerReleaseMyPort.obj
文件 9004 2009-11-11 20:28 SocketServerReleasesocketclient.obj
文件 5931 2009-11-11 20:28 SocketServerReleasesocketlistener.obj
文件 28672 2009-11-11 20:28 SocketServerReleaseSocketServer.exe
文件 10637 2009-11-11 20:28 SocketServerReleaseSocketServer.obj
文件 5756388 2009-11-11 20:28 SocketServerReleaseSocketServer.pch
文件 3192 2009-11-11 20:28 SocketServerReleaseSocketServer.res
文件 33980 2009-11-11 20:28 SocketServerReleaseSocketServerDlg.obj
文件 810 2009-11-11 20:28 SocketServerReleaseStdAfx.obj
文件 58368 2009-11-11 20:28 SocketServerReleasevc60.idb
目录 0 2009-11-11 20:28 SocketServerRelease
文件 1078 2009-11-06 21:41 SocketServer
esSocketServer.ico
文件 404 2009-11-06 21:41 SocketServer
esSocketServer.rc2
目录 0 2009-11-09 21:44 SocketServer
es
文件 1102 2009-11-06 22:01 SocketServer
esource.h
文件 36956 2009-11-08 20:52 SocketServerSocketServer.aps
文件 2383 2009-11-09 16:55 SocketServerSocketServer.clw
文件 2243 2009-11-07 09:01 SocketServerSocketServer.cpp
文件 4820 2009-11-07 10:25 SocketServerSocketServer.dsp
文件 549 2009-11-06 21:41 SocketServerSocketServer.dsw
文件 1388 2009-11-07 09:00 SocketServerSocketServer.h
文件 82944 2009-11-11 20:37 SocketServerSocketServer.ncb
文件 61952 2009-11-11 20:37 SocketServerSocketServer.opt
文件 2349 2009-11-11 20:28 SocketServerSocketServer.plg
文件 6544 2009-11-08 20:52 SocketServerSocketServer.rc
文件 10665 2009-11-11 17:30 SocketServerSocketServerDlg.cpp
............此处省略55个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)