🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2366

TIdUDPServer (and Client)

#2366
From: Paul
Date: Sun, 28 May 2006 09:29
95 lines
3615 bytes
Hi,

I'm trying to get an idea of how Indy(9) TIdUDPServer and Client works and 
have created a small test application.

I have got the client sending data to the server, and this seems ok (the 
server event handler, and client thread extract is below).  What I don't 
understand or know how to achieve is the client receiving data back from the 
server.  I've not managed to get this to work so far.

Since the UDP protocol is connectionless, the read event must be at some 
mid-point in the process of receiving the client data.  So can the server 
only respond in read event handler, using the socket binding information to 
know where to return the data?

I've also noticed that when the server sends data, it receives its own data 
(when using 127.0.0.1 addresses for both ends).

Last question, if I want two PCs to communicate async, do I need both ends 
to be server and clients?

Paul


//---------------------------------------------------------------------------
// server form

void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
      TStream *AData, TIdSocketHandle *ABinding)
{
    try
    {
        int BufSize = AData->Size;
        int BufIndx = 0;
        char * Buf = new char[BufSize];

        while (BufSize > 0)
        {
            int iRead = AData->Read( &Buf[BufIndx], BufSize );

            if( iRead > 0 )
            {
                // success
                BufIndx += iRead;
                BufSize -= iRead;
            }
            else
            {
                // fail
                BufSize = 0;
            }
        }

        // process buf
        Memo1->Lines->Add(Buf);

        // does not work???
        char msg [] = "Server: Testing Loopback UDP to Client";
        ABinding->Send(msg, sizeof(msg),0) ;

        // delete buf
        delete [] Buf;
    }
    catch(const Exception &)
    {
        ;
    }
}

//---------------------------------------------------------------------------
// client thread....

            if (IdUDPClient)
            {
                try
                {
                    char msg [] = "Client: Testing Loopback UDP to Server";
                    IdUDPClient->SendBuffer( msg, sizeof(msg) );

                    char rxd [100];
                    int  rxl = 100;
                    AnsiString peer = "";
                    int peerport = 0;

                    int len = IdUDPClient->ReceiveBuffer( rxd, rxl, peer, 
peerport, 100 );
                    if (len)
                    {
                        AnsiString str = len;
                    }
                }
                catch (...)
                {
                }
            }
Technical Details
Message-ID: <44796d7e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,826a7ca2cbe06100,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Paul" <plmacca@clara.co.uk>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: TIdUDPServer (and Client)
Date: Sun, 28 May 2006 10:29:57 +0100
Lines: 97
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
NNTP-Posting-Host: 86.133.178.7
Message-ID: <44796d7e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1148808574 86.133.178.7 (28 May 2006 02:29:34 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail