Created
February 19, 2014 17:56
-
-
Save drgarcia1986/9097549 to your computer and use it in GitHub Desktop.
Classe feita em Delphi que encapsula um cliente UDP enviando e recebendo mensagens de um servidor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
procedure TForm1.Button1Click(Sender: TObject); | |
var | |
wPort: Word; | |
sIp: string; | |
sMsg: string; | |
begin | |
TUDPClient.GetInstance().EndOfProtocol := '.'; | |
sMsg := TUDPClient.GetInstance().SendBroadcast('UDP-MSG',sIp,wPort); | |
Memo1.Lines.Add(Format('[%s] [%d] : %s',[sIp,wPort,sMsg])); | |
end; | |
procedure TForm1.Button2Click(Sender: TObject); | |
var | |
sMsg: string; | |
begin | |
TUDPClient.GetInstance().EndOfProtocol := '.'; | |
sMsg := TUDPClient.GetInstance().SendMessage('UDP-MSG','127.0.0.1',49152); | |
Memo1.Lines.Add(sMsg); | |
end; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit UDP.Client; | |
interface | |
uses | |
IdUDPClient; | |
type | |
TUDPClient = class | |
strict private const | |
DEFAULT_UDP_PORT = 49152; | |
strict private | |
FidUDPClient : TIdUDPClient; | |
FUDPPort : word; | |
FEndOfProtocol : string; | |
class var FInstance : TUDPClient; | |
constructor Create(); | |
destructor Free(); | |
function ReceiveMessage(out AIpReceiver : string; out APortReceiver : Word):string; | |
private | |
class procedure ReleaseInstance(); | |
public | |
procedure SetUDPPort(const APort : word); | |
function SendBroadcast(const AMessage : string; out AIpReceiver : string; out APortReceiver : Word):string; | |
function SendMessage(const AMessage : string; const AHost : string;const APort : word):string; | |
property EndOfProtocol : string read FEndOfProtocol write FEndOfProtocol; | |
class function GetInstance() : TUDPClient; | |
end; | |
implementation | |
uses | |
System.SysUtils; | |
{ TUDPClient } | |
constructor TUDPClient.Create; | |
begin | |
Self.FidUDPClient := TIdUDPClient.Create(nil); | |
Self.FidUDPClient.ReceiveTimeout := 3000; | |
Self.FUDPPort := Self.DEFAULT_UDP_PORT; | |
end; | |
destructor TUDPClient.Free; | |
begin | |
Self.FidUDPClient.Free; | |
end; | |
class function TUDPClient.GetInstance: TUDPClient; | |
begin | |
if not Assigned(Self.FInstance) then | |
Self.FInstance := TUDPClient.create; | |
Result := Self.FInstance; | |
end; | |
function TUDPClient.ReceiveMessage(out AIpReceiver: string; | |
out APortReceiver: Word): string; | |
var | |
sBuffer : string; | |
begin | |
while true do | |
begin | |
sBuffer := Self.FidUDPClient.ReceiveString(AIpReceiver,APortReceiver); | |
if (sBuffer = EmptyStr) or (sBuffer = Self.FEndOfProtocol) then | |
break; | |
Result := Concat(Result,sBuffer); | |
end; | |
end; | |
class procedure TUDPClient.ReleaseInstance; | |
begin | |
if Assigned(Self.FInstance) then | |
Self.FInstance.free; | |
end; | |
function TUDPClient.SendBroadcast(const AMessage: string; | |
out AIpReceiver: string; out APortReceiver: Word): string; | |
begin | |
Self.FidUDPClient.Broadcast(AMessage,Self.FUDPPort); | |
Result := Self.ReceiveMessage(AIpReceiver,APortReceiver); | |
end; | |
function TUDPClient.SendMessage(const AMessage, AHost: string; | |
const APort: word): string; | |
var | |
sHost : string; | |
wPorta : word; | |
begin | |
Self.FidUDPClient.Send(AHost,APort,AMessage); | |
Result := Self.ReceiveMessage(sHost,wPorta); | |
end; | |
procedure TUDPClient.SetUDPPort(const APort: word); | |
begin | |
Self.FUDPPort := APort; | |
end; | |
initialization | |
finalization | |
TUDPClient.ReleaseInstance(); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment