Posiblemente dependa de la versión del delphi que uses.
La versión Cliente que he probado con delphi7 y Berlin es esta:
Código Delphi
[-]unit Unit2;
interface
uses
Windows, WinSock, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
TMIB_IPADDRROW = packed record
dwAddr: DWORD;
dwIndex: DWORD;
dwMask: DWORD;
dwBCastAddr: DWORD;
dwReasmSize: DWORD;
unused1: SmallInt;
wType: SmallInt;
end;
TMIB_IPADDRTABLE = record
dwNumEntries: DWORD;
table: array[0..0] of TMIB_IPADDRROW;
end;
PMIB_IPADDRTABLE = ^TMIB_IPADDRTABLE;
function GetIpAddrTable(IpAddrTable: PMIB_IPADDRTABLE; pdwSize: PULONG;
Order: BOOL): DWORD; stdcall; external 'iphlpapi.dll' name 'GetIpAddrTable';
var
Form2: TForm2;
implementation
{$R *.dfm}
function GetCurrentIP: DWORD;
var
Wsa: WSADATA;
Name: array[0..255] of AnsiChar;
hostinfo: PHOSTENT;
begin
Result:= 0;
FillChar(Wsa, SizeOf(WSAData), 0);
if WSAStartup(MAKEWORD(2, 2), Wsa) = 0 then
begin
if gethostname(Name, SizeOf(Name)) = 0 then
begin
hostinfo:= gethostbyname(Name);
if hostinfo <> nil then
Result:= PDWORD(hostinfo^.h_addr_list^)^;
WSACleanup;
end;
end;
end;
function GetBrodcastAddress: AnsiString;
var
pIPAddrTable: PMIB_IPADDRTABLE;
dwSize: DWORD;
i: integer;
BroadCastInAddr: IN_ADDR;
begin
BroadCastInAddr.S_addr:= 0;
dwSize:= 0;
GetIpAddrTable(nil, @dwSize, true);
GetMem(pIPAddrTable, dwSize);
if pIPAddrTable<>nil then
begin
if GetIpAddrTable(pIPAddrTable, @dwSize, true) = NO_ERROR then
for i:=0 to pIPAddrTable^.dwNumEntries-1 do
begin
if GetCurrentIP = pIPAddrTable^.table[i].dwAddr then
begin
BroadCastInAddr.S_addr:= pIPAddrTable^.table[i].dwAddr or not pIPAddrTable^.table[i].dwMask;
break;
end;
end;
FreeMem(pIPAddrTable);
end;
Result:= inet_ntoa(BroadCastInAddr);
end;
procedure SendUDP(Msg: AnsiString; IP: AnsiString; Port: WORD);
var
Wsa: WSADATA;
S: TSocket;
Addr: WinSock.sockaddr_in;
Host: PHostent;
begin
if WinSock.WSAStartup(MAKEWORD(2, 2), Wsa) = 0 then
try
S:= Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if S <> INVALID_SOCKET then
begin
Host:= WinSock.gethostbyname(PAnsiCHAR(IP));
Addr.sin_family:= AF_INET;
Addr.sin_addr.S_addr:= PInAddr(Host.h_addr_list^)^.S_addr;
Addr.sin_port:= WinSock.htons(Port);
WinSock.Sendto(S, PAnsiChar(Msg)^, Length(Msg), 0, Addr, SizeOf(sockaddr_in));
end;
finally
WinSock.WSACleanup();
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
SendUDP('Hola', GetBrodcastAddress, 9090);
end;
end.
Ten en cuenta que el UNICODE juega malas pasadas. Subo un proyecto con fuentes y ejecutables compilados.