📄 clientprogramdlg.cpp
字号:
// ClientProgramDlg.cpp : implementation file
//
// Copyright (c) 2002
//
// Written by: Jeff Miles
// Email: jeffmiles99@hotmail.com
//
// Thank you to both Adam Clauss for his IniFile class, and
// Chris Maunder and Warren Young for their System Tray work.
//
// This program is the Client portion to a complete
// Client/Server framework. Feel free to use this framework
// as the basis for your network application. If you do use
// this framework, please let me know via e-mail.
//
// Since this program has not been extensively tested, expect bugs.
// Please e-mail me any suggestions or bugs that you find, or better
// yet, any modifications that you have made that adds to this programs
// functionality or efficiency.
//
// ***THIS VERSION FIXED THE FOLLOWING TWO BUGS IN VERSION 1.0***
// Known Bug: The Server Program does not compile in Debug mode.
// Use Release mode instead.
//
// Known Bug: The Server Program's Exit button does not function correctly.
// Use the "Shut Down Server" button instead.
//
#include "stdafx.h"
#include "ClientProgram.h"
#include "ClientProgramDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CClientProgramDlg dialog
CClientProgramDlg::CClientProgramDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientProgramDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientProgramDlg)
m_Name = _T("User");
m_IP = _T("127.0.0.1");
m_Port = _T("4306");
m_Password = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientProgramDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientProgramDlg)
DDX_Text(pDX, IDC_NAME, m_Name);
DDX_Text(pDX, IDC_IP, m_IP);
DDX_Text(pDX, IDC_PORT, m_Port);
DDX_Text(pDX, IDC_PASSWORD, m_Password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientProgramDlg, CDialog)
//{{AFX_MSG_MAP(CClientProgramDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, ConnectToServer)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_RECEIVE_MESSAGE,OnReceiveMessage)
ON_MESSAGE(WM_DISCONNECT,OnDisconnect)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientProgramDlg message handlers
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// The following block is Created by MFC and has not been modified at all.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
void CClientProgramDlg::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 CClientProgramDlg::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 CClientProgramDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// End of MFC Block ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
BOOL CClientProgramDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientProgramDlg::ConnectToServer()
{
//Gets the name, password, IP, and port number info from the dialog, as well as
//sets the Version number of this program to 1.0 (as you create new versions, just change this variable
UpdateData(TRUE);
version = "1.0";
name = m_Name;
password = m_Password;
IP = m_IP;
nPort = atoi(m_Port);
//Creates and readies the socket for connecting.
Sock.Create();
Sock.hParent = m_hWnd;
//Attempts to connect. If successful, it attempts to log on.
if (Sock.Connect(IP, nPort))
SendServerMessage("C"+name+"|"+version+"|"+password+"|");
else
AfxMessageBox("Unable to Connect to Server");
}
//Sends the Server a message. This function is used by the other functions,
//and you probably won't need to call it directly.
void CClientProgramDlg::GiveMessage(CString message)
{
long messageLength = message.GetLength();
//Sends the length of the message to the server
Sock.Send(&messageLength,4);
//Sends the actual message to the server
Sock.Send(message, messageLength);
}
//Sends the User who is on the Server's 'n' Socket a Message. In order to determine
//what socket a certain user is on, use the userList variable.
void CClientProgramDlg::SendUserMessage(CString message, int nSock)
{
char sock[10];
_itoa(nSock, sock, 10);
GiveMessage("A"+ CString(sock) + "|" + message);
}
//Sends all users who are connected a message.
void CClientProgramDlg::SendAllUsersMessage(CString message)
{
GiveMessage("B" + message);
}
//Sends all users who are connected a message except for the User who is on the Server's 'n' Socket
void CClientProgramDlg::SendAllUsersMessageExceptForOne(CString message, int nSock)
{
char sock[10];
_itoa(nSock, sock, 10);
GiveMessage("C" + CString(sock) + "|" + message);
}
//Sends a message who's final destination is the server itself.
void CClientProgramDlg::SendServerMessage(CString message)
{
GiveMessage("D" + message);
}
//This is called when a message arrives from the Server. This converts the message to a CString,
//and then passes this on to MessageReceive, which is where you should focus.
LRESULT CClientProgramDlg::OnReceiveMessage(WPARAM receiveMessage,LPARAM)
{
char *pMessage = (char *)receiveMessage;
CString message = pMessage;
MessageReceive(message);
return (0);
}
void CClientProgramDlg::MessageReceive(CString message)
{
if (message[0] == '1') // This version is not the most updated, server kicking us off.
{
Sock.Close();
AfxMessageBox("Sorry, the version of your program is not updated.");
}
else if (message[0] == '2') // Server allowing us to connect.
{
AfxMessageBox("Welcome to the Server.");
}
else if (message[0] == '3') // Server kicking us off due to incorrect password
{
Sock.Close();
AfxMessageBox("Sorry, the password you have entered is incorrect");
}
else if (message[0] == '4') // Server sending us the users list (who are currently online)
{
message.Delete(0);
CString userName;
CString userSocket;
int count=0;
do{
count++;
userName = ParseMessage(message,count);
count++;
userSocket = ParseMessage(message,count);
if (userName=="ERROR" || userSocket=="ERROR")
break;
userList.InsertItem(0,userName);
userList.SetItemText(0,1,userSocket);
} while (true);
}
// Add all of your message parsing here //
}
//This code helps to parse individual messages. See MessageReceive for how to use it.
CString CClientProgramDlg::ParseMessage(CString message, int nSlot)
{
int TempSize;
CString singleMessage;
for (int x=0;x<nSlot;x++)
{
TempSize=message.Find("|",0);
if (TempSize==-1)
return "ERROR";
singleMessage=message.Left(TempSize);
message=message.Mid(TempSize+1);
}
return singleMessage;
}
//Called when we lose the connection to the server, due to the Server being Shut down,
//or some other error.
LRESULT CClientProgramDlg::OnDisconnect(WPARAM,LPARAM)
{
AfxMessageBox("The connection to the Server has been closed.");
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -