两个Indy UDP服务器之间的连接

时间:2018-11-01 15:33:10

标签: udp c++builder indy

我正在使用RAD Studio 10.2和来自Indy 10的两个TIdUDPServer实例。

我在Windows 10上运行我的程序,并检查已发送和已接收包的计数器,但是没有收到包。同时,我通过Wireshark看到它们进入了PC,但是第二个TIdUDPServer没有收到软件包。为什么?

这是我的代码:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

typedef struct {
    char Data[10000];
} struct_Buffer;

int i = 0;
int n = 0;
int k = 0;
int TxSize = 1400;
char TxData;
struct_Buffer TxBuffer;

AnsiString ServerIP1 = "192.168.10.1";
AnsiString ServerIP2 = "192.168.10.2";
TBytes Buffer;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

class TMyQueueProc1 : public TCppInterfacedObject<TThreadProcedure>
{
private:
    int m_counter;
    TIdBytes m_bytes;

public:
    TMyQueueProc1(int ACounter, const TIdBytes &AData) : m_counter(ACounter), m_bytes(AData) {}
    INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);

    void __fastcall Invoke()
    {
        Form1->Label1->Caption = "Rx " + IntToStr(m_counter);
    }
};

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    try {
        TIdSocketHandle *SocketHandle_Server = Form1->IdUDPServer1->Bindings->Add();
        SocketHandle_Server->IP = ServerIP1;
        SocketHandle_Server->Port = 4004;
        Form1->IdUDPServer1->Active = true;
    }
    catch(Exception *ex) {
        ShowMessage("IdUDPServer1 start error!");
    }

    try {
        TIdSocketHandle *SocketHandle_Echo = Form1->IdUDPServer2->Bindings->Add();
        SocketHandle_Echo->IP = ServerIP2;
        SocketHandle_Echo->Port = 4004;
        Form1->IdUDPServer2->Active = true;
    }
    catch(Exception *ex) {
        ShowMessage("IdUDPServer2 start error!");
    }

    Timer1->Interval = 100;
    Timer1->Enabled = true;

    Label3->Caption = "IdUPDServer1: " + ServerIP1;
    Label4->Caption = "IdUDPServer2: " + ServerIP2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    TxData++;
    if (TxData == 255) TxData = 0;
    for (k = 0; k < TxSize; k++) TxBuffer.Data[k] = TxData;

    Buffer = RawToBytes(&TxBuffer.Data[0], TxSize);
    Form1->IdUDPServer1->SendBuffer(ServerIP2, 4004, Buffer);

    n++;
    Label2->Caption = "Tx " + IntToStr(n);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer2UDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
          TIdSocketHandle *ABinding)
{
    i++;
    TThread::Queue(NULL, _di_TThreadProcedure(new TMyQueueProc1(i, AData)));
}
//---------------------------------------------------------------------------

0 个答案:

没有答案