LoginSignup
1
1

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > Indy > UDP > コマンド送信して応答を受信する > TIdUDPServerを使う例 > ASCIIコードのみ対応版

Last updated at Posted at 2017-02-22
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)

関連 http://qiita.com/7of9/items/a91a396f03fac9516814

コマンドを送信して応答を返すサーバがあるとする。
そのサーバに対してコマンドを送信して応答を受信する処理には何を使うのが良いのか。

TIdUDPClientを使う場合もあるが、以下のやり取りを見るとタイムアウト処理の考慮などからTIdUDPServerを使うという方法も考えられる。
http://stackoverflow.com/questions/10550440/delphi-indy-idudpclient-read-operation-no-data-returned

Alternatively, use TIdUDPServer instead. Set up its Bindings property with the local IP/Port pair(s) you want to receive data on, and then let its OnUDPRead event tell you whenever new data arrives from any remote IP/Port.

以下で実装できた。

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdSocketHandle.hpp>
#include <IdUDPBase.hpp>
#include <IdUDPServer.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TButton *Button1;
    TIdUDPServer *IdUDPServer1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
          TIdSocketHandle *ABinding);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    IdUDPServer1->DefaultPort = 6000;
    IdUDPServer1->Active = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String cmd = L"L_myIP";
    String ipadr = L"192.168.0.31";
    int port = 6000;

    IdUDPServer1->Send(ipadr, port, cmd, IndyTextEncoding(932) );

}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
          TIdSocketHandle *ABinding)
{
    String rcvdStr;
    Idglobal::_di_IIdTextEncoding encSJIS;

    encSJIS = IndyTextEncoding(932);

    int init_pos = 0;
    rcvdStr = encSJIS->GetString(AData, init_pos, AData.Length);
    int nop=1;
}
//---------------------------------------------------------------------------

Button1を押すとコマンドが送信され、応答がIdUDPServer1UDPRead()にて受信される。
( 例: L"\x02L_myIP,192.168.0.31\x03・" )

この方法では、TThreadや受信確認のための別処理が不要である点が良い。

ASCIIコードのみ対応

(追記 2017/02/23)

こちらのコードの場合、CodePage 932にしているため、ASCIIコードのみしか受信できない。
http://qiita.com/7of9/items/94844e82e3f94898f329

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1