// ChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Chat.h"
#include "ChatDlg.h"
#include "ServerSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog
CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatDlg)
m_ListenPort = 0;
m_ServerPort = 0;
m_Name = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatDlg)
DDX_Control(pDX, IDC_SERVER_IP, m_ServerIP);
DDX_Control(pDX, IDC_INPUTTEXT, m_InputText);
DDX_Control(pDX, IDC_SHOWTEXT, m_ShowText);
DDX_Text(pDX, IDC_LISTEN_PORT, m_ListenPort);
DDV_MinMaxUInt(pDX, m_ListenPort, 0, 65535);
DDX_Text(pDX, IDC_SERVER_PORT, m_ServerPort);
DDV_MinMaxUInt(pDX, m_ServerPort, 0, 65535);
DDX_Text(pDX, IDC_NAME, m_Name);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
//{{AFX_MSG_MAP(CChatDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_CONNECT_CLOSE, OnConnectClose)
ON_BN_CLICKED(IDC_SERVER_CREATE, OnServerCreate)
ON_BN_CLICKED(IDC_SERVER_CLSOE, OnServerClsoe)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CONNECT, OnClientConnect)
ON_MESSAGE(WM_SERVER_CREATED, OnServerCreated)
ON_MESSAGE(WM_SERVER_CLOSED, OnServerClosed)
ON_MESSAGE(WM_CLIENT_CLOSE, OnClientClose)
ON_MESSAGE(WM_ERROR, OnServerCreateError)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers
BOOL CChatDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
m_ServerOnline = FALSE;
m_ClientOnline = FALSE;
m_bExit = FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CChatDlg::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 CChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatDlg::OnSend()
{
// TODO: Add your control notification handler code here
CString str;
m_InputText.GetWindowText(str);
if (str.IsEmpty())
{
AfxMessageBox("发送的内容不能为空!");
return;
}
m_InputText.SetWindowText("");
if (m_ServerOnline)
{
CString strSend("服务器消息: " + str);
for (int i = 0; i < m_ServerSocket.size(); i++)
{
m_ServerSocket[i]->Send(strSend, strSend.GetLength() + 1);
}
str = "服务器: " + str;
}
if (m_ClientOnline)
{
m_ClientSocket->Send(str, str.GetLength() + 1);
str = "我说: " + str;
}
ShowText(str);
}
void CChatDlg::OnConnect()
{
// TODO: Add your control notification handler code here
if (m_ServerIP.IsBlank())
{
MessageBox("请输入IP地址!", "提示", MB_OK | MB_ICONWARNING);
return;
}
DWORD dwAddress;
m_ServerIP.GetAddress(dwAddress);
UpdateData();
if (m_Name.IsEmpty())
{
char name[BufferSize];
gethostname(name, sizeof(name));
m_Name = name;
UpdateData(FALSE);
}
ShowText(CString("正在连接服务器, 请稍候..."));
SOCKADDR_IN SockAddr;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_addr = htonl(dwAddress);
SockAddr.sin_port = htons(m_ServerPort);
m_ClientSocket = new CClientSocket(this);
m_ClientSocket->Create();
m_ClientSocket->SetTimeOut(1000);
if (m_ClientSocket->Connect((SOCKADDR*)&SockAddr, sizeof(SockAddr)))
{
m_ClientSocket->KillTimeOut();
m_ClientOnline = TRUE;
m_ClientSocket->Send(m_Name, m_Name.GetLength() + 1);
EnableServer(FALSE, FALSE);
EnableClient(FALSE);
ShowText(CString("连接服务器成功!"));
}
else
{
m_ClientSocket->KillTimeOut();
m_ClientSocket->Close();
delete m_ClientSocket;
ShowText(CString("连接服务器失败!"));
}
}
void CChatDlg::OnConnectClose()
{
// TODO: Add your control notification handler code here
ConnectClose();
}
void CChatDlg::OnServerCreate()
{
// TODO: Add your control notification handler code here
UpdateData();
m_hListenThread = CreateThread(NULL, 0, ListenThread, this, NULL, NULL);
CloseHandle(m_hListenThread);
}
void CChatDlg::OnServerClsoe()
{
// TODO: Add your control notification handler code here
ServerClose();
}
void CChatDlg::ShowText(CString str)
{
str += "\r\n";
int nLength = m_ShowText.SendMessage(WM_GETTEXTLENGTH);
m_ShowText.SetSel(nLength, nLength);
m_ShowText.ReplaceSel(str);
}
void CChatDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_ShowText.SetWindowText("");
}
DWORD WINAPI CChatDlg::ListenThread(LPVOID p)
{
CChatDlg* pDlg = (CChatDlg*)p;
CSocket ListenSocket, AcceptSocket;
if (ListenSocket.Create(pDlg->m_ListenPort) == FALSE)
{
ListenSocket.Close();
::PostMessage(pDlg->m_hWnd, WM_ERROR, 1, 1);
return 0;
}
if (ListenSocket.Listen() == FALSE)
{
ListenSocket.Close();
::PostMessage(pDlg->m_hWnd, WM_ERROR, 1, 2);
return 0;
}
CString ip;
ListenSocket.GetSockName(ip, pDlg->m_ListenPort);
pDlg->m_ServerOnline = TRUE;
::PostMessage(pDlg->m_hWnd, WM_SERVER_CREATED, 0, 0);
while (pDlg->m_ServerOnline)
{
if (ListenSocket.Accept(AcceptSocket, NULL, NULL) == FALSE)
{
pDlg->m_ServerOnline = FALSE;
::PostMessage(pDlg->m_hWnd, WM_ERROR, 1, 3);
}
if (pDlg->m_ServerOnline == FALSE)
{
break;
}
CString strIP;
UINT nPort;
AcceptSocket.GetSockName(strIP, nPort);
::PostMessage(pDlg->m_hWnd, WM_CONNECT, (WPARAM)AcceptSocket.Detach(), 0);
AcceptSocket.Close();
}
ListenSocket.Close();
::PostMessage(pDlg->m_hWnd, WM_SERVER_CLOSED, 0, 0);
return 0;
}
void CChatDlg::OnClientConnect(SOCKET AcceptSocket, LPARAM lParam)
{
m_ServerSocket.push_back(new CServerSocket(this));
m_ServerSocket.back()->Attach(AcceptSocket);
}
void CChatDlg::OnServerCreated(WPARAM wParam, LPARAM lParam)
{
m_ServerOnline = TRUE;
UpdateData(FALSE);
EnableServer(FALSE);
EnableClient(FALSE, FALSE);
ShowText(CString("服务器建立成功!"));
hostent* pHostEnt;
char HostName[BufferSize];
gethostname(HostName, sizeof(HostName));
pHostEnt = gethostbyname(HostName);
CString name, ip;
IN_ADDR addr;
name.Format("服务器名字 1 : %s", pHostEnt->h_name);
ShowText(name);
int i;
for (i = 0; pHostEnt->h_aliases[i] != NULL; i++)
{
name.Format("服务器名字 %d : %s", i + 2, pHostEnt->h_aliases[i]);
ShowText(name);
}
for (i = 0; pHostEnt->h_addr_list[i] != NULL; i++)
{
addr.S_un.S_addr = *(u_long*)pHostEnt->h_addr_list[i];
ip.Format("服务器地址 %d : %s", i + 1, inet_ntoa(addr));
ShowText(ip);
}
}
void CChatDlg::OnServerCreateError(WPARAM wParam, LPARAM lParam)
{
s
- 1
- 2
- 3
- 4
- 5
- 6
前往页