unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Sockets, ExtCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
ServerUdpSocket: TUdpSocket;
ClientUdpSocket: TUdpSocket;
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerUdpSocketReceive(Sender: TObject; Buf: PAnsiChar;
var DataLen: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ServerUdpSocketConnect(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var buf:
string;
begin
buf:=Memo1.Text;
ClientUdpSocket.SendBuf(buf, sizeof(buf));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
try
with ServerUdpSocket
do // Empfang
begin
LocalPort:='
4000';
Localhost:='
127.0.0.1';
Active:=True;
Open;
end;
except
ServerUdpSocket.Free;
end;
try
with ClientUdpSocket
do // Senden
begin
RemotePort:='
4000';
RemoteHost:='
127.0.0.1';
Active:=True;
Open;
end;
except
ClientUdpSocket.Free;
end;
end;
procedure TForm1.ServerUdpSocketReceive(Sender: TObject; Buf: PAnsiChar;
var DataLen: Integer);
var Buf_:
string;
begin
ServerUdpSocket.ReceiveBuf(Buf_, ServerUdpSocket.BytesReceived); ;
Memo2.Text:=Buf_
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var buf:
string;
begin
Memo1.Text:=inttostr(GetTickCount);
buf:=Memo1.Text;
ClientUdpSocket.SendBuf(buf, sizeof(buf));
end;
procedure TForm1.ServerUdpSocketConnect(Sender: TObject);
begin
Label2.Caption:=ServerUdpSocket.RemoteHost;
end;
end.