Article View: borland.public.cppbuilder.internet.socket
Article #150Re: Hair 0, Indy10 1
From: Gene Buckle
Date: Tue, 03 Jun 2008 21:21
Date: Tue, 03 Jun 2008 21:21
294 lines
8777 bytes
8777 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
> "Gene Buckle" <geneb@grumble.deltasoft.com> wrote in message
> news:4845a18d@newsgroups.borland.com...
>
>> Is this, or is this not the correct way to use TIdTCPClient:
>
> In general, yes. But earlier you showed that you are using it inside of a
> classSimulator class. I'm more interested in seeing how THAT class is being
> used. In particular, you also showed a call to ShowMessage(), but you did
> not indicate WHERE that was being called from exactly. Can you please
> provide a concrete compilable code snippet that demonstrates your problem in
> a reproducable way?
>
classSimulator is declared as a global in the code file that contains
WinMain().
It's declared:
classSimulator *Simulator;
Other files that reference it contain:
extern classSimulator *Simulator;
Note that the class functions perfectly in the absense of TIdTCPClient.
For example, in my main form, I execute this code:
Simulator->TCPClient->Sendln("data");
param_count = FunctionConfig->g_SimParams.size();
Simulator->busy = true; // I'm busy! Quit bugging me and get off my damn lawn! .
if (Simulator->g_SimParams.size() == 0) {
// we've not loaded this up yet...
for (zed = 0; zed < param_count; zed++) {
Application->ProcessMessages();
const string &key = FunctionConfig->g_SimParams.get(zed);
const string &value = FunctionConfig->g_SimParams.get(key);
cmd = value.c_str();
cmd = "get " + cmd;
Simulator->TCPClient->Sendln(cmd);
reply = Simulator->TCPClient->Receiveln('\n');
if (reply != '\r') {
Simulator->g_SimParams.set(value.c_str(),reply.c_str());
} else {
Simulator->g_SimParams.set(value.c_str(),"invalid");
}
}
}
This works flawlessly.
>
> But you did not show how the classSimulator itself was being used.
>
See above.
>> bool init_done;
>
> You should always intialize your globals.
>
> bool init_done = false;
>
You mean like I was doing in the constructor?:
__fastcall classSimulator::classSimulator() {
busy = false;
init_done = false;
SimulatorType = -1;
g_SimParams.clear();
g_SimParamsHold.clear();
}
>> void __fastcall classSimulator::InitCommSystem() {
>> if (!init_done) {
>> busy = true;
>> TCPClient = new TIdTCPClient(NULL);
>> TCPClient->OnDisconnected = MainForm->DisconnectHandler;
>>
>> busy = false;
>> init_done = true;
>> }
>> }
>
> Where is TCPClient actually declared? Is it global, or is it a member of
> classSimulator? Where is InitCommSystem() being called from?
>
It's declared in the header file for classSimulator. Like I mentioned earlier,
TCPClient is declared:
TIdTCPClient *TCPClient;
Where InitCommSystem() is being called from is irrelevant as it's simply a
a function and works as it should using TTCPClient:
void __fastcall classSimulator::InitCommSystem() {
if (!init_done) {
busy = true;
TCPClient = new TTcpClient(NULL);
TCPClient->OnDisconnect = MainForm->DisconnectHandler;
busy = false;
init_done = true;
}
}
>> } catch (Exception &ex) {
>
> You should always catch exceptions by 'const' reference instead:
>
> } catch (const Exception &ex) {
>
Noted, thanks. Why const?
>> if (TCPClient->Connected()) {
>> TCPClient->Disconnect();
>
> You don't need to call Connected() first.
>
Why bother calling Disconnect if I'm not connected to begin with? :)
>> void __fastcall classSimulator::CloseCommSystem() {
>> TCPClient->Free();
>> }
>
> Where is CloseCommSystem() called from?
>
Irrelevant.
> Never call Free() directly. Use 'delete' instead, especially since you used
> 'new' to allocate it. And be sure to reset your pointer back to NULL as
> well:
>
> void __fastcall classSimulator::CloseCommSystem()
> {
> delete TCPClient;
> TCPClient = NULL;
> }
Is there any valid instance to use Free() instead of delete?
Is there an example anywhere of a C++ program that uses TIdTCPClient that
actually works? I have seen _zero_ C++ examples for this library.
The documentation is written from the standpoint that everyone that sees
it must be a C++ master, therefore no examples are neccessary.
FYI, attached is the current version of classSimulator, both cpp and header.
It works without error.
#ifndef classSimulatorH
#define classSimulatorH
#include "cockpitmaster.h"
//#include <IdBaseComponent.hpp>
//#include <IdComponent.hpp>
//#include <IdIOHandler.hpp>
//#include <IdIOHandlerStream.hpp>
//#include <IdTCPClient.hpp>
//#include <IdTCPConnection.hpp>
#include <Sockets.hpp>
#include "classUnsortedMap.h"
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef std::map<std::string,std::string> StringMap;
class classSimulator {
public:
__fastcall classSimulator();
__fastcall ~classSimulator();
void __fastcall InitCommSystem();
void __fastcall CloseCommSystem();
bool __fastcall StartComms();
void __fastcall StopComms();
bool __fastcall GetBoolValue(AnsiString function_name);
void __fastcall SetBoolValue(AnsiString function_name, bool value);
bool busy; // flag used to hold threads at bay until the current
// operation is completed.
AnsiString port;
AnsiString host;
TTcpClient *TCPClient;
int SimulatorType;
classUnsortedMap g_SimParams;
classUnsortedMap g_SimParamsHold;
private:
};
//---------------------------------------------------------------------------
#endif
#pragma hdrstop
#include "classSimulator.h"
#include "defines.h"
#include "frmMain.h"
bool init_done;
__fastcall classSimulator::classSimulator() {
busy = false;
init_done = false;
SimulatorType = -1;
g_SimParams.clear();
g_SimParamsHold.clear();
}
__fastcall classSimulator::~classSimulator() {
}
void __fastcall classSimulator::InitCommSystem() {
if (!init_done) {
busy = true;
TCPClient = new TTcpClient(NULL);
TCPClient->OnDisconnect = MainForm->DisconnectHandler;
busy = false;
init_done = true;
}
}
bool __fastcall classSimulator::StartComms() {
// connects to the selected simulator
if ((port == "") || (host == "")) {
return false;
}
TCPClient->RemotePort = port;
TCPClient->RemoteHost = host;
try {
if (TCPClient->Connect()) {
// sim specific connection processing?...
return true;
}
//return true;
} catch (const Exception &ex) {
ShowMessage(ex.Message);
return false;
}
}
void __fastcall classSimulator::StopComms() {
// disconnects from the selected simulator
if (TCPClient->Connected) {
TCPClient->Disconnect();
}
}
void __fastcall classSimulator::CloseCommSystem() {
delete TCPClient;
TCPClient = NULL;
}
bool __fastcall GetBoolValue(AnsiString function_name) {
}
void __fastcall SetBoolValue(AnsiString function_name, bool value) {
}
--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
Technical Details
Message-ID:
<4845b5dc@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feed.tweaknews.nl!83.80.28.12.MISMATCH!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder1.xlned.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<48406d41@newsgroups.borland.com> <48407d4c$1@newsgroups.borland.com> <48408535@newsgroups.borland.com> <48442d6c$1@newsgroups.borland.com> <4845851e@newsgroups.borland.com> <48459486$1@newsgroups.borland.com> <4845a18d@newsgroups.borland.com> <4845abc7$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,9339fc05f2f275c
X-Google-Attributes: gid11508e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Message-ID: <4845b5dc@newsgroups.borland.com>
From: Gene Buckle <geneb@grumble.deltasoft.com>
Subject: Re: Hair 0, Indy10 1
Newsgroups: borland.public.cppbuilder.internet.socket
References: <48406d41@newsgroups.borland.com> <48407d4c$1@newsgroups.borland.com> <48408535@newsgroups.borland.com> <48442d6c$1@newsgroups.borland.com> <4845851e@newsgroups.borland.com> <48459486$1@newsgroups.borland.com> <4845a18d@newsgroups.borland.com> <4845abc7$1@newsgroups.borland.com>
User-Agent: tin/1.8.2-20060425 ("Shillay") (UNIX) (Linux/2.6.23.15-80.fc7 (i686))
NNTP-Posting-Host: 199.254.199.46
Date: 3 Jun 2008 14:21:32 -0700
X-Trace: newsgroups.borland.com 1212528092 199.254.199.46 (3 Jun 2008 14:21:32 -0700)
Lines: 294
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feed.tweaknews.nl!83.80.28.12.MISMATCH!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder1.xlned.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail