🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TCP Client/Server comms using Indy 9

#5384
From: Remy Lebeau (TeamB)
Date: Mon, 30 Aug 2004 17:16
104 lines
4305 bytes
"Alfred Thomas" <alfred@fischerint.co.za> wrote in message
news:4132e67f$1@newsgroups.borland.com...

> I need to write an application using Indy's TIdTCPClient and TIdTCPServer.
> All I need is for the server to accept a connection and receive a text
string,
> and the client to make a connection and send a text string out.

That is very simple to do.  Just have the server call the connection's
ReadLn() method in the OnExecute event, and the client call its WriteLn()
method after connecting to the server.  For example:

    --- server ---

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        AnsiString Buffer = AThread->Connection->ReadLn(LF,
IdTimeoutDefault, -1);
        RequestEngine->RunQuery("X1001", Buffer);
        AThread->Connection->WriteLn("Some Response String Here");
    }


    --- client ---

    // set up the client as needed...
    IdTCPClient1->Connect(5000);
    //...
    IdTCPClient1->WriteLn("Some Query String Here");
    AnsiString Response = IdTCPClient1->ReadLn(LF, IdTimeoutDefault, -1);
    // use Response as needed ...
    IdTCPClient1->Disconnect();


Alternatively:

    --- server ---

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        AnsiString Buffer = AThread->Connection->CurrentReadBuffer();
        RequestEngine->RunQuery("X1001", Buffer);
        AThread->Connection->WriteLn("Some Response String Here");
    }


    --- client ---

    // set up the client as needed...
    IdTCPClient1->Connect(5000);
    //...
    IdTCPClient1->Write("Some Query String Here");
    AnsiString Response = IdTCPClient1->CurrentReadBuffer();
    // use Response as needed ...
    IdTCPClient1->Disconnect();


Alternatively:

    --- server ---

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        int length = AThread->Connection->ReadInteger(true);
        AnsiString Buffer = AThread->Connection->ReadString(length);
        RequestEngine->RunQuery("X1001", Buffer);
        AnsiString Response = "Some Response String Here";
        AThread->Connection->WriteInteger(Response.Length(), true);
        AThread->Connection->Write(Response);
    }


    --- client ---

    // set up the client as needed...
    IdTCPClient1->Connect(5000);
    //...
    AnsiString Query = "Some Query String Here";
    IdTCPClient1->WriteInteger(Query.Length(), true);
    IdTCPClient1->Write(Query);
    int length = IdTCPClient1->ReadInteger(true);
    AnsiString Response = IdTCPClient1->ReadString(length);
    // use Response as needed ...
    IdTCPClient1->Disconnect();


> Im having great difficulties with indefinate time-outs, exceptions etc.

Well, for one thing, your client is trying to read a string back from the
server, but your server code does not appear to ever actually send a string
back to the client at all.  Also, you have two other problems in your server
code - 1) you are catching all exceptions and then discarding them, so the
server will never know about socket errors that occur, and 2) you are using
ShowMessage(), which is not thread-safe.  The OnExecute event is triggered
in the context of a worker thread, not the main VCL thread.

> I have seen other demos where ReadLn is used and I have tried that,
> with no success.

Why not?  Please be more specific.


Gambit
Technical Details
Message-ID: <413360a6$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4132e67f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,342abc5fb29e757e
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4132e67f$1@newsgroups.borland.com>
Subject: Re: TCP Client/Server comms using Indy 9
Date: Mon, 30 Aug 2004 10:16:47 -0700
Lines: 106
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <413360a6$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1093886118 68.69.17.41 (30 Aug 2004 10:15:18 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail