J
jxc163
Unregistered / Unconfirmed
GUEST, unregistred user!
向局域网电脑的1434端口发送02, 03, 04 ,以搜索网内的数据库服务器。<br>(这里不用的内容去除,原贴见:http://www.delphibbs.com/delphibbs/dispq.asp?LID=2057477)<br>#include "stdafx.h"<br>#include <string.h><br>#include <stdio.h><br>#include <winsock2.h><br><br>void decode_recv (char *buf, int size)<br>{<br> int index;<br> int counter = 0;<br> for (index = 3; index < size; index++)//-----这个循环为什么从3开始<br> {<br> if ((buf[index] == ';') && (buf[index+1] != ';'))<br> { <br> //Look for a semi-colon and check for end of record (;<br> if ((counter % 2) == 0)<br> {<br> printf(":");<br> counter++;<br> }<br> else<br> {<br> printf("/n");<br> counter++;<br> }<br> }<br> else<br> {<br> if (buf[index] != ';')<br> { <br> // If an end of record (;, then double-space for next instance<br> printf("%c",buf[index]);<br> }<br> else<br> {<br> printf("/n");<br> }<br> }<br> } <br> printf("/n");<br>}<br><br>void listen (void* v)<br>{<br> static const unsigned int buffersize = 64000;<br> static char buffer [buffersize];<br><br> SOCKET s = (SOCKET)v; -------这句是什么意思?<br><br> for (;------这个循环怎么会这样呢?<br> {<br> struct sockaddr_in udpfrom;<br> int udpfromlen = sizeof(udpfrom);<br> int n = recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr *)&udpfrom, &udpfromlen);<br> int e = WSAGetLastError();<br> <br> if (n > 0 && e == 0)<br> decode_recv(buffer, n);<br> }<br>}<br><br>int main(int argc, char* argv[])<br>{<br> WSADATA WSAData;<br> SOCKET sock;<br> SOCKADDR_IN addr_in;<br> char buf[5]={'/x02'}; ----这句是什么意思?<br> HANDLE listener;<br><br> if (argc<2)<br> {<br> return false;<br> }<br> if (WSAStartup(MAKEWORD(2,0),&WSAData)!=0)<br> {<br> printf("WSAStartup error.Error:%d/n",WSAGetLastError());<br> return false;<br> }<br> if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET)<br> {<br> printf("Socket failed.Error:%d/n",WSAGetLastError());<br> return false;<br> }<br><br> addr_in.sin_family=AF_INET;<br> addr_in.sin_port=htons(1434);<br> addr_in.sin_addr.S_un.S_addr=inet_addr(argv[1]);<br><br> const int SNDBUF = 0;<br> const int TCPNODELAY = true;<br> const int BROADCAST = true;<br> <br> if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&SNDBUF, sizeof(SNDBUF))==SOCKET_ERROR)<br> {<br> printf("Set SO_SNDBUF failed.Error:%d",WSAGetLastError());<br> return false;<br> }<br> if (setsockopt(sock, SOL_SOCKET, TCP_NODELAY, (const char*)&TCPNODELAY, sizeof(TCPNODELAY))==SOCKET_ERROR)<br> {<br> printf("Set TCP_NODELAY failed.Error:%d",WSAGetLastError());<br> return false;<br> }<br> if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&BROADCAST, sizeof(BROADCAST))==SOCKET_ERROR)<br> {<br> printf("Set SO_BROADCAST failed.Error:%d",WSAGetLastError());<br> return false;<br> }<br><br> listener = (HANDLE) _beginthread(listen, 0, (void*)sock);<br><br>// e = sendto(s, "/08", 1, 0,(sockaddr*) &hostaddr, sizeof(hostaddr));<br> if (sendto(sock, buf, sizeof(buf), 0,(sockaddr*) &addr_in, sizeof(addr_in))==SOCKET_ERROR)<br> {<br> printf("Send failed.Error:%d/n",WSAGetLastError());<br> return false;<br> }<br><br> printf("Listening..../n/n");<br><br> // wait a little while for listener thread<br> WaitForSingleObject(listener, 5000);<br><br> WSACleanup();<br><br> printf("SQLPing Complete./n");<br> return 0;<br>}<br><br>