此代码在 Linux 和 Windows XP 上运行良好,但它总是无法在 Windows 7 64 位和 Windows Server 2008 上添加多播成员资格。
uses
Classes, Sockets,
{$IFDEF MSWINDOWS}
Windows,
Winsock,
{$ELSE}
BaseUnix,
UnixType,
{$ENDIF}
Sysutils;
type
TIP_mreq = packed record
imr_multiaddr: in_addr; // IP multicast address of group
imr_interface: in_addr; // local IP address of interface
end;
var
ASock_Addr : SockAddr_In;
FSocket : LongInt;
ipmreq : TIP_mreq;
MasterFD,
TempFD : TFDSet;
TestInt : Integer;
Timeout : TTimeVal;
begin
FSocket := fpSocket(AF_INET, LongInt(SOCK_DGRAM), 0);
if (FSocket > 0) then
begin
fpSetSockOpt(FSocket, SOL_SOCKET, SO_REUSEADDR, PChar('True'), Length('True'));
FillByte(ASock_Addr, SizeOf(SockAddr_In), 0);
ASock_Addr.sin_family := AF_INET;
ASock_Addr.sin_port := HtoNS(15510);
ASock_Addr.sin_addr.S_addr := INADDR_ANY;
if (fpbind(FSocket, @ASock_Addr, SizeOf(SockAddr_In)) = 0) then
begin
FillByte(ipmreq, SizeOf(Tip_mreq),0);
ipmreq.imr_multiaddr.s_addr := StrToNetAddr('224.126.126.126').s_addr;
ipmreq.imr_interface.s_addr := StrToNetAddr('10.0.0.3').s_addr;
fpSetsockOpt(FSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, @ipmreq, SizeOf(ipmreq));
{$IFDEF MSWINDOWS}
FD_SET(FSocket, MasterFD);
{$ELSE}
fpFD_SET(FSocket, MasterFD);
{$ENDIF}
此代码中的 fpSetSockOpt 在 Windows 7 和 Server 2008 上将始终返回 -1