类的实现:// Wn_4_6_1_serverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "wn_4_6_1_server.h"
#include "Wn_4_6_1_serverDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWn_4_6_1_serverDlg dialog
CWn_4_6_1_serverDlg::CWn_4_6_1_serverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWn_4_6_1_serverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWn_4_6_1_serverDlg)
//}}AFX_DATA_INIT
}
void CWn_4_6_1_serverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWn_4_6_1_serverDlg)
DDX_Control(pDX,IDC_EDIT4,host_name);
DDX_Control(pDX,IDC_EDIT3,client_data);
DDX_Control(pDX,IDC_EDIT2,server_port);
DDX_Control(pDX,IDC_EDIT1,server_ip);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWn_4_6_1_serverDlg,CDialog)
//{{AFX_MSG_MAP(CWn_4_6_1_serverDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1,OnBegin)
ON_BN_CLICKED(IDC_BUTTON2,OnClose)
ON_BN_CLICKED(IDC_BUTTON3,OnClear)
// NOTE: the ClassWizard will add message map macros here
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWn_4_6_1_serverDlg message handlers
void CWn_4_6_1_serverDlg::OnBegin()
{
WSADATA tvd;
WSAStartup(2,&tvd);
struct sockaddr_in dz,cdz;
dz.sin_family=2;
dz.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
dz.sin_port=2008;
int sersock=socket(2,1,0);
bind(sersock,(struct sockaddr*)&dz,sizeof(dz));
int ldz=sizeof(dz);
getsockname(sersock,(struct sockaddr*)&dz,(int*)ldz);
char pt[28],hstn[20];
CString s_ip=inet_ntoa(dz.sin_addr);
itoa(dz.sin_port,pt,sizeof(pt));
gethostname(hstn,sizeof(hstn));
server_ip.SetSel(0,-1);
server_ip.ReplaceSel(s_ip);
server_port.SetSel(0,-1);
server_port.ReplaceSel(pt);
host_name.SetSel(0,-1);
host_name.ReplaceSel(hstn);
int jt=listen(sersock,30);
int l=sizeof(cdz);
int rvs,sds;
char buff[80];
CString zfc;
zfc.Empty();
int ddr,dds,dd,ddl,zj=0;
int zs=1;
while(zs==1)
{
dd=1;
while(dd==1)
{
ddr=accept(sersock,(struct sockaddr*)&cdz,(int*)&l);
if(ddr>=0)
{
do
{
rvs=recv(ddr,buff,sizeof(buff),0);
if(rvs>0)
{
zfc=buff;
zfc=zfc.Left(rvs);
if(zfc=="bye")
zj=1;
client_data.ReplaceSel(zfc);
}
}while(rvs>0);
dd=0;
}
closesocket(ddr);
}
ddl=1;
while(ddl==1)
{
dds=accept(sersock,(struct sockaddr*)&cdz,(int*)&l);
if(dds>=0)
{
if(zj==0)
sds=send(dds,"OK!",sizeof("OK!"),0);
else
{
sds=send(dds,"bye!",sizeof("bye!"),0);
zs=0;
sds=-1;
}
ddl=0;
}
closesocket(dds);
WSACleanup();
}
}
}
void CWn_4_6_1_serverDlg::OnClose()
{
//TODO:add your control notification handler code here
OnOK();
}
void
CWn_4_6_1_serverDlg::OnClear()
{
//TODO:add your control notification handler code here
client_data.SetSel(0,-1);
client_data.Clear();
}