The speed of communications is rapidly increasing and new advances in the underlying technology happen with increasing frequency. One of the oldest and maybe one of the most used types of connections in the world is the TCP/IP (Transmission Control Protocol/Internet Protocol) connection. This protocol is used to enable end-to-end data connections. It is a standard that specifies how data should be packaged, addressed, transmitted, routed, and received. With the C++ programming language it is easy to develop Server (back-end) and Client (front-end) applications which make use of these kinds of standard protocols. In this post we explain how to create TCP/IP Server and Client applications in C++ Builder.
Table of Contents
What is TCP and TCP/IP in C++?
The phrase TCP/IP is an abbreviation of Transmission Control Protocol/Internet Protocol. TCP/IP is a communication protocol used to connect network devices on the internet or in a private computer network. This protocol is used to define the end-to-end data connections that specify how data should be packaged, addressed, transmitted, routed, and received. TCP/IP connection is also known as the Internet Protocol Suite which is used for organizing the set of communication protocols to an agreed set of rules and standards so that all devices and programs using that standard can mutually ‘understand’ how to communicate with each other.
TCP/IP Sockets let you create lightweight clients. For example, if you are writing a web-based client application, you cannot be sure that client systems support more specific focused technologies such as DCOM due to differences in hardware and operating system. Sockets provide the lowest common denominator that you know will be available for connecting to the application server. For more information about sockets, see Working with Sockets.
We can use a range of different libraries and components to create and use socket connections in C++ Builder. Here are some of the ways to make those connections,
- Using DCOM Connections
- Using SOAP Connections
- Using Socket Components
- Using 3rd Party Components and Libraries
- Using Indy Components
- Using IPWorks Components
- Using other 3rd Party components and libraries
However, in this post we explain specifically how to create and use a TCP/IP Connection with IPWorks Components.
How to create a TCP/IP Server and Client connection in C++?
If you want to develop very modern TCP/IP Server or Client applications easily in C++, you can use RAD Studio, C++ Builder 11. I suggest you use either the IPWorks components in your VCL or FMX applications or the similar very popular Indy components. Indy has the advantage of being free and open source but the IPWorks set is very powerful and well-respected.
Check your component palette to see if the Indy components are already installed. You should see them under the “Indy Clients” and “Indy Servers” categories.
IPWorks has TCP/IP Client and Server components that allows you to add TCP/IP connections to your applications. In RAD Studio C++ Builder, you can easily download and install IPWorks components from the GetIt Package Manager (Tools->GetIt Package Manager… menu).
IPWorks by ‘/n software‘ is one of the most comprehensive component libraries for professional Internet development. IPWorks is a component package for Internet-based application development, that provides programmable SSL-enabled components that facilitate tasks such as ensuring security, sending email, transferring files, managing networks, browsing the Web, consuming Web Services, etc. IPWorks 2020 version is very compatible with RAD Studio 10.x and 11.x versions.
If you have the latest RAD Studio, C++ Builder 11.x version, you can install IPWorks via GetIt Package Manager (from Tools->GetIt Package Manager… menu) as below.
To install it successfully you may need to restart your RAD Studio. Now, let’s see some simple client and server examples.
Is there a simple TCP/IP client connection example in C++ Builder?
If you have the Indy components, you can use IdTCPClient (TIdTCPClient) component to furnish a client connection. Just drag this component to your form. Here is a simple client connection that sends data to your local server application,
1 2 3 4 |
IdTCPClient1->Host = "127.0.0.1"; // ServerIP IdTCPClient1->Connect(); IdTCPClient1->Socket->Write(127); // Write Integer Data 127 to TCP Socket |
Is there a simple TCP/IP server connection example in C++ Builder?
With Indy you can use IdTCPServer (TIdTCPServer) component to do server connection. Just add a button, drag this component to your form and double click to its OnExecute event in Object Inspector window. Here is a simple example to local server application that activates by a button click and then listens TCP/IP socket execution and reads data from the context,
1 2 3 4 5 6 7 8 9 |
void __fastcall TForm1::Button1Click(TObject *Sender) { IdTCPServer1->Active=true; } void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext) { int x = AContext->Connection->Socket->ReadInt32(); // Read Integer Data from TCP Socket } |
Is there a full TCP/IP server connection example in C++ Builder?
Here is a simple TCP/IP server C++ example:
- we need a IdTCPClient (TIdTCPClient) component to do client connection,
- and a IdTCPServer (TIdTCPServer) component to do server connection.
- we need a Memo (TMemo) to display TCP data obtained from a server,
- and a Timer (TTimer) that outputs TCP data from a client to a server.
- Finally we need a Button (TButton) to start connection.
Arrange them as below, double click to its OnExecute event of IdTCPServer11 component in Object Inspector window, double click to Button1 to create OnClick event, double click to Timer to create OnTimer event.
Now, in our example out timer increases the value every interval and writes to client. When the server receives this message, it outputs received value to our Memo component. To do this, you need to add some code like that shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "TCP_CLient_Server_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int t=0; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(IdTCPClient1->Connected) try { IdTCPClient1->Socket->Write(t++); } catch(const int e) { throw(e); } } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext) { int x = AContext->Connection->Socket->ReadInt32(); Memo1->Lines->Add( IntToStr(x) ); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { IdTCPServer1->DefaultPort=80; IdTCPServer1->Active=true; IdTCPClient1->Host = "127.0.0.1"; // ServerIP IdTCPClient1->Port = 80; IdTCPClient1->Connect(); Timer1->Enabled=true; } //--------------------------------------------------------------------------- |
This is a very simple TCP/IP connection example application in VCL that acts as a client and a server. Here Indy TCP Client/Server example which is mostly same as above. Note that both IPWorks and Indy components support FMX applications too.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition