📜 ⬆️ ⬇️

Programming network applications in C ++

Good day! I want to share with you the experience gained over several months of work on this topic.
Now network games and applications continue to gain popularity, and at the same time the number of those who want to write their own program that works with the network is increasing.
It all started with the fact that I needed to write a normal chat in C ++. Naturally, it was necessary to find a simpler way, without spending much time.

The C ++ Builder environment provides several options for writing network applications:

1. Use the winsock.h library (Windows Sockets library)
This method is good because it is universal, and this is an absolute advantage. But if you choose this method, be prepared for the fact that you have to dig into WinAPI and create threads, and this is not for everyone. Therefore, this method is not very suitable in a situation where there is not much time to write a finished program.
')
2. Use Indy components
If you use this method, be prepared to experience one serious drawback - poor tolerance. Components very seriously depend on the version of the library, and with each new version of the software environment a new version of the Indy library is delivered, in which new properties and methods of components are added or processed. And I also quickly cut off this method, because I wanted to find something more universal.

3. Use the TcpServer and TcpClient components
Personally, I tried to build a chat on these components, but nothing happened. I hope that you, dear habrayusers, had a successful experience in using these components.

4. Use ServerSocket and ClientSocket components
Fundamentally, these components are based on Windows Sockets, only here you will not have to mess with WinAPI. It all comes down to correctly use the properties and methods of these components. This method saves enough time, and therefore for situations where you need a result in a short time, it fits perfectly.

So a few words about these components.
They are located on the Internet tab of the component palette in C ++ Builder 6. In later versions of this software, developers removed the components in question from this palette, but an installation package is supplied as standard (in C ++ Builder 2010, the file dclsockets140.bpl: versions may differ only in the last digit), which will need to be set through the project settings (menu “Project” -> “Options” -> “Packages” -> button “Add ...”). After installing the specified package components appear on the palette in the tab "Internet".

ServerSocket and ClientSocket can operate in two modes: blocking and asynchronous. The differences are that in blocking mode, the server program will pause its work until a new client connects, and each client is served in a separate thread. Therefore, it is better to choose an asynchronous mode in which the server program will quietly continue working without waiting for the next client.

In order to create a server, in the ServerSocket component it is enough just to specify the port to be used. This is an integer from 0 to 65535. After that, only the following methods are used: Open () to create a server and Close () to destroy it.

Using the ClientSocket component, you can create the client part of the application. The ClientSocket component has the following properties:
ClientSocket1->Address; // , IP-
ClientSocket1->Host; // , DNS
ClientSocket1->Port; // , ,

It is worth noting that the Host property is a higher priority than the Address property. Thus, if you specify both properties, the connection will be established from the DNS server, which is specified in the Host property. The connection is established or terminated using the same methods Open () and Close ().

Common to both components is the Boolean property Active, which determines the correctness (or activity) of the compound.

As you can see, everything is simple. But be careful with the sequence of events committed in these components, and I believe that in a few days you can write a simple chat to communicate with friends.

The result of my research on this topic turned into a methodological development , thanks to which I successfully passed the programming exam. It also describes in detail the working principles of the components considered in this article.
I also recommend reading the book of A.Ya. Arkhangelsky. “Programming techniques in C ++ Builder 6 and 2006. Windows network mechanisms”, which also deals with the topic of developing network applications.

Good luck to you!

Source: https://habr.com/ru/post/119055/


All Articles