Sort IP addresses in a cross-platform way lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- JD · 2014-02-18 12:37 Hi there everyone, I have a Windows function that sorts IP addresses & I'm trying to make it cross-platform & I need some help with that. The function is as shown below. It relies on Winsock so I need the equivalent on other platforms e.g Linux: Code: [Select]  { Helper functions }   function IPSort(AString1, AString2: string): integer;   // Modified original source: http:www.delphipages.com/forum/showthread.php?t=104191   var     a, b: TInAddr;   begin     //     {$IFDEF MSWINDOWS}     a.S_addr := inet_addr(PChar(AString1));     b.S_addr := inet_addr(PChar(AString2));     //     if ntohl(a.S_addr) > ntohl(b.S_addr) then       Result := -1     else if ntohl(b.S_addr) > ntohl(a.S_addr) then       Result := 1     else       Result := 0;     {$ELSE}     // this part is untested and fails to compile bcos of in-addr     a.S_addr := in_addr(PChar(AInt1));     b.S_addr := in_addr(PChar(AInt2));     //     if ntohl(a.S_addr) > ntohl(b.S_addr) then       Result := -1     else if ntohl(b.S_addr) > ntohl(a.S_addr) then       Result := 1     else       Result := 0;     {$ENDIF}   end; I use Code: [Select]{$IFDEF MSWINDOWS}Winsock,{$ELSE}Sockets,{$ENDIF} in the uses clause. Thanks, JD ---------------------------------------------------------------------- marcov · 2014-02-18 12:54 winsock has no *nix equivalent. But while sockets originates on *nix, it is supported on Windows for over 8 years. Simply change everything to use sockets. ---------------------------------------------------------------------- JD · 2014-02-18 15:18 Quote from: marcov on February 18, 2014, 01:54:55 pmwinsock has no *nix equivalent. But while sockets originates on *nix, it is supported on Windows for over 8 years. Simply change everything to use sockets. How do I do that? Thanks. ---------------------------------------------------------------------- engkin · 2014-02-18 16:15 Unit Sockets has ntohl and StrToHostAddr. ---------------------------------------------------------------------- JD · 2014-02-19 08:44 Quote from: engkin on February 18, 2014, 05:15:53 pmUnit Sockets has ntohl and StrToHostAddr. So if I understand you, changing in_addr(PChar(AInt1)) to StrToHostAddr(PChar(AInt1)) ought to do the trick since StrToHostAddr is a function which returns an in_addr value? That seems to be the only change necessary. ---------------------------------------------------------------------- Leledumbo · 2014-02-19 10:44 Quote from: JD on February 18, 2014, 04:18:08 pmQuote from: marcov on February 18, 2014, 01:54:55 pmwinsock has no *nix equivalent. But while sockets originates on *nix, it is supported on Windows for over 8 years. Simply change everything to use sockets. How do I do that? Thanks. Change: Code: [Select]{$IFDEF MSWINDOWS}Winsock,{$ELSE}Sockets,{$ENDIF}to: Code: [Select]Sockets ---------------------------------------------------------------------- JD · 2014-02-20 05:51 Quote from: Leledumbo on February 19, 2014, 11:44:07 amQuote from: JD on February 18, 2014, 04:18:08 pmQuote from: marcov on February 18, 2014, 01:54:55 pmwinsock has no *nix equivalent. But while sockets originates on *nix, it is supported on Windows for over 8 years. Simply change everything to use sockets. How do I do that? Thanks. Change: Code: [Select]{$IFDEF MSWINDOWS}Winsock,{$ELSE}Sockets,{$ENDIF}to: Code: [Select]Sockets Ah. I see. Thanks.