远程开机

完整的程序有需要的话可以联系我[email protected]

//总的来说远程开机运用了amd制定的wol协议协议的具体内容是发送一个魔数包:ffffffffffff(12个f,共六个字节)+16次的mac地址通过udp发送(因为udp不要建立连接)

mac地址可以通过在目标机上运行getmac或者ipconfig /all获得

需要注意的一点udp编程时候如果指定了ip地址为255.255.255.255,这样就要调用setsockopt函数来设置广播模式否则在win7下老是返回10013错误(无权限)

// walkonlanDlg.cpp : implementation file
//


#include "stdafx.h"
#include "walkonlan.h"
#include "walkonlanDlg.h"
#include <WINSOCK2.H>
#pragma comment(lib, "ws2_32.lib")


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// 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()


/////////////////////////////////////////////////////////////////////////////
// CWalkonlanDlg dialog


CWalkonlanDlg::CWalkonlanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWalkonlanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWalkonlanDlg)
m_Edit_IpAddr = _T("");
m_Edit_MAC = _T("");
m_EditPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CWalkonlanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWalkonlanDlg)
DDX_Text(pDX, IDC_EDIT_IP, m_Edit_IpAddr);
DDX_Text(pDX, IDC_EDIT_MAC, m_Edit_MAC);
DDX_Text(pDX, IDC_EDIT_PORT, m_EditPort);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWalkonlanDlg, CDialog)
//{{AFX_MSG_MAP(CWalkonlanDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_WOL, OnBtnWol)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CWalkonlanDlg message handlers


BOOL CWalkonlanDlg::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

return TRUE;  // return TRUE  unless you set the focus to a control
}


void CWalkonlanDlg::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 CWalkonlanDlg::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 CWalkonlanDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}


void CWalkonlanDlg::WOL()
{
WORD wVersionRequested; 
WSADATA wsaData; 
int err; 
unsigned char SendBuff[1024];

wVersionRequested=MAKEWORD(1,1); 
err=WSAStartup(wVersionRequested,&wsaData); 

if(err!=0)
{ 
AfxMessageBox("加载套接字失败!");
return ; 
} 


UpdateData(TRUE);
//创建用于监听的嵌套字 
unsigned char MAC[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xff};
//unsigned char MAC[6] = "90004E1DF31D";
int i;

memset(SendBuff, 0, sizeof(SendBuff));
//memcpy(MAC, m_Edit_MAC,sizeof(MAC));
memset(SendBuff,0xff,6);
int magicpacket=6;
for(i = 0; i < 16; i++)
{
memcpy(SendBuff+magicpacket,MAC,6);
magicpacket+=6;
}


SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0); 
if (sockClient==INVALID_SOCKET)
{
AfxMessageBox("创建套接字失败!");
return;
}
BOOL bOptVal=TRUE;
int iOptLen=sizeof(BOOL);


if (setsockopt(sockClient, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, iOptLen)==SOCKET_ERROR)
{
AfxMessageBox("创建套接字失败!");
return;
}


SOCKADDR_IN addrSrv; 
addrSrv.sin_family = AF_INET; 
addrSrv.sin_addr.S_un.S_addr = inet_addr("255.255.255.255"/*m_Edit_IpAddr.GetBuffer(0)*/); // 接收方IP地址
//addrSrv.sin_addr.s_addr=htonl(INADDR_BROADCAST);//等同上句
//addrSrv.sin_port = htons(/*m_EditPort*/6646); 
int err1 = sendto(sockClient, (const char *)SendBuff, magicpacket, 0, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); 
if(err1 == SOCKET_ERROR)
{
char errcode[10];
wsprintf(errcode,"%d",WSAGetLastError());
AfxMessageBox(errcode);
}
// char sendlen[10];
// wsprintf(sendlen,"%d",err1);
//  AfxMessageBox(sendlen);
//关闭嵌套字 
closesocket(sockClient); 
WSACleanup(); 
//AfxMessageBox(MAC);
}


void CWalkonlanDlg::OnBtnWol() 
{
// TODO: Add your control notification handler code here
WOL();
}


你可能感兴趣的:(远程开机)