// ServerDlg.cpp : implementation file
//#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialogCServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SHOWWINDOW()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
struct kk
{
SOCKET sock;
CServerDlg *thiss;
SOCKADDR_IN in;
};
BOOL CServerDlg::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

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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}DWORD WINAPI CServerDlg::ThreadProc(LPVOID lpParameter)
{
SOCKET sock=((kk*)lpParameter)->sock;
CServerDlg *thiss=((kk*)lpParameter)->thiss;
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"开始等待客户端....\r\n\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
SOCKADDR_IN in;
int nn=sizeof(SOCKADDR);
while(TRUE)
{
int n=accept(sock,(SOCKADDR*)&in,(int*)&nn);
if(n==INVALID_SOCKET)
{
memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"等待客户端错误!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
return 0;
}
else
{
char duankou[10];sprintf(duankou,"%d",in.sin_port);
char txt[100];memset(txt,0,100);strcat(txt,"IP为:");strcat(txt,inet_ntoa(in.sin_addr));strcat(txt,",端口为:");strcat(txt,duankou);
strcat(txt,"连接服务端!\r\n");
memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,txt);
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
kk *p=new kk;
p->sock=sock;
p->thiss=thiss;
p->in=in;
CreateThread(0,0,ThreadProcSub,p,0,0);
}
}
}DWORD WINAPI CServerDlg::ThreadProcSub(LPVOID a)
{
SOCKET sock=((kk*)a)->sock;
CServerDlg *thiss=((kk*)a)->thiss;
SOCKADDR_IN in=((kk*)a)->in;
while(TRUE)
{
char txt[100];
memset(txt,0,100);
int ns=recv(sock,txt,100,0);
if(ns==SOCKET_ERROR)
{
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);
strcat(ptxt,"IP为:");strcat(ptxt,inet_ntoa(in.sin_addr));
char duankou[10];sprintf(duankou,"%d",in.sin_port);strcat(ptxt,",端口为:");strcat(ptxt,duankou);strcat(ptxt,"的客户与服务器断开连接!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
return 0;
}
if(ns==0)
{
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);
strcat(ptxt,"IP为:");strcat(ptxt,inet_ntoa(in.sin_addr));
char duankou[10];sprintf(duankou,"%d",in.sin_port);strcat(ptxt,",端口为:");strcat(ptxt,duankou);strcat(ptxt,"的客户与服务器断开连接!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
return 0;
}
else
{
char user[10];char pass[10];memset(user,0,10);memset(pass,0,10);
int m=0;//当m=1表示开始录制pass
int z=0;
for(int n=0;n<ns;n++)
{
if(txt[n]=='|')
{
m=1;
z=0;
}
if(!m) //表示录制user
{
user[z]=txt[n];
z++;
}
if(m)
{
pass[z]=txt[n];
z++;
}
}
if(strcmp(user,"ServerDlg"))  //账号不对
{
send(sock,"error",5,0);
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"IP为:");
strcat(ptxt,inet_ntoa(in.sin_addr));
char duankou[10];sprintf(duankou,"%d",in.sin_port);
strcat(ptxt,",端口为:");strcat(ptxt,duankou);strcat(ptxt,"的客户端验证失败!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
}
else
{
if(!strcmp(pass,"ok"))
{
send(sock,"ok",2,0);
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"IP为:");
strcat(ptxt,inet_ntoa(in.sin_addr));
char duankou[10];sprintf(duankou,"%d",in.sin_port);
strcat(ptxt,",端口为:");strcat(ptxt,duankou);strcat(ptxt,"的客户端验证失败!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
}
else
{
send(sock,"error",5,0);
char ptxt[3000];memset(ptxt,0,3000);thiss->GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"IP为:");
strcat(ptxt,inet_ntoa(in.sin_addr));
char duankou[10];sprintf(duankou,"%d",in.sin_port);
strcat(ptxt,",端口为:");strcat(ptxt,duankou);strcat(ptxt,"的客户端验证成功!\r\n");
thiss->SetDlgItemText(IDC_EDIT1,ptxt);
}
}
}
}
}void CServerDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
CDialog::OnShowWindow(bShow, nStatus);

// TODO: Add your message handler code here
SOCKET sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==INVALID_SOCKET)
{
SetDlgItemText(IDC_EDIT1,"无法创建网络!");
WSACleanup();
}
else
{
char ptxt[3000];memset(ptxt,0,3000);GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"创建网络成功!\r\n");
SetDlgItemText(IDC_EDIT1,ptxt);
SOCKADDR_IN in;
in.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
in.sin_family=AF_INET;
in.sin_port=htons(999);
if(bind(sock,(SOCKADDR*)&in,sizeof(SOCKADDR))==SOCKET_ERROR)
{
memset(ptxt,0,3000);GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"端口被占用!\r\n");
SetDlgItemText(IDC_EDIT1,ptxt);
WSACleanup();
}
else
{
memset(ptxt,0,3000);GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"监听成功!\r\n");
SetDlgItemText(IDC_EDIT1,ptxt);
int o=listen(sock,100);
if(o==SOCKET_ERROR)
{
memset(ptxt,0,3000);GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"设置客户端连接数失败!\r\n");
SetDlgItemText(IDC_EDIT1,ptxt);
return;
}
memset(ptxt,0,3000);GetDlgItemText(IDC_EDIT1,ptxt,3000);strcat(ptxt,"设置客户端连接数成功!\r\n");
SetDlgItemText(IDC_EDIT1,ptxt);
kk *p=new kk;
p->sock=sock;
p->thiss=this;
CreateThread(0,0,ThreadProc,p,0,0);
}
}
}