// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include <afx.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
long shumu=0;
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
CServerDlg *hINFO = NULL;
CWinThread* pThread1;
UINT ProcessTreadIOA( LPVOID pParam);
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
m_port = 0;
m_locoal = _T("");
m_shumu = 0;
//}}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)
DDX_Control(pDX, IDC_IPADDRESS, m_ip);
DDX_Text(pDX, IDC_PORT, m_port);
DDX_Text(pDX, IDC_EDIT1, m_locoal);
DDX_Text(pDX, IDC_EDIT2, m_shumu);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_LICSEN, OnButtonLicsen)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
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
hINFO = this;
int i;
char sTmp[32]="";
unsigned long IP;
IP=-1;
for (i=0; IP!=0;i++)
{
IP=GetLocalNumericIP(i);
if (IP !=0)
{
wsprintf(sTmp,"%d.%d.%d.%d",(IP&0xFF),((IP>>8 ) & 0xFF),((IP>>16) & 0xFF),((IP>>24) & 0xFF));
}
CString theString = sTmp;
theString.Format(_T("%s"), sTmp);
m_locoal=theString;
}
UpdateData(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 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;
}
void CServerDlg::OnButtonLicsen()
{
// TODO: Add your control notification handler code here
CFileDialog Dlg(TRUE);
if(Dlg.DoModal()!=IDOK)
return;
CFile file1;
CString dwj;
LPCTSTR end="%%%";
if(!file1.Open(Dlg.GetPathName(), CFile::modeRead | CFile::typeBinary))
{
AfxMessageBox("文件不存在!",MB_OK|MB_ICONERROR);
return;
}
shumu=0;
file1.SeekToBegin();/////存入到CString之中
CString youyong;
int nLen=file1.GetLength();
char *buf=new char [nLen+10];
file1.ReadHuge(buf,nLen);
dwj += buf;
file1.Close();
int n=dwj.Find(end);
youyong=dwj.Mid(0,n+4);
UpdateData(true);
unsigned int lport=0;
CSocket sockSrvr;
sockSrvr.Create();
lport=m_port;
unsigned char *pIP;
CString strIP;
DWORD dwIP;
m_ip.GetAddress(dwIP);
pIP=(unsigned char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3),*(pIP+2),*(pIP+1),*pIP);
if(!sockSrvr.Connect(strIP, lport))
{
AfxMessageBox("连接到对方机器失败!");
return;
}
AfxMessageBox(youyong);
//int recvbuflen=(int)strlen(recvbuf);
sockSrvr.Send(youyong,youyong.GetLength());
UINT dwRead=0;sockSrvr.Close();
AfxMessageBox("发送完毕!");
shumu=shumu+1;
}
DWORD CServerDlg::GetLocalNumericIP(int Index)
{
if (0==gethostname (HostName,100))
{
if (!(lpHostEnt = gethostbyname(HostName)))
{
return(-1);
} else
{
lpDW=((DWORD*)(*lpHostEnt).h_addr_list);
lpDW+=Index;
lpDW2=(DWORD *) *lpDW;
if (lpDW2==0)
return 0;
else
return (*lpDW2);
}
} else
return (-1);
}
void CServerDlg::OnButton1()
{
CDialog::OnOK();
}
UINT ProcessTreadIOA( LPVOID pParam)
{
CFileDialog Dlg(TRUE);
if(Dlg.DoModal()!=IDOK)
return 0;
CFile file1;
CString dwj;
LPCTSTR end="%%%";
if(!file1.Open(Dlg.GetPathName(), CFile::modeRead | CFile::typeBinary))
{
AfxMessageBox("文件不存在!",MB_OK|MB_ICONERROR);
return 0;
}
file1.SeekToBegin();/////存入到CString之中
CString youyong;
int nLen=file1.GetLength();
char *buf=new char [nLen+10];
file1.ReadHuge(buf,nLen);
dwj += buf;
file1.Close();
int n=dwj.Find(end);
youyong=dwj.Mid(0,n+4);
unsigned int lport=0;
lport=hINFO->m_port;
unsigned char *pIP;
CString strIP;
DWORD dwIP;
hINFO->m_ip.GetAddress(dwIP);
pIP=(unsigned char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3),*(pIP+2),*(pIP+1),*pIP);
for(;;)
{
Sleep(60);
CSocket sockSrvr;
sockSrvr.Create();
if(!sockSrvr.Connect(strIP, lport))
{
AfxMessageBox("连接到对方机器失败!");
return 0;
}
sockSrvr.Send(youyong,youyong.GetLength());
sockSrvr.Close();
}
return 0;
}
void CServerDlg::OnButton2()
{
UINT dwRead=0;
hINFO->UpdateData(true);
if (pThread1==NULL)
{
pThread1 =AfxBeginThread(ProcessTreadIOA,GetSafeHwnd());
return ;
}
}
void CServerDlg::OnButton3()
{
if (pThread1==NULL)
return ;
// ::TerminateThread(pThread1->m_hThread,NULL);
pThread1->SuspendThread();
return ;
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页