www.pudn.com > MFCl.rar > WSAAsyncSelectClientDlg.cpp
// WSAAsyncSelectClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WSAAsyncSelectClient.h"
#include "WSAAsyncSelectClientDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CWSAAsyncSelectClientDlg dialog
CWSAAsyncSelectClientDlg::CWSAAsyncSelectClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWSAAsyncSelectClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWSAAsyncSelectClientDlg)
m_sShowText = _T("");
m_uPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWSAAsyncSelectClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWSAAsyncSelectClientDlg)
DDX_Text(pDX, IDC_SHOWTEXT, m_sShowText);
DDX_Text(pDX, IDC_PORT, m_uPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWSAAsyncSelectClientDlg, CDialog)
//{{AFX_MSG_MAP(CWSAAsyncSelectClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(CLI_MESSAGE,OnClientMessage)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDCUT, OnCut)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWSAAsyncSelectClientDlg message handlers
BOOL CWSAAsyncSelectClientDlg::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");
GetDlgItem(IDC_NAME)->SetWindowText("Guest");
GetDlgItem(IDC_CONNECT)->EnableWindow(true);
GetDlgItem(IDCUT)->EnableWindow(false);
m_Writeflag=TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CWSAAsyncSelectClientDlg::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 CWSAAsyncSelectClientDlg::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 CWSAAsyncSelectClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWSAAsyncSelectClientDlg::OnOK()
{
// TODO: Add extra validation here
m_uPort=7000;
UpdateData(FALSE);//把变量数据更新到控件
CDialog::OnOK();
}
void CWSAAsyncSelectClientDlg::OnConnect()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_CONNECT)->EnableWindow(false);
GetDlgItem(IDCUT)->EnableWindow(true);
CString str="";
GetDlgItem(IDC_NAME)->GetWindowText(str);
if (str=="") {
AfxMessageBox("请输入你的名字后,重新连接!");
return;
}
str="该用户的名字是:"+str;
str=str+"\r\n";
CString strServerIP;
GetDlgItem(IDC_SERVERIP)->GetWindowText(strServerIP);
UpdateData(TRUE);//把控件中输入数更新到对应的变量
if(m_client.InitAndConnet(m_hWnd,m_uPort,strServerIP)==FALSE)
{
AfxMessageBox("连接错误!");
return;
}
if (m_Writeflag) {
int sendlen=m_client.SendString(str);
if (WSAEWOULDBLOCK ==sendlen) {
m_Writeflag=FALSE;
return;
}
}
}
void CWSAAsyncSelectClientDlg::OnCut()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDCUT)->EnableWindow(false);
GetDlgItem(IDC_CONNECT)->EnableWindow(true);
closesocket(m_client.m_sConnectSocket);
}
void CWSAAsyncSelectClientDlg::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("");
CString strName;
GetDlgItem(IDC_NAME)->GetWindowText(strName);
in=strName+"说:"+in;
m_sShowText+=in;
m_sShowText+="\r\n";
//将输入的话显示到显示窗口中
GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText);
CEdit * output=(CEdit *)GetDlgItem(IDC_SHOWTEXT);
//随时跟踪滚动条的位置
output->LineScroll(output->GetLineCount());
if (m_Writeflag) {
int sendlen=m_client.SendString(in);
if (WSAEWOULDBLOCK ==sendlen) {
m_Writeflag=FALSE;
return;
}
}
}
LRESULT CWSAAsyncSelectClientDlg::OnClientMessage(WPARAM wParam, LPARAM lParam)
{
CString str;
CEdit * output=NULL;
int recvlen;
char recvBuf[BUFFERLEN];//用户接收缓冲区
switch(lParam)
{
case FD_CONNECT:
if (WSAECONNREFUSED==WSAGETSELECTERROR(lParam)) {
AfxMessageBox("连接尝试被拒绝!");
::PostQuitMessage(0) ;
return 0 ;
}
else
{
m_Writeflag=TRUE;
m_sShowText="已经连接到服务器!\r\n";
GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText);
}
break;
case FD_READ:
recvlen=recv(m_client.m_sConnectSocket,recvBuf,sizeof(recvBuf),0);
if (SOCKET_ERROR==recvlen) {
AfxMessageBox("接收数据错误!");
}
recvBuf[recvlen]='\0';
m_sShowText+=recvBuf;
m_sShowText+="\r\n";
GetDlgItem(IDC_SHOWTEXT)->SetWindowText(m_sShowText);
//随时跟踪滚动条的位置
output=(CEdit *)GetDlgItem(IDC_SHOWTEXT);
output->LineScroll(output->GetLineCount());
break;
case FD_WRITE:
break;
case FD_CLOSE:
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))
{
output=(CEdit *)GetDlgItem(IDC_SHOWTEXT);
output->SetWindowText("有网络错误发生,此次连接被停止!\r\n");
closesocket(m_client.m_sConnectSocket);
}
else
{
output=(CEdit *)GetDlgItem(IDC_SHOWTEXT);
output->SetWindowText("未响应的事件!\r\n");
}
break;
}
return 0;
}