用MFC写一个聊天工具 要用到哪些方面的知识?

有服务端 我想知道应该怎样写服务器

就这些:

16.聊天室服务器端逻辑
一、服务器端所声明的类
class CCSocketDlg : public CDialog
{
// Construction
public:
CCSocketDlg(CWnd* pParent = NULL); // standard constructor
~CCSocketDlg();
// Dialog Data
//{{AFX_DATA(CCSocketDlg)
enum { IDD = IDD_CSOCKET_DIALOG };
CButton m_button;
CListCtrl m_list;
CEdit m_edit;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCSocketDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CCSocketDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
WSADATA wsaData;
SOCKET clisock;
SOCKET sListen, sAccept;
int addlen;
int count,s;
int getcount();
void sendtoall(SOCKET,char*);
struct sockaddr_in ser, cli; //服务器和客户的地址
int iLen; //客户地址长度
int iSend;//发送的数据长度
int flag;//标志位
char buf[1000];//要发送给客户的信息
void CRS();
};
UINT thread(LPVOID);
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CSOCKETDLG_H__2DFDFAF0_3473_43E6_A5CB_DBB8531B370E__INCLUDED_)
二、服务器端
// CSocketDlg.cpp : implementation file
//服务器端

#include "stdafx.h"
#include "CSocket.h"
#include "CSocketDlg.h"
#include <io.h>

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()

/////////////////////////////////////////////////////////////////////////////
// CCSocketDlg dialog

CCSocketDlg::CCSocketDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCSocketDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCSocketDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCSocketDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCSocketDlg)
DDX_Control(pDX, IDC_BUTTON1, m_button);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_EDIT1, m_edit);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCSocketDlg, CDialog)
//{{AFX_MSG_MAP(CCSocketDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCSocketDlg message handlers
//初始化对话框
BOOL CCSocketDlg::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
int count,s=1;
// char buff[100];
CDialog a;
CCSocketDlg *dlg=(CCSocketDlg*)AfxGetApp()->GetMainWnd();
count=0;
m_list.InsertColumn(0,"消息");
m_list.SetColumnWidth(0,435);
m_edit.SetLimitText(99);
dlg->sAccept=NULL;
//设定地址
dlg->ser.sin_addr.s_addr=htonl(INADDR_ANY);
dlg->ser.sin_family=AF_INET;
dlg->ser.sin_port=htons(5000);
addlen=sizeof(dlg->ser);
m_button.EnableWindow(FALSE);

//创建服务器端的套接口
dlg->sListen=socket(AF_INET,SOCK_STREAM,0);
if (dlg->sListen==INVALID_SOCKET)
{
m_edit.SetWindowText("创建套接口失败");
return FALSE;
}

//绑定
if (bind(dlg->sListen,(SOCKADDR*)&(dlg->ser),addlen))=SOCKET_ERROR)
{
closesocket(dlg->sListen);
m_edit.SetWindowText("绑定错误");
return FALSE;
}
else{
m_edit.SetWindowText("服务器创建成功");

//开始侦听
if (listen(dlg->sListen,5)==SOCKET_ERROR)
{
m_edit.SetWindowText("侦听失败");
return FALSE;
}

CRS();
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CCSocketDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
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 CCSocketDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// 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);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCSocketDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCSocketDlg::OnOK()
{
// CDialog::OnOK();
}
//发送数据
void CCSocketDlg::OnButton1()
{
char buff[100];
m_edit.GetWindowText(buff,99);
m_edit.SetWindowText("");
m_list.InsertItem(count++,buff);
//m_list.Scroll(size);
if (sAccept!=NULL)
//发送
send(sAccept,buff,100,0);
}
CCSocketDlg::~CCSocketDlg()
{
if (sAccept!=NULL)
send(sAccept,"Disconnected",100,0);
}
void CCSocketDlg::CRS()
{
char buff[100];
CCSocketDlg *dlg=(CCSocketDlg*)AfxGetApp()->GetMainWnd();
//初始化客户地址长度参数
iLen=sizeof(dlg->cli);
//进入一个无限循环,等待客户的连接请求
while(1)
{
dlg->sAccept=accept(dlg->sListen,(sockaddr*)&(dlg->ser),&(dlg->iLen));
if (dlg->sAccept==INVALID_SOCKET)
{ dlg->m_edit.SetWindowText("Error accept");}
dlg->m_list.InsertItem(dlg->count++,"连接成功");
char *ctime( const time_t *timer );
time_t ltime;
time(<ime);
dlg->m_list.InsertItem(dlg->count++,ctime( <ime ) );
s=recv(dlg->sAccept,buff,100,0);
dlg->SetForegroundWindow();
if (s!=SOCKET_ERROR)
{
dlg->m_list.InsertItem(dlg->count++,buff);
dlg->m_list.InsertItem(dlg->count++,ctime( <ime ) );

if (dlg->sAccept!=NULL)
//发送
send(dlg->sAccept,buff,100,0);
//dlg->sendtoall(dlg->sAccept,buff);
closesocket(dlg->sAccept);
}
}//end While
closesocket(dlg->sListen);
WSACleanup();
}

参考资料:百度文库:C++ Socket网络编程大全

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-02-20
socket
用户列表管理。
线程
界面
锐英源专业回答。
第2个回答  2010-02-19
winsock
相似回答