当前位置: 动力学知识库 > 问答 > 编程问答 >

delphi - Multicast socket on Windows 7

问题描述:

this code works fine on Linux and Windows XP, however it always fails to add multicast membership on Windows 7 64bit and 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 in this code will always return -1 on Windows 7 and Server 2008

网友答案:

I figured it out. This is a Freepascal issue related to those operating systems. By changing fpBind and fpSetSockOpt to their Winsock calls (Bind and SetSockOpt) it does work in these operating systems.

分享给朋友:
您可能感兴趣的文章:
随机阅读: