C++C++17Language FeatureLearn C++Videos

How To Create TCP/IP Server and Client Connections in C++ Builder?

How To Create TCPIP Server and Client Connections in C++ Builder

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.

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,

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.

How To Create TCPIP Server and Client Connections in C++ Builder The Indy components

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.

How To Create TCPIP Server and Client Connections in C++ Builder The GetIt window

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,

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,

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.

How To Create TCPIP Server and Client Connections in C++ Builder A form with some TCPIP components on it

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:

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.

How To Create TCPIP Server and Client Connections in C++ Builder The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?