Builder 2010 - Indy TcpClient / TcpServer examples

Hello

I am a newbie here so please go easy on me if this is not the correct place to ask this.
I have spent weeks and weeks googling for examples of using these Indy 10 components with Builder 2010.
So far every link that says it is a builder example, winds up being Delphi and doesn't really explain how these components work properly.

The Indy page itself only has delphi examples and not very good ones at that.

Previously I have used ICS with builder 2007 but their latest release gives me all sorts of errors under builder 2010 and their web pag is badly lagging the product version with old outdated examples etc.

Before I toss both of these and just go back to basic socket programming of my own, can anyone please point me in the direction of a GOOD example of using the TCP client server.
Any examples I have found in builder simply show connect,send a single packet, receive a single packet, disconnect.
Like most people I wish to connect, stay online and communicate packets both ways for an extended period of time and then disconnect.

Thanks in advance for any direction you can give, I am beginning to feel that upgrading to builder 2010 was not such a good idea since all the components I used to use now fail me and code that was doing a good job can no longer be used.
Even more frustrating now that embarcadero are throwing in full rad for those who bought the product 6 months after me - sigh.
Anyway if you can help with Indy 10 in builder 2010 Tc[Client and TcpServer example code or documentation that would be great.

Thanks in Advance
Barry
0
Barry
5/16/2010 3:16:34 AM
embarcadero.cppbuilder.socket 566 articles. 0 followers. Follow

6 Replies
3918 Views

Similar Articles

[PageSpeed] 29

<Barry Andrews> wrote in message news:243206@forums.embarcadero.com...

> I have spent weeks and weeks googling for examples of
> using these Indy 10 components with Builder 2010.
> So far every link that says it is a builder example, winds
> up being Delphi and doesn't really explain how these
> components work properly.

Delphi code can be used in C++ projects, and can be translated to C++ when 
needed.  What exactly about the examples are unclear to you?

> Any examples I have found in builder simply show connect,
> send a single packet, receive a single packet, disconnect.

What's wrong with that?  Those are the basic that you can then build on as 
needed for more complex communications.

-- 
Remy Lebeau (TeamB)
0
Remy
5/17/2010 4:08:49 AM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Barry Andrews> wrote in message news:243206@forums.embarcadero.com...
> 

Firstly thanks for answering

> > I have spent weeks and weeks googling for examples of
> > using these Indy 10 components with Builder 2010.
> > So far every link that says it is a builder example, winds
> > up being Delphi and doesn't really explain how these
> > components work properly.
> 
> Delphi code can be used in C++ projects, and can be translated to C++ when 
> needed.  What exactly about the examples are unclear to you?
>

Yes but often the implimentation of the class is not exactly the same in C++ and there is more searching to work out what the difference is.
 
> > Any examples I have found in builder simply show connect,
> > send a single packet, receive a single packet, disconnect.
> 
> What's wrong with that?  Those are the basic that you can then build on as 
> needed for more complex communications.

The trouble is it doesn't give a good understanding of how things works, just shows 4 out of all the possible options and doesn't really show how they operate.
Here are issues I have hit so far:

Send function

		if (IdTCPClientSocket->IOHandler && IdTCPClientSocket->IOHandler->Connected())
			TCPClient->IOHandler->Write(msg);
seems to work
		if (IdTCPClientSocket->IOHandler && IdTCPClientSocket->Connected())
			TCPClient->IOHandler->Write(msg);
Hangs the application and won't shutdown or disconnect, I have to reset the application under the IDE to get to to operate

The first one works, but if I do a IdTCPClientSocket->Disconnect()
The next connect attempt causes an exception which say's already connected?
Despite receiving an OnDisconnected event.


Receive side:
Code in timer event as I have gleemed from what examples I could find
void __fastcall TRefereeForm::TcpTimerTimer(TObject *Sender)
{
AnsiString RxData;
int  posn;

	if (IdTCPClientSocket->IOHandler && IdTCPClientSocket->IOHandler->Connected())
	{
		try
		{
			if (!IdTCPClientSocket->IOHandler->InputBufferIsEmpty())
			{
				RxData = IdTCPClientSocket->IOHandler->ReadLn();
				RcvdData += RxData;
				while (RcvdData.Length())
				{
					if (*RcvdData.c_str() != '\r' && *RcvdData.c_str() != '\n')
					{
						RcvdLine[RcvdPosn++] = *RcvdData.c_str();
						// Discard if line is too long
						if (RcvdPosn >= MAX_LINE)
							RcvdPosn = 0;
					}
					else
					{
						RcvdLine[RcvdPosn] = 0;
						ProcessMessage(IdTCPClientSocket, RcvdLine, RcvdPosn, true);
						RcvdPosn = 0;
					}
					RcvdData = RcvdData.SubString(2, RcvdData.Length() - 1);
				}
			}
		}
		catch(...)
		{

		}
	}
}



Receives the first line of data but no more.
I realise I am probably doing something very wrong in this receive, but not a lot of information to go with.

What I want to do is communicate with other application which uses standard socket and simply sends ascii data.
For receive I would like to just be able to receive whatever is in the buffer and then parse it myself.
For transmit I just want to be able to send a simple string.

I also need to be able to doisconnect and then reconnect later if required.

Finally I also want to be able to implement a TcpServer which can accept multiple clients (Up to 8) and process the data from these in basically the same manner.

Any help would be appreciated.



> 
> -- 
> Remy Lebeau (TeamB)
0
Barry
5/17/2010 1:53:10 PM
Got receiver working, changed to

			IdTCPClientSocket->IOHandler->CheckForDataOnSource(10);
			if (!IdTCPClientSocket->IOHandler->InputBufferIsEmpty())
			{
				RxData = IdTCPClientSocket->IOHandler->InputBufferAsString();

Now also disconnects correctly.
Are you able to help with server example please.
0
Barry
5/17/2010 2:06:19 PM
<Barry Andrews> wrote in message news:243572@forums.embarcadero.com...

> Yes but often the implimentation of the class is not exactly
> the same in C++

Such as?  The VCL and Indy are both the same in Delphi and C++.  Differences 
tend to be only in low-level places where language-specific compiler 
features are needed, which is rare to have to deal with manually.

> if (IdTCPClientSocket->IOHandler && 
> IdTCPClientSocket->IOHandler->Connected())
> TCPClient->IOHandler->Write(msg);
> seems to work

You do not need the validation checks, in fact you should remove them and 
let Write() raise an exception when needed instead.

> if (IdTCPClientSocket->IOHandler && IdTCPClientSocket->Connected())
> TCPClient->IOHandler->Write(msg);
> Hangs the application and won't shutdown or disconnect, I have to reset 
> the application under the IDE to get to to operate

There is no functional difference whatsoever between using 
IdTCPClientSocket->IOHandler->Connected() versus 
IdTCPClientSocket->Connected(), as the latter simple deleates to the former:

{code:delphi}
function TIdTCPConnection.Connected: Boolean;
begin
  // Its been changed now that IOHandler is not usually nil, but can be 
before the initial connect
  // and also this keeps it here so the user does not have to access the 
IOHandler for this and
  // also to allow future control from the connection.
  Result := IOHandler <> nil;
  if Result then begin
    Result := IOHandler.Connected;
  end;
end;
{code}

> The first one works, but if I do a IdTCPClientSocket->Disconnect()
> The next connect attempt causes an exception which say's already 
> connected?

That will happen if you left unread data in the IOHandler's InputBuffer when 
disconnecting.  Indy is intentionally designed to return Connected=True in 
that scenerio even if the physical socket has been closed.  Connect() raises 
the exception when Connected=True.

> Receive side:
> Code in timer event as I have gleemed from what examples I could find

Such an approach is only needed if the server is sending unsolicited data. 
Typically, this approach is better handled using a reading thread instead of 
a timer.

> if (IdTCPClientSocket->IOHandler && 
> IdTCPClientSocket->IOHandler->Connected())

Again, get rid of thosse validation checks.  Use 
IOHandler->CheckForDataOnSource() to fill the InputBuffer.

> if (!IdTCPClientSocket->IOHandler->InputBufferIsEmpty())
> {
> RxData = IdTCPClientSocket->IOHandler->ReadLn();
> RcvdData += RxData;
> while (RcvdData.Length())
> {
> if (*RcvdData.c_str() != '\r' && *RcvdData.c_str() != '\n')
<snip>
> Receives the first line of data but no more.

That is because your reading logic is wrong for Indy.  ReadLn() returns data 
up, but not including, the first available CRLF (or bare LF) and leaves any 
remaining data in the InputBuffer for subsequent reads.  Your RcvdData 
variable will never actually receive the CRLF characters, so your code will 
never even reach the call to ProcessMessage().  If you want to control how 
long lines are allowed to be, use the IOHandler's MaxLineLength and 
MaxLineAction properties instead.

Try this instead:

{code:cpp}
void __fastcall TRefereeForm::TcpTimerTimer(TObject *Sender)
{
    AnsiString RxData;

    try
    {
        if( IdTCPClientSocket->IOHandler->InputBufferIsEmpty() )
        {
            IdTCPClientSocket->IOHandler->CheckForDataOnSource(10);
            if( IdTCPClientSocket->IOHandler->InputBufferIsEmpty() )
                return;
        }


        RxData = IdTCPClientSocket->IOHandler->ReadLn();
        ProcessMessage(IdTCPClientSocket, RxData.c_str(), RxData.Length(), 
true);
    }
    catch(const Exception &)
    {
    }
}
{code}

> I also need to be able to doisconnect and then reconnect later if 
> required.

Simply make sure the InputBuffer is empty before calling Connect() after the 
first time.

> Finally I also want to be able to implement a TcpServer which can accept 
> multiple
> clients (Up to 8) and process the data from these in basically the same 
> manner.

Use the TIdTCPServer component for that.  Put your reading code in its 
OnExecute event.

-- 
Remy Lebeau (TeamB)
0
Remy
5/17/2010 5:20:35 PM
Thanks for all the information, makes things clearer now.
0
Barry
5/18/2010 12:05:10 AM
> {quote:title=Barry Andrews wrote:}{quote}
> Hello
> 
> I am a newbie here so please go easy on me if this is not the correct place to ask this.
> I have spent weeks and weeks googling for examples of using these Indy 10 components with Builder 2010.
> So far every link that says it is a builder example, winds up being Delphi and doesn't really explain how these components work properly.
> 
> The Indy page itself only has delphi examples and not very good ones at that.
> 
> Previously I have used ICS with builder 2007 but their latest release gives me all sorts of errors under builder 2010 and their web pag is badly lagging the product version with old outdated examples etc.
> 
> Before I toss both of these and just go back to basic socket programming of my own, can anyone please point me in the direction of a GOOD example of using the TCP client server.
> Any examples I have found in builder simply show connect,send a single packet, receive a single packet, disconnect.
> Like most people I wish to connect, stay online and communicate packets both ways for an extended period of time and then disconnect.
> 
> Thanks in advance for any direction you can give, I am beginning to feel that upgrading to builder 2010 was not such a good idea since all the components I used to use now fail me and code that was doing a good job can no longer be used.
> Even more frustrating now that embarcadero are throwing in full rad for those who bought the product 6 months after me - sigh.
> Anyway if you can help with Indy 10 in builder 2010 Tc[Client and TcpServer example code or documentation that would be great.
> 
> Thanks in Advance
> Barry

Have you noticed this thread?

https://forums.embarcadero.com/thread.jspa?threadID=34996&tstart=0
0
Zeljko
5/18/2010 9:01:57 PM
Reply:

Similar Artilces:

Socket to Socket example needed
Does anyone have any configuration examples of how to set up a socket to socket connection. I would appeciate any examples or help anyone can offer. Thanks. Rob McGinness Rutland Regional Medical Center 802-747-6526 rmcginness@rrmc.org I guess that you talk about one AIM module that drives two socket conections. If you try to build such module, you must create two protocol objects within the project. Each protocol object can control its own related objects (frame, port, open, close, etc.) > Does anyone have any configuration examples of how to set > up a socket to socke...

C++Builder 2010, Delphi 2010 and RAD Studio 2010 Preview
The RAD Studio 2010 Sneak Preview Center is now live at http://www.embarcadero.com/rad-studio-2010/. Our first preview covers just a few of the many new IDE features and enhancements in C++Builder and Delphi 2010. Additional preview videos and blog links will be published regularly up to the release date of the 2010 versions. Please stop by to see what's coming in C++Builder 2010. Thanks, Tim Tim DelChiaro wrote: > The RAD Studio 2010 Sneak Preview Center is now live at http://www.embarcadero.com/rad-studio-2010/. > > Our first preview covers just a few of the...

Re: C++Builder 2010, Delphi 2010 and RAD Studio 2010 Preview
<Eric Hufschmid> wrote in message news:145900@forums.codegear.com... > Have any of you people noticed that we have been lied to about the 9/11 > attack, and other crimes? Do you realize that there really is a large > crime gang operating in the world and fooling people into assisting with > their crimes? Take a look at the collapse of Building 7, of the World > Trade Center, if you don't know what I'm talking about. Watch the video, > "9/11, A Closer Look" at HugeQuestions.com. It's worse than that - check out www.drjudywood.com to se...

Moved: 9/11 discussion was: C++Builder 2010, Delphi 2010 and RAD Studio 2010 Preview
From: Mark Jacobs <mark@jacobsm.com> Subject: Re: C++Builder 2010, Delphi 2010 and RAD Studio 2010 Preview Message-ID: <151898@forums.codegear.com> Date: Wed, 19 Aug 2009 12:21:59 -0700 https://forums.embarcadero.com/thread.jspa?messageID=150212 -- David Dean (Embarcadero) Lead C++ QA Engineer ...

Upcoding AnsiString from Builder 6 to Builder 2010
I need to upgrade some BCB6 projects to 2010, and I've encountered issues with AnsiString during the compilation process. I've tried various methods to accommodate the new changes in 2010, but haven't quite figured it out yet. Below is a code snippit that's used extensively in all of the projects that need to be upgraded. Can someone show or explain what changes need to be made in order to arrive at the same effect? The primary problem involves the use of MessageBox(...) bool __fastcall TMainForm::GetMessageBox(*AnsiString MessageStr*) { if(Application->MessageB...

C++Builder 2010 and RAD Studio 2010 webinars
Four new webinars on C++Builder 2010 and RAD Studio 2010 products What’s New in C++Builder 2010 Thursday, August 27, 2009 11:00 AM PDT / 2:00 PM EDT http://update.codegear.com/forms/AMUSCA09008CGWebinar827 What’s new in Delphi 2010 Wednesday, August 26, 2009 11:00 AM PDT / 2:00 PM EDT http://update.codegear.com/forms/AMUSCA09008CGWebinar826 RAD Studio 2010 Overview Thursday, September 3, 2009 11:00 AM PDT / 2:00 PM EDT http://update.codegear.com/forms/AMUSCA0908CGWebinar903 Introduction to Delphi Prism 2010 Friday, August 28, 2009 11:00 AM PDT / 2:00 PM EDT http://updat...

C++Builder 2010 and C++Builder XE2 in the same machine
Is it possible to install C++Builder 2010 and C++Builder XE2 in the same machine? Can they coexist? Hi Richard Richard Weber wrote: > Is it possible to install C++Builder 2010 and C++Builder XE2 in the same > machine? Can they coexist? I don't know about those two, but it is normally not a problem to have different version of C++ Builder on the same machine. It is how ever recommended that they be installed in the order they were released. Best regards Asger-P Richard Weber <> wrote: >Is it possible to install C++Builder 2010 and >C++Builde...

C++Builder 2010: same old bugs.... goodbye, C++Builder.
I've tried the demo version, built my OCX, set the constrained size, and added some event handler. It works exactly like the old Borland CBuilder 6: the grab handles does not follow the constrained size, the OnCloseQuery and OnDestroy events are not fired, there's is not an OnAmbientChange event available. Building OCX is an important feature for us, as our customer needs them... so, if i were the one that take the final decision, i'll migrate to MS developement tools.... i'm not that one, but i'll strongly push in that direction. It's a shame, anyway. If yo...

Wanted: Example of asynchronous bidirectional socket client (a socket chat program)
Sorry if this appears twice. Since it bounced back to me -- probably=0Abeca= use of the HTML format -- I'm sending it again.=0A=0AI did some google sear= ching and I could not find an example of a=0Abidirectional asynchronous soc= ket client. A telnet client is an example=0Aof a bidirectional asynchronous= socket client.=0A=0AI don't specifically want source to a telnet client --= that would be=0Amuch fancier than what I require and would not be helpful = if the perl=0Aonly called C++. I just want an example in pure perl (or ruby= or=0Apython).=0A=0A=0ABy asynchronous I mean ...

RAD Studio 2010 Hotfix 1 for C++Builder 2010 is now available
Hotfix 1 for C++Builder 2010 is now available. Registered users can get Hotfix 1 and other updates and additional free software at http://cc.embarcadero.com/reg/c_builder. Here's the readme with more information. RAD Studio 2010 Hotfix 1 This software patch is being provided to licensed users of Embarcadero RAD Studio 2010. Installation and use are governed by the license statement for Embarcadero RAD Studio 2010. This Hotfix applies to: Product: Embarcadero RAD Studio, C++Builder Version: 2010 Update level: Update 4 / Update 5 (Database Pack) Editions: Professional, En...

ANN: Buy Del123 Delphi examples get Cbexamples C Builder examples for free
Only in February: buy Del123.com Delphi examples collection at: http://www.del123.com/ and get Cbexamples C++ Builder examples at: http://www.cbexamples.com for free. ...

ANN: Entire TMS VCL product line supports Delphi 2010 & C++Builder 2010
We are delighted to inform that all our VCL components are now supporting the latest Embarcadero IDEs. This means that active registered users can obtain free updates for any VCL product purchased with support for Delphi 2010 & C++Builder 2010. We now also have fully functional trial versions of all our VCL products specifically for Delphi 2010 & C++Builder 2010 at http://www.tmssoftware.com At the same time, our entire VCL IntraWeb product line now also supports the latest version IntraWeb 10 across all Delphi & C++Builder versions. Trial downloads for IntraWeb 10...

Patch for perldoc IO::Socket::UNIX (see http://perldoc.perl.org/IO/Socket/UNIX.html ) to provide examples
Hi, I noticed http://perldoc.perl.org/IO/Socket/UNIX.html does not contain any examples (including not in the synopsis). If acceptable, I can a prepare a patch that will include a simple server and a client for UNIX domain socket= s, see: http://www.mail-archive.com/beginners%40perl.org/msg116942.html Can it be in a section called EXAMPLES, or should it be in the SYNOPSIS? Regards, =C2=AD=E2=80=94 Shlomi Fish=20 --=20 ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ My Aphorisms - http://www.shlomifish.org/humo...

ANN: New version of X-DBGrid Component 4.23 and X-Files Components 4.23 with support for Delphi 2010 and C++Builder 2010!
Dear Developers, I'm pleased to announce the new version of X-DBGrid Component 4.23 and X-Files Components 4.23 has been released. What's new in version 4.23 ? ====================== - Package for Delphi 2010 Win32 and C++Builder 2010!!! What's new in version 4.22 ? ====================== - Included printable Developer's Guide in PDF format (227 pages)!!! - TXQRGrid is ready for using with QuickReport 5.0x for Delphi 2009 - Improved Calculator adjusted to InplaceEditor font style and size - Improved Calendar adjusted to InplaceEditor f...

ANN: New version of X-DBGrid Component 4.23 and X-Files Components 4.23 with support for Delphi 2010 and C++Builder 2010!
Dear Developers, I'm pleased to announce the new version of X-DBGrid Component 4.23 and X-Files Components 4.23 has been released. What's new in version 4.23 ? ====================== - Package for Delphi 2010 Win32 and C++Builder 2010!!! What's new in version 4.22 ? ====================== - Included printable Developer's Guide in PDF format (227 pages)!!! - TXQRGrid is ready for using with QuickReport 5.0x for Delphi 2009 - Improved Calculator adjusted to InplaceEditor font style and size - Improved Calendar adjusted to InplaceEditor f...

Web resources about - Builder 2010 - Indy TcpClient / TcpServer examples - embarcadero.cppbuilder.socket

ucspi-tcp - Wikipedia, the free encyclopedia
... is a public domain Unix TCP command-line tool for building TCP client-server applications. It consists of super-server tcpserver and tcpclient ...

Latest Updates
Latest Updates - Free source code and tutorials for Software developers and Architects.; Updated: 15 Feb 2013

Pumpkin.ino
- Gist is a simple way to share snippets of text and code with others.

CodeSOD: Tale of the Cryptic Encoding
... System.Net.Sockets Imports System.IO Imports Confidential Module EmailApp Sub Main() Dim ProcessPayment As New Secret Dim Server As TcpClient ...

RSA private key import from PEM format in C#
First of all, I want to apologies for not writing. From one hand, this is not a good think for me to disappeared from development community horizons, ...

Using FTP with Microsoft.NET
// Setup the FTP request object FtpWebRequest ftpRequest = ( FtpWebRequest ) FtpWebRequest . Create ( "ftp://localhost/file.txt" ) ; // Supply ...

Introduction
Kamaelia - making concurrency simpler in python - Introduction

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Latest Updates
Latest Updates - Free source code and tutorials for Software developers and Architects.; Updated: 15 Feb 2013

Resources last updated: 1/5/2016 5:56:21 AM