アプリ版:「スタンプのみでお礼する」機能のリリースについて

■環境
Windows XP SP2
VS2008 SP1
クライアント 及び リモート ともにWindows XP SP2

wakeonlanプログラムを以下の様に書いたのですが
printf("host->h_length=%d\n",host->h_length);の時点で落ちてしまいます。
デバッグすると、gethostbyname関数が成功していなくてhostに値が入っていない
のが原因なのですがなぜ失敗するか分かりません。

リモートはBIOSはwakeonlanをONに設定して、
ネットワークアダプタもMagic Packetに設定しております。

どなたかWindowsにおけるwake on lanの模範的なソースを提示していただけないでしょうか。


#include "stdafx.h"
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<commctrl.h>
#include<wininet.h>
int main(void)
{
unsigned char mac[7];
int i;
char* p = "Macアドレスを記入";
for (i = 0; i < 6; i++, p+=3) {
if (isxdigit(p[0]) && isxdigit(p[1]) && (!p[2] || p[2] == ':')) {
unsigned lonfiltered= strtoul(p, NULL, 16);
if (a != ULONG_MAX) { mac[i] = (byte)a; continue; }
}
fprintf(stderr, "invalid MAC address.%d %s\n", i, p); return -1;
}

int sockfd;
struct hostent* host;
struct sockaddr_in peeraddr;


sockfd = socket(AF_INET, SOCK_DGRAM, 0);
int optval = 1;
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,(char*)&optval,sizeof(optval));
host = gethostbyname("*.*.*.*の形でIPアドレスを設定しておく");

printf("host->h_length=%d\n",host->h_length);

memset(&peeraddr, 0, sizeof(peeraddr));
peeraddr.sin_family = AF_INET;
memcpy(&peeraddr.sin_addr, host->h_addr, host->h_length);
peeraddr.sin_port = htons(9); // discard

char buf[6+6*16];
memset(buf, 0xff, 6);
for (i = 0; i < 16; i++) memcpy(buf+6+i*6, mac, 6);

sendto(sockfd, buf, 6+6*16, 0, (struct sockaddr*)&peeraddr, sizeof(peeraddr));



return 0;
}

A 回答 (2件)

gethostbynameのリファレンスには、



> The gethostbyname function cannot resolve IP address strings passed to it.

と書かれています。

http://msdn.microsoft.com/en-us/library/ms738524 …
    • good
    • 0

it returns a null pointer and a specific error number can be retrieved by calling WSAGetLastError.


と書かれていますが…
WSAGetLastError()でエラーコードを調べないのはなぜですか?

# そのまんまなエラーコードが返されると思いますが。
    • good
    • 0
この回答へのお礼

こんばんは
WSAGetLastError()を知りませんでした
ご教授ありがとうございます。

お礼日時:2010/01/22 20:04

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!