LoginSignup
0
1

More than 3 years have passed since last update.

サーバ1対N通信について勉強中

Posted at

現在、TCPサーバについて勉強中です。
サーバソフトで1つの受信ポートに複数のクライアントが接続されるようなサーバに興味があり、色々と調べ調べていくと

”1対N通信”という言葉とこのサイトに辿り着きました。
https://qiita.com/7of9/items/8925cce71cbf43448d55

Unit.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)
{
    IdTCPServer1->DefaultPort = 7000;
    IdTCPServer1->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
    AnsiString rcvdStr;

    // <LF>終端受信
    rcvdStr = AContext->Connection->IOHandler->ReadLn(IndyTextEncoding(932));

    TList *threads;
    TIdContext *ac;

    threads = IdTCPServer1->Contexts->LockList();

    for(int idx=0; idx < threads->Count; idx++) {
        ac = reinterpret_cast<TIdContext *>(threads->Items[idx]);
        ac->Connection->IOHandler->WriteLn(rcvdStr);
    }

    IdTCPServer1->Contexts->UnlockList();
}
//---------------------------------------------------------------------------

C++ Builer Communityをインストールして、上記に掲載されたプログラムをコードして動かしてみて、複数のクライアントを同時に接続できていることを

netstat -nao | fine ":12000"

みたいな感じでnetstatで確認することもできました。

ただ、”1対N通信”で使用される

    TList *threads;
    //--------(中略)------------    
    threads = IdTCPServer1->Contexts->LockList();

1対1通信と1対N通信の違いはこのスレッドを使っているということだと思うのですが、
このスレッドというのはどういう風にイメージしたら良いのかよくわかりません。

このスレッドというTList型のポインターというのは、クライアントが増える度に1つずつ増えていくのでしょうか?

それとも何かしらサーバにデータが受信されるとそれごとに1つずつ増えるという感じなのでしょうか?

for(int idx=0; idx < threads->Count; idx++) {
--------(中略)------------ 
}

このコード中にあるfor文の中のthreads->Countというのは、サーバに現在接続されているクライアント数という意味なるのでしょうか?

どうぞ、ご教示の程よろしくお願い致します。

0
1
20

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
0
1