📄 subject_37227.htm
字号:
<p>
序号:37227 发表者:Supin 发表日期:2003-04-23 09:37:42
<br>主题:怎么编程判断局域网中另一台机器已开机?(在线等待)
<br>内容: 怎么编程判断局域网中另一台机器已开机?<BR>即是相互连通的。
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:bb 回复日期:2003-04-23 09:54:57
<br>内容:通过ping原理可以得到<BR><BR>MSDN第一张盘\Samples\VC98\sdk\netds\winsock\ping中有较完整的源程序
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Supin 回复日期:2003-04-23 10:03:16
<br>内容:bb大哥,ping是一种不错的选择,但感觉太麻烦了一点,有没有简单一点的Windows 网络API函数可以判断的?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:bb 回复日期:2003-04-23 10:10:52
<br>内容:/******************************************************************************\<BR>* ping.c - Simple ping utility using SOCK_RAW<BR>* <BR>* This is a part of the Microsoft Source Code Samples.<BR>* Copyright 1996-1997 Microsoft Corporation.<BR>* All rights reserved.<BR>* This source code is only intended as a supplement to<BR>* Microsoft Development Tools and/or WinHelp documentation.<BR>* See these sources for detailed information regarding the<BR>* Microsoft samples programs.<BR>\******************************************************************************/<BR><BR>#pragma pack(4)<BR><BR>#define WIN32_LEAN_AND_MEAN<BR>#include <winsock2.h><BR>#include <stdio.h><BR>#include <stdlib.h><BR><BR>#define ICMP_ECHO 8<BR>#define ICMP_ECHOREPLY 0<BR><BR>#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)<BR><BR>/* The IP header */<BR>typedef struct iphdr {<BR> unsigned int h_len:4; // length of the header<BR> unsigned int version:4; // Version of IP<BR> unsigned char tos; // Type of service<BR> unsigned short total_len; // total length of the packet<BR> unsigned short ident; // unique identifier<BR> unsigned short frag_and_flags; // flags<BR> unsigned char ttl; <BR> unsigned char proto; // protocol (TCP, UDP etc)<BR> unsigned short checksum; // IP checksum<BR><BR> unsigned int sourceIP;<BR> unsigned int destIP;<BR><BR>}IpHeader;<BR><BR>//<BR>// ICMP header<BR>//<BR>typedef struct _ihdr {<BR> BYTE i_type;<BR> BYTE i_code; /* type sub code */<BR> USHORT i_cksum;<BR> USHORT i_id;<BR> USHORT i_seq;<BR> /* This is not the std header, but we reserve space for time */<BR> ULONG timestamp;<BR>}IcmpHeader;<BR><BR>#define STATUS_FAILED 0xFFFF<BR>#define DEF_PACKET_SIZE 32<BR>#define MAX_PACKET 1024<BR><BR>#define xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))<BR>#define xfree(p) HeapFree (GetProcessHeap(),0,(p))<BR><BR>void fill_icmp_data(char *, int);<BR>USHORT checksum(USHORT *, int);<BR>void decode_resp(char *,int ,struct sockaddr_in *);<BR><BR>void Usage(char *progname){<BR> <BR> fprintf(stderr,"Usage:\n");<BR> fprintf(stderr,"%s <host> [data_size]\n",progname);<BR> fprintf(stderr,"datasize can be up to 1Kb\n");<BR> ExitProcess(STATUS_FAILED);<BR><BR>}<BR>int main(int argc, char **argv){<BR><BR> WSADATA wsaData;<BR> SOCKET sockRaw;<BR> struct sockaddr_in dest,from;<BR> struct hostent * hp;<BR> int bread,datasize;<BR> int fromlen = sizeof(from);<BR> int timeout = 1000;<BR> char *dest_ip;<BR> char *icmp_data;<BR> char *recvbuf;<BR> unsigned int addr=0;<BR> USHORT seq_no = 0;<BR><BR> if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0){<BR> fprintf(stderr,"WSAStartup failed: %d\n",GetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR><BR> if (argc <2 ) {<BR> Usage(argv[0]);<BR> }<BR> sockRaw = WSASocket (AF_INET,<BR> SOCK_RAW,<BR> IPPROTO_ICMP,<BR> NULL, 0,0);<BR> <BR> if (sockRaw == INVALID_SOCKET) {<BR> fprintf(stderr,"WSASocket() failed: %d\n",WSAGetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> bread = setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,<BR> sizeof(timeout));<BR> if(bread == SOCKET_ERROR) {<BR> fprintf(stderr,"failed to set recv timeout: %d\n",WSAGetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> timeout = 1000;<BR> bread = setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,<BR> sizeof(timeout));<BR> if(bread == SOCKET_ERROR) {<BR> fprintf(stderr,"failed to set send timeout: %d\n",WSAGetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> memset(&dest,0,sizeof(dest));<BR><BR> hp = gethostbyname(argv[1]);<BR><BR> if (!hp){<BR> addr = inet_addr(argv[1]);<BR> }<BR> if ((!hp) && (addr == INADDR_NONE) ) {<BR> fprintf(stderr,"Unable to resolve %s\n",argv[1]);<BR> ExitProcess(STATUS_FAILED);<BR> }<BR><BR> if (hp != NULL)<BR> memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);<BR> else<BR> dest.sin_addr.s_addr = addr;<BR><BR> if (hp)<BR> dest.sin_family = hp->h_addrtype;<BR> else<BR> dest.sin_family = AF_INET;<BR><BR> dest_ip = inet_ntoa(dest.sin_addr);<BR><BR> if (argc >2) {<BR> datasize = atoi(argv[2]);<BR> if (datasize == 0)<BR> datasize = DEF_PACKET_SIZE;<BR><BR> }<BR> else<BR> datasize = DEF_PACKET_SIZE;<BR> <BR> datasize += sizeof(IcmpHeader); <BR><BR> icmp_data = xmalloc(MAX_PACKET);<BR> recvbuf = xmalloc(MAX_PACKET);<BR><BR> if (!icmp_data) {<BR> fprintf(stderr,"HeapAlloc failed %d\n",GetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> <BR><BR> memset(icmp_data,0,MAX_PACKET);<BR> fill_icmp_data(icmp_data,datasize);<BR><BR> while(1) {<BR> int bwrote;<BR> <BR> ((IcmpHeader*)icmp_data)->i_cksum = 0;<BR> ((IcmpHeader*)icmp_data)->timestamp = GetTickCount();<BR><BR> ((IcmpHeader*)icmp_data)->i_seq = seq_no++;<BR> ((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data, <BR> datasize);<BR><BR> bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest,<BR> sizeof(dest));<BR> if (bwrote == SOCKET_ERROR){<BR> if (WSAGetLastError() == WSAETIMEDOUT) {<BR> printf("timed out\n");<BR> continue;<BR> }<BR> fprintf(stderr,"sendto failed: %d\n",WSAGetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> if (bwrote < datasize ) {<BR> fprintf(stdout,"Wrote %d bytes\n",bwrote);<BR> }<BR> bread = recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from,<BR> &fromlen);<BR> if (bread == SOCKET_ERROR){<BR> if (WSAGetLastError() == WSAETIMEDOUT) {<BR> printf("timed out\n");<BR> continue;<BR> }<BR> fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError());<BR> ExitProcess(STATUS_FAILED);<BR> }<BR> decode_resp(recvbuf,bread,&from);<BR> Sleep(1000);<BR><BR> }<BR> return 0;<BR><BR>}<BR>/* <BR> The response is an IP packet. We must decode the IP header to locate <BR> the ICMP data <BR>*/<BR>void decode_resp(char *buf, int bytes,struct sockaddr_in *from) {<BR><BR> IpHeader *iphdr;<BR> IcmpHeader *icmphdr;<BR> unsigned short iphdrlen;<BR><BR> iphdr = (IpHeader *)buf;<BR><BR> iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes<BR><BR> if (bytes < iphdrlen + ICMP_MIN) {<BR> printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr));<BR> }<BR><BR> icmphdr = (IcmpHeader*)(buf + iphdrlen);<BR><BR> if (icmphdr->i_type != ICMP_ECHOREPLY) {<BR> fprintf(stderr,"non-echo type %d recvd\n",icmphdr->i_type);<BR> return;<BR> }<BR> if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) {<BR> fprintf(stderr,"someone else's packet!\n");<BR> return ;<BR> }<BR> printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr));<BR> printf(" icmp_seq = %d. ",icmphdr->i_seq);<BR> printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp);<BR> printf("\n");<BR> <BR>}<BR><BR><BR>USHORT checksum(USHORT *buffer, int size) {<BR><BR> unsigned long cksum=0;<BR><BR> while(size >1) {<BR> cksum+=*buffer++;<BR> size -=sizeof(USHORT);<BR> }<BR> <BR> if(size ) {<BR> cksum += *(UCHAR*)buffer;<BR> }<BR><BR> cksum = (cksum >> 16) + (cksum & 0xffff);<BR> cksum += (cksum >>16);<BR> return (USHORT)(~cksum);<BR>}<BR>/* <BR> Helper function to fill in various stuff in our ICMP request.<BR>*/<BR>void fill_icmp_data(char * icmp_data, int datasize){<BR><BR> IcmpHeader *icmp_hdr;<BR> char *datapart;<BR><BR> icmp_hdr = (IcmpHeader*)icmp_data;<BR><BR> icmp_hdr->i_type = ICMP_ECHO;<BR> icmp_hdr->i_code = 0;<BR> icmp_hdr->i_id = (USHORT)GetCurrentProcessId();<BR> icmp_hdr->i_cksum = 0;<BR> icmp_hdr->i_seq = 0;<BR> <BR> datapart = icmp_data + sizeof(IcmpHeader);<BR> //<BR> // Place some junk in the buffer.<BR> //<BR> memset(datapart,'E', datasize - sizeof(IcmpHeader));<BR><BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:neilgan 回复日期:2003-04-23 10:53:25
<br>内容:Try connect to port 139 of the computer, which is its netbios port. If succeeded, it is power on.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Supin 回复日期:2003-04-23 12:24:02
<br>内容:不好意思,只能给一个人分,下次再请大家光顾。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -