Q&A

  • UDP 로 통신을 하려면 어떻게 해야하나요?
안녕하세요

UDP 1024 PORT 로 통신하려고 하는데

일반 Server/Client Socket으로는  통신이 않돼나요?

그럼 어떻게 통신할 수 있나요

관련 자료 부탁드립니다.
1  COMMENTS
  • Profile
    마코디 2004.02.13 18:36
    UDP 통신을 할때 SOCKET 이나 INDY 컴포넌트를 사용하는데

    델파이 7에는 인디가 기본적으로 있으니까 그것을 이용하시면 편하게 쓸 수 있습니다. 관련자료는 인디 컴포넌트 사이트에 가시면 데모 소스 잘 되어 있구요..


    C/S 버전 하나 소스 올리지요.
    1024 PORT 를 사용하고 싶다면 포트 설정만 하시면 됩니다.

    {-----------------------------------------------------------------------------
    Demo Name: UDP Client
    Author:    <unknown - please contact me to take credit! - Allen O'Neill>
    Copyright: Indy Pit Crew
    Purpose:
    History:
    Date:      27/10/2002 01:00:36
    Checked with Indy version: 9.0 - Allen O'Neill - Springboard Technologies Ltd  - http://www.springboardtechnologies.com
    -----------------------------------------------------------------------------
    Notes:

    Simple UDP client demo

    }


    unit UDPClientMain;

    interface

    uses
      Windows, Messages, Graphics, Controls, Forms, Dialogs, IdWinsock2,   stdctrls,
      SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
      IdComponent, IdUDPBase, IdUDPClient, IdStack;

    type
      TUDPMainForm = class(TForm)
        SourceGroupBox: TGroupBox;
        HostNameLabel: TLabel;
        HostAddressLabel: TLabel;
        HostName: TLabel;
        HostAddress: TLabel;
        UDPAntiFreeze: TIdAntiFreeze;
        PortLabel: TLabel;
        Port: TLabel;
        DestinationLabel: TLabel;
        DestinationAddress: TLabel;
        BufferSizeLabel: TLabel;
        BufferSize: TLabel;
        UDPMemo: TMemo;
        SendButton: TButton;
        UDPClient: TIdUDPClient;
        procedure FormCreate(Sender: TObject);
        procedure SendButtonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      UDPMainForm: TUDPMainForm;

    implementation

    const
      HOSTNAMELENGTH = 80;
      RECIEVETIMEOUT = 5000; // milliseconds

    {$R *.DFM}

    procedure TUDPMainForm.FormCreate(Sender: TObject);
    begin
      Randomize; // remove if you want reproducible results.
      HostName.Caption := UDPClient.LocalName;
      HostAddress.Caption := GStack.LocalAddress;
      Port.Caption := IntToStr(UDPClient.Port);
      DestinationAddress.Caption := UDPClient.Host;
      BufferSize.Caption := IntToStr(UDPClient.BufferSize);
      UDPClient.ReceiveTimeout := RECIEVETIMEOUT;
    end;

    procedure TUDPMainForm.SendButtonClick(Sender: TObject);
    var
      MessageID: Integer;
      ThisMessage: String;
      ReceivedString: String;
    begin
      MessageID := Random(MAXINT);
      ThisMessage := 'Message: ' + IntToStr(MessageID);
      UDPMemo.Lines.Add('Sending ' + ThisMessage);
      UDPClient.Send(ThisMessage);
      ReceivedString := UDPClient.ReceiveString();
      if ReceivedString = '' then
        UDPMemo.Lines.Add('No response received from the server after ' + IntToStr(UDPClient.ReceiveTimeout) + ' millseconds.')
      else
        UDPMemo.Lines.Add('Received: ' + ReceivedString)
    end;

    end.


    {
    -----------------------------------------------------------------------------
    Demo Name: UDP Server
    Author:    <unknown - please contact me to take credit! - Allen O'Neill>
    Copyright: Indy Pit Crew
    Purpose:
    History:
    Date:      27/10/2002 01:00:36
    Checked with Indy version: 9.0 - Allen O'Neill - Springboard Technologies Ltd
      - http://www.springboardtechnologies.com
    -----------------------------------------------------------------------------
    Notes:
    Simple UDP server demo
    }

    unit UDPServerMain;

    interface

    uses
      Windows, Messages, Graphics, Controls, Forms, Dialogs, IdWinsock2, stdctrls,
      SysUtils, Classes, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
      IdComponent, IdUDPBase, IdUDPClient, IdStack, IdUDPServer, IdSocketHandle;


    type
      TUDPMainForm = class(TForm)
        SourceGroupBox: TGroupBox;
        HostNameLabel: TLabel;
        HostAddressLabel: TLabel;
        HostName: TLabel;
        HostAddress: TLabel;
        UDPServer: TIdUDPServer;
        UDPAntiFreeze: TIdAntiFreeze;
        PortLabel: TLabel;
        Port: TLabel;
        BufferSizeLabel: TLabel;
        BufferSize: TLabel;
        UDPMemo: TMemo;
        procedure FormCreate(Sender: TObject);
        procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      UDPMainForm: TUDPMainForm;

    implementation

    const
      HOSTNAMELENGTH = 80;

    {$R *.DFM}

    procedure TUDPMainForm.FormCreate(Sender: TObject);
    begin
      HostName.Caption := UDPServer.LocalName;
      HostAddress.Caption := GStack.LocalAddress;
      Port.Caption := IntToStr(UDPServer.DefaultPort);
      BufferSize.Caption := IntToStr(UDPServer.BufferSize);
      UDPServer.Active := True;
    end;

    procedure TUDPMainForm.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
    var
      DataStringStream: TStringStream;
      s: String;
    begin
      DataStringStream := TStringStream.Create('');
      try
        DataStringStream.CopyFrom(AData, AData.Size);
        UDPMemo.Lines.Add('Received "' + DataStringStream.DataString + '" from ' + ABinding.PeerIP + ' on port ' + IntToStr(ABinding.PeerPort));
        s := 'Replied from ' + UDPServer.LocalName + ' to "' + DataStringStream.DataString + '"';
        ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, s[1], Length(s));
      finally
        DataStringStream.Free;
      end;
    end;


    end.