I have the code to listen on a UDP port when the first button
is clicked, and send a packet when the second button is clicked.
It works fine except when the second button is clicked, it sends
two packets (I captured that with Ethereal.) Is there anything
I'm not doing right?

-- 
Best regards,
Jack


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WinSock, WSocket;

type
  TForm1 = class(TForm)
    WSocket1: TWSocket;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    btnSend: TButton;
    edtDstIP: TEdit;
    edtSvrPort: TEdit;
    btnListen: TButton;
    edtPort: TEdit;
    procedure WSocket1DataAvailable(Sender: TObject; ErrCode: Word);
    procedure btnSendClick(Sender: TObject);
    procedure btnListenClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnListenClick(Sender: TObject);
begin
  WSocket1.Proto := 'udp';
  WSocket1.Port := edtSvrPort.Text;
  WSocket1.Addr := '0.0.0.0';
  WSocket1.Listen;
  btnListen.Enabled := False;
end;

procedure TForm1.btnSendClick(Sender: TObject);
var Src: TSockAddrIn; SrcLen: Integer;
begin
  Src.sin_family := AF_INET;
  Src.sin_addr.S_addr := inet_addr(PChar(edtDstIp.Text));
  Src.sin_port := hToNs(StrToInt(edtPort.Text));
  SrcLen := SizeOf(Src);
  WSocket1.SendTo(Src, SrcLen, PChar('Hello'), 5);
end;

procedure TForm1.WSocket1DataAvailable(Sender: TObject; ErrCode: Word);
begin
  if ErrCode = 0 then Memo1.Lines.Add(WSocket1.ReceiveStr)
  else Memo1.Lines.Add('WSocket1DataAvailable() error = ' + IntToStr(ErrCode));
end;

end.


-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to