www.pudn.com > MFCl.rar > WSAAsyncSelectServerDlg.cpp


// WSAAsyncSelectServerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "WSAAsyncSelectServer.h" 
#include "WSAAsyncSelectServerDlg.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CWSAAsyncSelectServerDlg dialog 
 
CWSAAsyncSelectServerDlg::CWSAAsyncSelectServerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CWSAAsyncSelectServerDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CWSAAsyncSelectServerDlg) 
	m_sShowText = _T(""); 
	m_uPort = 0; 
	m_sInputText = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CWSAAsyncSelectServerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CWSAAsyncSelectServerDlg) 
	DDX_Control(pDX, IDC_SERVERIP, m_ServerIp); 
	DDX_Text(pDX, IDC_SHOWTEXT, m_sShowText); 
	DDX_Text(pDX, IDC_PORT, m_uPort); 
	DDX_Text(pDX, IDC_INPUTTEXT, m_sInputText); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CWSAAsyncSelectServerDlg, CDialog) 
	//{{AFX_MSG_MAP(CWSAAsyncSelectServerDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_MESSAGE(SER_MESSAGE,OnServerMessage) 
	ON_BN_CLICKED(IDC_SERVER, OnServer) 
	ON_BN_CLICKED(IDC_SEND, OnSend) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CWSAAsyncSelectServerDlg message handlers 
 
BOOL CWSAAsyncSelectServerDlg::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 
	GetDlgItem(IDC_SERVERIP)->SetWindowText("127.0.0.1"); 
	GetDlgItem(IDC_PORT)->SetWindowText("6000"); 
 
	m_connectionList.RemoveAll(); 
	ASSERT(m_connectionList.GetCount()==0); 
 
	m_Writeflag=FALSE; 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CWSAAsyncSelectServerDlg::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 CWSAAsyncSelectServerDlg::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 CWSAAsyncSelectServerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CWSAAsyncSelectServerDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
 
	CDialog::OnOK(); 
} 
 
BOOL CWSAAsyncSelectServerDlg::DestroyWindow()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
 
	SOCKET tempSocket; 
	int length=m_connectionList.GetCount(); 
	for(int i=0;im_hWnd,m_uPort)==FALSE) 
	{ 
		return; 
	} 
	else 
	{ 
		m_sShowText="服务器设置成功,准备通讯!\r\n"; 
		GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText); 
	} 
} 
 
void CWSAAsyncSelectServerDlg::OnSend()  
{ 
	// TODO: Add your control notification handler code here 
 
	 
	CString in; 
	GetDlgItem(IDC_INPUTTEXT)->GetWindowText(in); 
	 
	//消去无用的空格键字符 
	in.TrimRight(" "); 
	 
	if(in.GetLength()<1) 
	{ 
		AfxMessageBox("请输入你说的话!"); 
		return; 
	}	 
 
	//将输入框中的字符清空,表示字符已经被发送 
	GetDlgItem(IDC_INPUTTEXT)->SetWindowText(""); 
	 
 
	in="服务器说:"+in; 
	m_sShowText+=in; 
	m_sShowText+="\r\n"; 
 
	//将输入的话显示到显示窗口中 
	GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText);		 
	CEdit * output=(CEdit *)GetDlgItem(IDC_SHOWTEXT); 
	 
	//随时跟踪滚动条的位置 
	output->LineScroll(output->GetLineCount()); 
	 
 
	//如果当前用户是服务器端则向所有已经连接的客户端发送消息 
	int length=m_connectionList.GetCount(); 
	SOCKET  socket; 
	for(int i=0;iSetWindowText(m_sShowText); 
 
		output=(CEdit *)GetDlgItem(IDC_SHOWTEXT); 
		//随时跟踪滚动条的位置 
		output->LineScroll(output->GetLineCount()); 
 
		m_connectionList.AddHead(clientSocket); 
		break;		 
	case FD_READ: 
		length=m_connectionList.GetCount(); 
		for(i=0;iSetWindowText(m_sShowText); 
 
				//随时跟踪滚动条的位置 
				output=(CEdit *)GetDlgItem(IDC_SHOWTEXT);				 
				output->LineScroll(output->GetLineCount()); 
				return 0; 
			} 
		} 
		break;		 
	case FD_WRITE: 
		break;		 
		 
	case FD_CLOSE: 
		closesocket(wParam); 
		m_connectionList.RemoveAt(m_connectionList.Find(wParam)); 
		m_sShowText+="一个客户端的套接字连接被关闭!\r\n"; 
		GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText); 
		output=(CEdit *)GetDlgItem(IDC_SHOWTEXT); 
		//随时跟踪滚动条的位置 
		output->LineScroll(output->GetLineCount()); 
		break; 
		 
	default: 
		if (WSAECONNREFUSED==WSAGETSELECTERROR(lParam))  
		{ 
			m_sShowText=m_sShowText+"和一个客户端之间发生了一个网络错误!"; 
			GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText); 
			output=(CEdit *)GetDlgItem(IDC_SHOWTEXT); 
			//随时跟踪滚动条的位置 
			output->LineScroll(output->GetLineCount()); 
			 
			m_connectionList.RemoveAt(m_connectionList.Find(wParam)); 
			closesocket(wParam); 
		} 
		else 
		{ 
			output=(CEdit *)GetDlgItem(IDC_SHOWTEXT); 
			output->SetWindowText("未响应的事件!\r\n"); 
		} 
		break; 
	} 
	return 0; 
}