实 验 报 告
课程名称:
网络编程
实验名称:
多点聊天程序
院 (系):
信息与控制工程学院
专业班级:
计算机 0901
姓 名:
张万福
学 号:
090620115
指导教师:
赵安军
2012 年 6 月 8 日
实验名称:多点聊天程序
一、实验目的
1.悉 VisualC++的基本操作;
2.基本了解基于对话框的 windows 应用程序的编写过程;
3.对于 Windows Socket 编程建立初步概念。
二、实验设备
需要装有 win7 操作系统 PC 机器一台,VC 开发环境。
三、实验内容
采用客户/服务器模式,分为客户端程序和服务器端程序。服务器采用
WINSOCK I/O 模型中的任一种,支持多个客户同时在线聊天。客户端程序
和服务器程序通过网络交换聊天字符串内容,服务器窗口的列表框中显示当
前在线用户,支持客户端之间的私聊。
四、实验结构及截图
1、服务器运行:
2、有客户端连接到服务器时:
五、多点聊天程序源代码
// tcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "tc.h"
#include "tcDlg.h"
#include "CSocket.h"
#include "Msg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);
// DDX/DDV
support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
}
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcDlg dialog
CTcDlg::CTcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTcDlg)
m_strCName = _T("");
m_strMsg = _T("");
m_strSName = _T("");
m_nPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pSocket=NULL;
m_pFile=NULL;
m_pArchiveIn=NULL;
m_pArchiveOut=NULL;
}
void CTcDlg::DoDataExchange(CDataExchange* pDX)
{
}
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTcDlg)
DDX_Control(pDX, IDOK, m_Send);
DDX_Control(pDX, IDC_BUTTON_CONN, m_btnConn);
DDX_Control(pDX, IDC_BUTTON_CLOSE, m_btnClose);
DDX_Control(pDX, IDC_LIST_MSG, m_listMsg);
DDX_Text(pDX, IDC_EDIT_CNAME, m_strCName);
DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
DDX_Text(pDX, IDC_EDIT_SNAME, m_strSName);
DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
//}}AFX_DATA_MAP
BEGIN_MESSAGE_MAP(CTcDlg, CDialog)
//{{AFX_MSG_MAP(CTcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_BN_CLICKED(IDC_BUTTON_CONN, OnButtonConn)
ON_BN_CLICKED(IDOK, OnSend)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcDlg message handlers
BOOL CTcDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX,
strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);
// Set big icon
SetIcon(m_hIcon, FALSE);
// Set small icon
// TODO: Add extra initialization here
m_strCName = "客户 1";
m_nPort = 8000;
m_strSName = _T("localhost");
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE);
UpdateData(FALSE);
return TRUE;
// return TRUE unless you set the focus to a control
}
void CTcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
}
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
}
else
{
}
CAboutDlg dlgAbout;
dlgAbout.DoModal();
CDialog::OnSysCommand(nID, lParam);
// If you add a minimize button to your dialog, you will need the code below
//
//
to draw the icon. For MFC applications using the document/view model,
this is automatically done for you by the framework.
void CTcDlg::OnPaint()
{
0);
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(),
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);