📄 echoserverdlg.cpp
字号:
// EchoSerVerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EchoSerVer.h"
#include "EchoSerVerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include<process.h>
SOCKET connected[CLIENT];
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CEchoSerVerDlg dialog
CEchoSerVerDlg::CEchoSerVerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEchoSerVerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEchoSerVerDlg)
m_port = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEchoSerVerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEchoSerVerDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_PORT, m_port);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEchoSerVerDlg, CDialog)
//{{AFX_MSG_MAP(CEchoSerVerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_STARTUP, OnStartup)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEchoSerVerDlg message handlers
BOOL CEchoSerVerDlg::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
StarUp();
m_port=5050;
UpdateData(FALSE);
num=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CEchoSerVerDlg::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 CEchoSerVerDlg::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 CEchoSerVerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEchoSerVerDlg::StarUp()
{
////////////////初始化环境
WSADATA wsaData;
WORD version=MAKEWORD(1,1);//版本号为1.1
int ret =WSAStartup(version,&wsaData);
if(ret !=0)
{
TRACE("初始化失败");
return ;
}
}
void CEchoSerVerDlg::CleanUp()
{
////////清除,释放资源
if(WSACleanup()!=0)
{
TRACE("UnInitilize Error:%d\n",WSAGetLastError());
}
}
BOOL CEchoSerVerDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CleanUp();
return CDialog::DestroyWindow();
}
void CEchoSerVerDlg::OnStartup()
{
// TODO: Add your control notification handler code here
if(m_socket!=NULL)
{
closesocket(m_socket);
m_socket=NULL;
}
if(m_socket==NULL)
{
m_socket=socket(AF_INET,SOCK_STREAM,0);/////建立套接字
ASSERT(m_socket!=NULL);///////
}
UpdateData();
m_addr.sin_family=AF_INET;
m_addr.sin_addr.S_un.S_addr=INADDR_ANY;//绑定本机
m_addr.sin_port=htons(m_port);
//////
////////
int ret=0;
int error=0;
///绑定套接字到本机地址
ret =bind(m_socket,(LPSOCKADDR)&m_addr,sizeof(m_addr));
if(ret==SOCKET_ERROR){
TRACE("Bind Error: %d\n",(error=WSAGetLastError()));
return;
}
//开始侦听
ret =listen(m_socket,3);
if(ret==SOCKET_ERROR){
TRACE("listen error:%d\n",(error==WSAGetLastError()));
return;
}
CEchoSerVerDlg *pdlg=this;
//unsigned threadID;
HANDLE hThread;
hThread=(HANDLE)_beginthreadex(NULL,0,acception,(void*)pdlg,0,0);
if(hThread!=0){
CString str;
CTime time = CTime::GetCurrentTime();
CString m_time = time.Format("%Y-%m-%d %H:%M:%S");
str.Format("%s"," 启动成功 ");
m_list.AddString(m_time+str);
}
}
unsigned __stdcall acception(void * p) {
//如果有用户发出请求,就应该建立边接,同时阻等待客户的边接;
CEchoSerVerDlg* m_Dlg=(CEchoSerVerDlg*)p;
int count=1;
m_Dlg->num=1;
HANDLE hThread[CLIENT];
while(true){
connected[count]=accept(m_Dlg->m_socket,NULL,NULL);
if(connected[count]==SOCKET_ERROR){
TRACE("Accept Error:%d\n",(WSAGetLastError()));
AfxMessageBox("acception error");
return 1;
}
else{
/* CString str,str1;
str1.Format("%d",count);
CTime time = CTime::GetCurrentTime();
CString m_time = time.Format("%Y-%m-%d %H:%M:%S");
str.Format("%s"," usr ");
m_Dlg->m_list.AddString(m_time+str+str1);*/
//建立了边接,就等待用户发来信息,该函数也阻塞
Clinfor pa;
pa.ID=count;
pa.q=m_Dlg;
hThread[count]=(HANDLE)_beginthreadex(NULL,0,recvandsend,(void*)&pa,0,0);
if(hThread[count]==0){
AfxMessageBox("receve error");
return 1;
}
}
count++;
m_Dlg->num++;
}
_endthreadex( 0 );
return 0;
}
unsigned __stdcall recvandsend(void * p){
Clinfor* pp=(Clinfor*)p;
CEchoSerVerDlg* m_Dlg=pp->q;
int count=pp->ID;
char buff[256];
int ret;
while(true){
ret=recv(connected[count],buff,256,0);
if(ret==0||ret==SOCKET_ERROR){
TRACE("Recv data error:%d\n",WSAGetLastError());
return 1;
}
buff[ret]='\0';
//对用户的信息进行处理;
CString str;
CTime time = CTime::GetCurrentTime();
CString m_time = time.Format("%Y-%m-%d %H:%M:%S");
str.Format("%s"," ");
m_Dlg->m_list.AddString(m_time+str+buff);
int ret1;
int i;
for(i=1;i<=m_Dlg->num;i++){
if(connected[i]!=NULL)
ret1=send(connected[i],buff,ret,0);
if(strcmp(buff,"exit")==0){
closesocket(connected[count]);
break;
}
}
}
_endthreadex( 0 );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -