J
jingtao
Unregistered / Unconfirmed
GUEST, unregistred user!
unit UdpBroadStr;
interface
uses
Windows,WinSock;
function MyUdpBroadStr(Port:integer;Str:String):Boolean;
implementation
function MyUdpBroadStr(Port:integer;Str:String):Boolean;
var WSData:TWSAData;
UdpSock:TSocket;
UdpSockAddr:TSockAddr;
i:integer;
BroadCast:Boolean;
Buf:Array[1..1024] of Char;
begin
Result:=False;
// if WSAStartup($101,{MakeWord(2,2),}WSData)<>0 then Exit; //初始化Wsock32.dll
UdpSock:=Socket(AF_INET,SOCK_DGRAM,0); //初始化socket
if UdpSock = SOCKET_ERROR then Exit;
BroadCast:=True;
if setsockopt(UdpSock,SOL_SOCKET,SO_BROADCAST, @BroadCast,sizeof(BroadCast))=SOCKET_ERROR then Exit;
//绑定要发送的Udp包的socketaddr_in结构
ZeroMemory(@UdpSockAddr,sizeof(sockaddr_in));
UdpSockAddr.sin_family:=AF_INET;
UdpSockAddr.sin_port:=htons(Port);
UdpSockAddr.sin_addr.S_addr:=INADDR_BROADCAST;
for i:=1 to Length(Str) do Buf:=Str;
Sendto(UdpSock,Buf,Length(Str),0,UdpSockAddr,sizeof(UdpSockAddr));//向网络广播Udp包
if UdpSock<>NULL then CloseSocket(UdpSock);
// WSACleanUp;
Result:=True;
end;
var
Re:integer;
Initialization
Re:=WSAStartup($101,Wsa); //初始化Wsock32.dll,MakeWord(2,2),
if Re<>0 then MessageBox(0,'WSAStartup Failed',MpgServerName,MB_ICONERROR);
finalization
WSACleanup;
end.
其中
if setsockopt(UdpSock,SOL_SOCKET,SO_BROADCAST, @BroadCast,sizeof(BroadCast))=SOCKET_ERROR then Exit;
在WIN2K下无问题
9X就出错.
WHY?
interface
uses
Windows,WinSock;
function MyUdpBroadStr(Port:integer;Str:String):Boolean;
implementation
function MyUdpBroadStr(Port:integer;Str:String):Boolean;
var WSData:TWSAData;
UdpSock:TSocket;
UdpSockAddr:TSockAddr;
i:integer;
BroadCast:Boolean;
Buf:Array[1..1024] of Char;
begin
Result:=False;
// if WSAStartup($101,{MakeWord(2,2),}WSData)<>0 then Exit; //初始化Wsock32.dll
UdpSock:=Socket(AF_INET,SOCK_DGRAM,0); //初始化socket
if UdpSock = SOCKET_ERROR then Exit;
BroadCast:=True;
if setsockopt(UdpSock,SOL_SOCKET,SO_BROADCAST, @BroadCast,sizeof(BroadCast))=SOCKET_ERROR then Exit;
//绑定要发送的Udp包的socketaddr_in结构
ZeroMemory(@UdpSockAddr,sizeof(sockaddr_in));
UdpSockAddr.sin_family:=AF_INET;
UdpSockAddr.sin_port:=htons(Port);
UdpSockAddr.sin_addr.S_addr:=INADDR_BROADCAST;
for i:=1 to Length(Str) do Buf:=Str;
Sendto(UdpSock,Buf,Length(Str),0,UdpSockAddr,sizeof(UdpSockAddr));//向网络广播Udp包
if UdpSock<>NULL then CloseSocket(UdpSock);
// WSACleanUp;
Result:=True;
end;
var
Re:integer;
Initialization
Re:=WSAStartup($101,Wsa); //初始化Wsock32.dll,MakeWord(2,2),
if Re<>0 then MessageBox(0,'WSAStartup Failed',MpgServerName,MB_ICONERROR);
finalization
WSACleanup;
end.
其中
if setsockopt(UdpSock,SOL_SOCKET,SO_BROADCAST, @BroadCast,sizeof(BroadCast))=SOCKET_ERROR then Exit;
在WIN2K下无问题
9X就出错.
WHY?