Windows 7上的多播套接字

时间:2013-04-04 13:04:17

标签: windows delphi sockets multicast freepascal

此代码在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

1 个答案:

答案 0 :(得分:4)

我明白了。这是与这些操作系统相关的Freepascal问题。通过将fpBind和fpSetSockOpt更改为其Winsock调用(Bind和SetSockOpt),它可以在这些操作系统中运行。