📄 serverdlg.cpp
字号:
// 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
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//CRITICAL_SECTION g_cs;
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()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
HANDLE hMutex;
SOCKET CServerDlg::sockconn[4] = {INVALID_SOCKET , INVALID_SOCKET , INVALID_SOCKET , INVALID_SOCKET};
SOCKET CServerDlg::sockcon = INVALID_SOCKET;
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
m_client1 = 0;
m_client2 = 0;
m_client3 = 0;
m_client4 = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nAlarm = 0;
//m_strCommand = "";
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Text(pDX, IDC_CLIENT1, m_client1);
DDX_Text(pDX, IDC_CLIENT2, m_client2);
DDX_Text(pDX, IDC_CLIENT3, m_client3);
DDX_Text(pDX, IDC_CLIENT4, m_client4);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_SET, OnSet)
ON_MESSAGE(ON_COM_RECEIVE, OnComRecv)
ON_BN_CLICKED(IDC_TEST, OnTest)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETCLIENT,SetClient)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
//CString CServerDlg::strCommand = "hello";
BOOL CServerDlg::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
//InitializeCriticalSection(&g_cs);
fileini.iniopen("setclient.ini",8);
if(fileini.iniread("CLIENTA") != NULL)
{
m_client1 = atoi(fileini.iniread("CLIENTA"));
m_client2 = atoi(fileini.iniread("CLIENTB"));
m_client3 = atoi(fileini.iniread("CLIENTC"));
m_client4 = atoi(fileini.iniread("CLIENTD"));
UpdateData(FALSE);
}
//if(fileini.iniread(""))
InitSocket();
if(!m_comAlarm.Open(1, 9600))
{
AfxMessageBox("port 1 cannot open~");//默认9600
//exit(0);
}
m_comAlarm.SetWnd(m_hWnd);
RECVPARAM* pRecvParam1 = new RECVPARAM;
pRecvParam1->sock=m_socket;
pRecvParam1->hwnd=m_hWnd;
hMutex = CreateMutex(NULL,FALSE,NULL);
HANDLE hThread1 = CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam1,0,NULL);
CloseHandle(hThread1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CServerDlg::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 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;
}
LRESULT CServerDlg::OnComRecv(WPARAM wParam, LPARAM lParam)
{
if(wParam == 1)
{
CString strAlarm = "00[080~/";
//m_comAlarm.ReadString(strAlarm.GetBuffer(strAlarm.GetLength()),1024);
m_commands = m_commands + strAlarm;
int npos = strAlarm.Find("[",0);
strAlarm = strAlarm.Mid((npos+1),3);
if(npos > 0)
{
char *strbuf = NULL;
// char clientbuf[100];
m_commands.Format("%s",strAlarm.Right(strAlarm.GetLength()-8)); // 00[002~/
strbuf = strAlarm.GetBuffer(strAlarm.GetLength());
int conn = atoi(strbuf);
if(conn < m_client1)
{
if(SOCKET_ERROR == ::send(sockconn[0],strbuf,strlen(strbuf)+1,0))
{
MessageBox("client error!");
return -1;
}
}
else if(conn < m_client2)
{
if(SOCKET_ERROR == ::send(sockconn[1],strbuf,strlen(strbuf)+1,0))
{
MessageBox("client error!");
return -1;
}
}
else if(conn < m_client3)
{
if(SOCKET_ERROR == ::send(sockconn[2],strbuf,strlen(strbuf)+1,0))
{
MessageBox("client error!");
return -1;
}
}
else if(conn < m_client4)
{
if(SOCKET_ERROR == ::send(sockconn[3],strbuf,strlen(strbuf)+1,0))
{
MessageBox("client error!");
return -1;
}
}
else
{
MessageBox("can't to send!");
return -1;
}
}
}
return 0;
}
BOOL CServerDlg::InitSocket()
{
m_socket=socket(AF_INET,SOCK_STREAM,0);
if(INVALID_SOCKET==m_socket)
{
MessageBox("套接字创建失败!");
return FALSE;
}
SOCKADDR_IN addrSock;
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(8888);
addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
int retval;
retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
if(SOCKET_ERROR==retval)
{
closesocket(m_socket);
MessageBox("绑定失败!");
return FALSE;
}
return TRUE;
}
DWORD WINAPI CServerDlg::RecvProc(LPVOID lpParameter)
{
SOCKET sock = ((RECVPARAM*)lpParameter)->sock;
HWND hwnd = ((RECVPARAM*)lpParameter)->hwnd;
delete lpParameter;
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
// CString strCommand = "";
int nconn = 0;
char tempbuf[60];
while(true)
{
WaitForSingleObject(hMutex,INFINITE);
listen(sock,5);
sockcon = accept(sock,(SOCKADDR*)&addrClient,&len);
if(INVALID_SOCKET == sockcon)
{
//closesocket(sockcon);
continue;
}
// send(sockconn[nconn],"?",2,0);
sprintf(tempbuf,"%s",inet_ntoa(addrClient.sin_addr));
::SendMessage(hwnd,WM_SETCLIENT,(WPARAM)nconn,(LPARAM)(tempbuf));
if(nconn == 3)
{
nconn = 0;
//closesocket(sockconn[nconn]);
continue;
}
nconn++;
// ::PostMessage(hwnd,WM_PAINT,0,0);
ReleaseMutex(hMutex);
}
return 0;
}
//DEL DWORD WINAPI CServerDlg::RecvIPProc(LPVOID lpParameter)
//DEL {
//DEL SOCKET sock = ((RECVPARAM*)lpParameter)->sock;
//DEL HWND hwnd = ((RECVPARAM*)lpParameter)->hwnd;
//DEL delete lpParameter;
//DEL
//DEL int retval;
//DEL char tempbuf[100];
//DEL while(true)
//DEL {
//DEL WaitForSingleObject(hMutex,INFINITE);
//DEL
//DEL retval = recv(sock,tempbuf,100,0);
//DEL if(retval == SOCKET_ERROR)
//DEL {
//DEL ReleaseMutex(hMutex);
//DEL continue;
//DEL }
//DEL if(tempbuf[0]=='!')
//DEL {
//DEL CString strtemp = tempbuf;
//DEL strtemp = strtemp.Right(strtemp.GetLength()-1);
//DEL }
//DEL
//DEL ReleaseMutex(hMutex);
//DEL }
//DEL return 0;
//DEL }
void CServerDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
for(int i = 0; i < 4; i++)
{
if(sockconn[i] == INVALID_SOCKET)
continue;
send(CServerDlg::sockconn[i],"$",strlen("$")+1,0);
}
if(m_socket)
closesocket(m_socket);
if(sockcon)
closesocket(sockcon);
for(i = 0;i < 4;i++)
{
closesocket(CServerDlg::sockconn[i]);
}
/*
if(fileini.iniread("0") != NULL)
fileini.inimodify("0","NULL");
if(fileini.iniread("1") != NULL)
fileini.inimodify("1","NULL");
if(fileini.iniread("2") != NULL)
fileini.inimodify("2","NULL");
if(fileini.iniread("3") != NULL)
fileini.inimodify("3","NULL");
*/
CDialog::OnClose();
}
void CServerDlg::OnSet()
{
// TODO: Add your control notification handler code here
UpdateData();
char strbuf[10];
if(fileini.iniread("CLIENTA") == NULL)
{
//fileini.iniopen("setclient.ini",8);
itoa(m_client1,strbuf,10);
fileini.iniwrite("CLIENTA",strbuf);
itoa(m_client2,strbuf,10);
fileini.iniwrite("CLIENTB",strbuf);
itoa(m_client3,strbuf,10);
fileini.iniwrite("CLIENTC",strbuf);
itoa(m_client4,strbuf,10);
fileini.iniwrite("CLIENTD",strbuf);
}
else
{
itoa(m_client1,strbuf,10);
fileini.inimodify("CLIENTA",strbuf);
itoa(m_client2,strbuf,10);
fileini.inimodify("CLIENTB",strbuf);
itoa(m_client3,strbuf,10);
fileini.inimodify("CLIENTC",strbuf);
itoa(m_client4,strbuf,10);
fileini.inimodify("CLIENTD",strbuf);
}
UpdateData(FALSE);
}
void CServerDlg::OnTest()
{
// TODO: Add your control notification handler code here
::SendMessage(m_hWnd,ON_COM_RECEIVE,1,0);
}
void CServerDlg::SetClient(WPARAM wParam,LPARAM lParam)
{
char bufid[10];
CString ip = (char*)lParam;
int nnum = 0;
for(;nnum < 4;)
{
itoa(nnum,bufid,10);
if(fileini.iniread(bufid) != NULL)
{
CString strip = fileini.iniread(bufid);
if(strip == ip)
{
sockconn[nnum] = sockcon;
}
}
nnum++;
}
/*
if(fileini.iniread(bufid) == NULL)
fileini.iniwrite(bufid,ip.GetBuffer(ip.GetLength()));
else
fileini.inimodify(bufid,ip.GetBuffer(ip.GetLength()));
fileini.Fileflush();
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -