?? chatserverdlg.cpp
字號:
// chatserverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "chatserver.h"
#include "chatserverDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CChatserverDlg dialog
CChatserverDlg::CChatserverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatserverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatserverDlg)
m_msg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatserverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatserverDlg)
DDX_Text(pDX, IDC_EDIT1, m_msg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatserverDlg, CDialog)
//{{AFX_MSG_MAP(CChatserverDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnOpen)
ON_MESSAGE(WM_SERVER_ACCEPT, OnServerAccept)
ON_MESSAGE(WM_SOCKET_READ, OnServerreadorclose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatserverDlg message handlers
BOOL CChatserverDlg::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
for(int i=0;i<20;i++)
{
SocketAccept[i]=INVALID_SOCKET;
}
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatserverDlg::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 CChatserverDlg::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 CChatserverDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatserverDlg::OnOpen()
{ //建立Sockt對象
SockettoListen = socket(AF_INET, SOCK_STREAM, 0);
if (SockettoListen == INVALID_SOCKET)
{ MessageBox("創建socket失敗!");
return;
}
//為分配Sockt端口
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(4009);
WSAAsyncSelect(SockettoListen, m_hWnd, WM_SERVER_ACCEPT, FD_ACCEPT);
//bind
if(bind(SockettoListen,(LPSOCKADDR)&servaddr, sizeof(servaddr))== SOCKET_ERROR)
{
MessageBox("綁定socket失敗!");
return;
}
//listen
if (listen(SockettoListen, 20) == SOCKET_ERROR)
{
MessageBox("偵聽socket失敗!");
return;
}
m_msg="服務器已啟動!!!";
m_msg= m_msg+"\r\n";
m_msg=m_msg+"運行中等待服務請求.......";
UpdateData(FALSE);
}
//accept 請求人數為20
LRESULT CChatserverDlg::OnServerAccept(WPARAM wParam, LPARAM lParam)
{
int nLength = sizeof(SOCKADDR);
int i;
if (WSAGETSELECTEVENT(lParam) == FD_ACCEPT)
{
for(i=0;(i<20)&&(SocketAccept[i]!=INVALID_SOCKET);i++) ;
{
}
if(i==20)
return 0;
SocketAccept[i] = accept(SockettoListen, (LPSOCKADDR)&clientaddr, (LPINT)&nLength);
uinfo[i].userip=inet_ntoa(clientaddr.sin_addr);
WSAAsyncSelect(SocketAccept[i],m_hWnd,WM_SOCKET_READ,FD_READ|FD_CLOSE);
}
return 0;
}
LRESULT CChatserverDlg::OnServerreadorclose(WPARAM wParam, LPARAM lParam)
{
if (WSAGETSELECTEVENT(lParam) == FD_READ) //網絡數據到達事件
{
CString s;
int i;
for(i=0;(i<20)&&(SocketAccept[i]!=wParam);i++)
{
}
if(i==20) return 0;
int BRead;
sendinfo info;
BRead=recv(SocketAccept[i], (char*)&info,sizeof(info), 0);
if (BRead == SOCKET_ERROR)
MessageBox("接收到一個錯誤信息. ");
switch(info.type)
{
case 1:
int j;
for(j=0;j<20;j++)
{
if(SocketAccept[j]!=INVALID_SOCKET&&j!=i)
send(SocketAccept[j], (char*)&info,sizeof(info), 0);
}
break;
case 2:
int k;
for(k=0;(k<20)&&(uinfo[k].username!=info.name);k++)
{
}
send(SocketAccept[k], (char*)&info,sizeof(info), 0);
break;
case 0:
CTime t=CTime::GetCurrentTime();
CString strTime="(%y-%m-%d %H:%M:%S)";
strTime=t.Format(strTime);
uinfo[i].username=info.name;
s=info.name;
s=strTime+s+"進入聊天室";
LPCTSTR pq1=s;
memcpy(info.msg,pq1,s.GetLength()+1);
int j1;
for(j1=0;j1<20;j1++)
{
if(SocketAccept[j1]!=INVALID_SOCKET)
{send(SocketAccept[j1], (char*)&info,sizeof(info), 0);
}
}
int n;
for(n=0;n<20;n++)
{ if((uinfo[n].username!="")&&(n!=i))
{
sendinfo namelist;
namelist.type=0;
LPCTSTR nlist=uinfo[n].username;
CString nli;
nli="";
LPCTSTR nlis=nli;
memcpy(namelist.name,nlist,uinfo[n].username.GetLength()+1);
memcpy(namelist.msg,nlis,nli.GetLength()+1);
send(SocketAccept[i], (char*)&namelist,sizeof(namelist), 0);
}
}
m_msg=m_msg+"\r\n"+strTime+"用戶"+uinfo[i].username+"由ip"+uinfo[i].userip+"進入服務器";
UpdateData(FALSE);
break;
}
}
if (WSAGETSELECTEVENT(lParam) == FD_CLOSE) //客戶端斷開事件
{
CTime t=CTime::GetCurrentTime();
CString strTime="(%y-%m-%d %H:%M:%S)";
strTime=t.Format(strTime);
int i;
for(i=0;(i<20)&&(SocketAccept[i]!=wParam);i++) ;
closesocket(SocketAccept[i]);
SocketAccept[i] = INVALID_SOCKET;
sendinfo closeinfo,closeinfo1;
closeinfo.type=1;
closeinfo1.type=3;
CString sc;
sc= strTime+uinfo[i].username+"退出聊天室";
LPCTSTR pc=sc;
LPCTSTR pcname=uinfo[i].username;
memcpy(closeinfo.msg,pc,sc.GetLength()+1);
memcpy(closeinfo1.name,pcname,uinfo[i].username.GetLength()+1);
int jc;
for(jc=0;jc<20;jc++)
{
if(SocketAccept[jc]!=INVALID_SOCKET)
{
send(SocketAccept[jc], (char*)&closeinfo,sizeof(closeinfo), 0);
send(SocketAccept[jc], (char*)&closeinfo1,sizeof(closeinfo1), 0);
}
}
m_msg=m_msg+"\r\n"+strTime+"用戶"+uinfo[i].username+"由ip"+uinfo[i].userip+"退出服務器";
uinfo[i].userip="";
uinfo[i].username="";
UpdateData(FALSE);
}
CEdit *pEdit;
pEdit=(CEdit *)GetDlgItem(IDC_EDIT1);
int i=pEdit->GetLineCount();
pEdit->LineScroll(i,0);
return 0L;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -