谁能把下面的这段C代码译成DELPHI7的代码???! (100分)

  • 主题发起人 主题发起人 jxc163
  • 开始时间 开始时间
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 &lt;string.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;winsock2.h&gt;<br><br>void decode_recv (char *buf, int size)<br>{<br>&nbsp; &nbsp; int index;<br>&nbsp; &nbsp; int counter = 0;<br>&nbsp; &nbsp; for (index = 3; index &lt; size; index++)//-----这个循环为什么从3开始<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((buf[index] == ';') &amp;&amp; (buf[index+1] != ';'))<br>&nbsp; &nbsp; &nbsp; &nbsp; { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Look for a semi-colon and check for end of record (;;)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((counter % 2) == 0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(":");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("/n");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (buf[index] != ';')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If an end of record (;;), then double-space for next instance<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%c",buf[index]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("/n");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; } &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; printf("/n");<br>}<br><br>void listen (void* v)<br>{<br>&nbsp; &nbsp; static const unsigned int buffersize = 64000;<br>&nbsp; &nbsp; static char buffer [buffersize];<br><br>&nbsp; &nbsp; SOCKET s = (SOCKET)v; &nbsp;-------这句是什么意思?<br><br>&nbsp; &nbsp; for (;;)------这个循环怎么会这样呢?<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; struct sockaddr_in udpfrom;<br>&nbsp; &nbsp; &nbsp; &nbsp; int udpfromlen = sizeof(udpfrom);<br>&nbsp; &nbsp; &nbsp; &nbsp; int n = recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr *)&amp;udpfrom, &amp;udpfromlen);<br>&nbsp; &nbsp; &nbsp; &nbsp; int e = WSAGetLastError();<br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; if (n &gt; 0 &amp;&amp; e == 0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decode_recv(buffer, n);<br>&nbsp; &nbsp; }<br>}<br><br>int main(int argc, char* argv[])<br>{<br>&nbsp; &nbsp; WSADATA WSAData;<br>&nbsp; &nbsp; SOCKET sock;<br>&nbsp; &nbsp; SOCKADDR_IN addr_in;<br>&nbsp; &nbsp; char buf[5]={'/x02'}; ----这句是什么意思?<br>&nbsp; &nbsp; HANDLE listener;<br><br>&nbsp; &nbsp; if (argc&lt;2)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; if (WSAStartup(MAKEWORD(2,0),&amp;WSAData)!=0)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("WSAStartup error.Error:%d/n",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Socket failed.Error:%d/n",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; addr_in.sin_family=AF_INET;<br>&nbsp; &nbsp; addr_in.sin_port=htons(1434);<br>&nbsp; &nbsp; addr_in.sin_addr.S_un.S_addr=inet_addr(argv[1]);<br><br>&nbsp; &nbsp; const int SNDBUF = 0;<br>&nbsp; &nbsp; const int TCPNODELAY = true;<br>&nbsp; &nbsp; const int BROADCAST = true;<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&amp;SNDBUF, sizeof(SNDBUF))==SOCKET_ERROR)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Set SO_SNDBUF failed.Error:%d",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; if (setsockopt(sock, SOL_SOCKET, TCP_NODELAY, (const char*)&amp;TCPNODELAY, sizeof(TCPNODELAY))==SOCKET_ERROR)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Set TCP_NODELAY failed.Error:%d",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&amp;BROADCAST, sizeof(BROADCAST))==SOCKET_ERROR)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Set SO_BROADCAST failed.Error:%d",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; listener = (HANDLE) _beginthread(listen, 0, (void*)sock);<br><br>// &nbsp; &nbsp;e = sendto(s, "/08", 1, 0,(sockaddr*) &amp;hostaddr, sizeof(hostaddr));<br>&nbsp; &nbsp; if (sendto(sock, buf, sizeof(buf), 0,(sockaddr*) &amp;addr_in, sizeof(addr_in))==SOCKET_ERROR)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Send failed.Error:%d/n",WSAGetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; return false;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; printf("Listening..../n/n");<br><br>&nbsp; &nbsp; // wait a little while for listener thread<br>&nbsp; &nbsp; WaitForSingleObject(listener, 5000);<br><br>&nbsp; &nbsp; WSACleanup();<br><br>&nbsp; &nbsp; printf("SQLPing Complete./n");<br>&nbsp; &nbsp; return 0;<br>}<br><br>
 
这个贴子你看看,有个法子很新颖,你试试看<br>http://www.delphibbs.com/delphibbs/dispq.asp?LID=2057477
 
没有人知道吗?
 
太长了,看起来太累了
 
建议你用Sniffer看一下 网络数据包就明白了<br><br>for (index = 3; index &lt; size; index++)//表示前几个字节没有用不用管它<br><br>SOCKET s = (SOCKET)v; 表示强制类型转换<br><br>for (;;)------表示开始一个死循环,没有循环变量,没有结束条件<br><br>char buf[5]={'/x02'}; 表示一个字符串数组,用一个字符串初始化 /x02表示 Asc码是0x02的字符<br><br>
 
lick老兄,可以帮我看看这个delphi过程,要怎么才能实现与上面的C一样的功能呢?我是把返回的字符串加到一个TStrings中的。<br>unit GetServer;<br>interface<br><br>uses Windows, Messages, SysUtils, Variants, Classes, Winsock, Dialogs, Forms, Controls;<br><br>const<br>&nbsp; Sql_Port = 1434;<br>&nbsp; <br>Procedure GetSQLServer(var L : TStrings);stdcall;<br>function GetLocalIP : string;stdcall;<br><br>implementation<br><br>//取本机的IP<br>function GetLocalIP : string;<br>type<br>&nbsp; TaPInAddr = array [0..10] of PInAddr;<br>&nbsp; PaPInAddr = ^TaPInAddr;<br>var<br>&nbsp; phe &nbsp;: PHostEnt;<br>&nbsp; pptr : PaPInAddr;<br>&nbsp; Buffer : array [0..63] of char;<br>&nbsp; I &nbsp; &nbsp;: Integer;<br>&nbsp; GInitData &nbsp; &nbsp; &nbsp;: TWSADATA;<br>begin<br>&nbsp; WSAStartup($2, GInitData);<br>&nbsp; Result := '';<br>&nbsp; GetHostName(Buffer, SizeOf(Buffer));<br>&nbsp; phe :=GetHostByName(buffer);<br>&nbsp; if phe = nil then Exit;<br>&nbsp; pptr := PaPInAddr(Phe^.h_addr_list);<br>&nbsp; I := 0;<br>&nbsp; while pptr^ &lt;&gt; nil do begin<br>&nbsp; &nbsp; result:=StrPas(inet_ntoa(pptr^^));<br>&nbsp; &nbsp; Inc(I);<br>&nbsp; end;<br>&nbsp; WSACleanup;<br>end;<br><br>procedure GetSqlServer(var L : TStrings);<br>type<br>&nbsp; PParam = ^RParam;<br>&nbsp; RParam = Record<br>&nbsp; &nbsp; L : TStrings;<br>&nbsp; &nbsp; V : TSocket;<br>&nbsp; end;<br>var<br>&nbsp; Ws : WsaData;<br>&nbsp; IP : String;//保存本机的IP地址<br>&nbsp; Sock : TSocket;//保存Socket套接字<br>&nbsp; Addr_In : SockAddr_in;<br>&nbsp; SndBuf : PChar;<br>&nbsp; NoDelay : PChar;<br>&nbsp; Broadcast : PChar;<br>&nbsp; Listener : THandle;<br> P : PParam;<br>&nbsp; Buf : Array [0..5] of Char;<br><br>&nbsp; procedure Decode_Recv(var L : TStrings; Buf : Pchar; Size : Integer);<br>&nbsp; var<br>&nbsp; &nbsp; Index,Counter : Integer;<br>&nbsp; &nbsp; S : String;<br>&nbsp; begin<br>&nbsp; &nbsp; Counter := 0;<br>&nbsp; &nbsp; S := '';<br>&nbsp; &nbsp; for Index :=0 to Size do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (Buf[index] = ';') and (Buf[index+1] &lt;&gt; ';') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Counter div 2 = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S := S + ':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Counter);<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S := S + #10;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Counter);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Buf[Index] &lt;&gt; ';' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S := S + Buf[Index]<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S := S + #10;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if (S = '') and (S &lt;&gt; #10) then<br>&nbsp; &nbsp; &nbsp; L.Add(S);<br>&nbsp; end;<br><br>&nbsp; procedure Listen(var L : TStrings; V : TSocket);<br>&nbsp; const<br>&nbsp; &nbsp; BufSize = 64000;<br>&nbsp; var<br>&nbsp; &nbsp; Buf : Array [0..BufSize] of Char;<br>&nbsp; &nbsp; S : TSocket;<br>&nbsp; &nbsp; UdpFrom : SockAddr_in;<br>&nbsp; &nbsp; UdpFromLen : Integer;<br>&nbsp; &nbsp; N,err:Integer;<br>&nbsp; &nbsp; I : Integer;<br>&nbsp; begin<br>&nbsp; &nbsp; S := V;<br>&nbsp; &nbsp; I := 0;<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; UdpFromLen := SizeOf(UdpFrom);<br>&nbsp; &nbsp; &nbsp; N := RecvFrom(S,Buf,BufSize,0,UdpFrom,UdpFromLen);<br>&nbsp; &nbsp; &nbsp; Err := WSAGetLastError();<br>&nbsp; &nbsp; &nbsp; if (N &gt;0) and (Err = 0) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Decode_Recv(L,Buf,N);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if I = 254 then I := 0;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; until I &gt;255;<br>&nbsp; end;<br><br>begin<br>&nbsp; if WsaStartUp(MakeWord(2,0),Ws) &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('WsaStartUp Error! Error: '+ IntToStr(WSAGetLastError)),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; Sock := Socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);<br>&nbsp; if Sock = INVALID_SOCKET then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('Socket Error! Error: '+ IntToStr(WSAGetLastError())),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; IP := GetLocalIP;<br><br>&nbsp; Addr_In.sin_family := AF_INET;<br>&nbsp; Addr_In.sin_port := Sql_Port;<br>&nbsp; Addr_In.sin_addr.S_addr := Inet_addr(PChar(IP));<br><br>&nbsp; New(SndBuf);<br>&nbsp; SndBuf^ := '0';<br>&nbsp; if SetSockOpt(Sock,SOL_SOCKET,SO_SNDBUF,SndBuf,SizeOf(SndBuf)) = SOCKET_ERROR then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('Set SO_SNDBUF Error! Error: '+ IntToStr(WSAGetLastError())),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; New(NoDelay);<br>&nbsp; NoDelay^ := Char(True);<br>&nbsp; if SetSockOpt(Sock, SOL_SOCKET,TCP_NODELAY,NoDelay,SizeOf(NoDelay)) = SOCKET_ERROR then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('Set TCP_NODELAY Error! Error: '+ IntToStr(WSAGetLastError())),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; New(Broadcast);<br>&nbsp; Broadcast^ := '1';<br>&nbsp; if SetSockOpt(sock, SOL_SOCKET, SO_BROADCAST, Broadcast, SizeOf(Broadcast))=SOCKET_ERROR then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('Set SO_BROADCAST Error! Error: '+ IntToStr(WSAGetLastError())),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; New(P);<br>&nbsp; P.L := L;<br>&nbsp; P.V := Sock;<br><br>&nbsp; CreateThread(nil,0,@Listen,P,0,Listener);<br><br>&nbsp; Buf := char(02);<br><br>&nbsp; if SendTo(Sock, Buf, SizeOf(buf), 0, addr_in, sizeof(addr_in))=SOCKET_ERROR then<br>&nbsp; begin<br>&nbsp; &nbsp; MessageBox(Application.Handle,PChar('Send Failed! Error: '+ IntToStr(WSAGetLastError())),'错误',MB_ICONERROR);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; WaitForSingleObject(Listener,5000);<br><br>&nbsp; WsaCleanUp;<br><br>end;<br><br>end.
 
你的C程序是用来探测端口的,而我的程序则是采用SQLDMO来的,不一样,<br>我的程序如果不注册为服务程序的话,那么运行很正常,但是一旦注册为服务程序后就只能看到本机的了,我自己还没有弄明白,不过如果你只是需要列举局域网中的SQL Server的话,可以用我的那个方法,;有什么疑问的话和我联系:Songyuehua@163.net<br>其实你看看App2001的那个地址就知道怎么做了<br>http://www.delphibbs.com/delphibbs/dispq.asp?LID=2057477<br>
 

Similar threads

I
回复
0
查看
729
import
I
I
回复
0
查看
830
import
I
I
回复
0
查看
851
import
I
I
回复
0
查看
1K
import
I
I
回复
0
查看
785
import
I
后退
顶部