Delphi 7 Development of socket application with Indy component

Source: Internet
Author: User
Tags requires socket thread

In the previous work, I need to develop a simple network data transmission program. Because usually use Delphi to do point development, so this time is no exception. Delphi 7 comes with two sets of TCP socket components: the Indy socket component (Idtcpclient and Idtcpserver) and the Delphi native TCP socket Component (Clientsocket and ServerSocket). However, Borland has claimed that Clientsocket and serversocket components are about to be discarded, and the corresponding Indy components are recommended instead. Therefore, the author uses the Indy. Based on the brief introduction of Indy, this paper has created a set of simple TCP socket data transmission applications to demonstrate the use of Indy.

Open source Internet Component Set--internet Direct (Indy) Internet Direct (Indy) is a set of open source Internet components that cover almost all popular Internet protocols. Indy is written with Delphi and is included in Delphi 6,kylix 1 and C + + Builder 6 and above in each version of the Borland development environment. Indy was once called Winshoes (a pair of socket libraries for winsock--windows), built by a group of developers led by Chad Z. Hower to find more information and download new versions of Indy's site Www.nevrona.com/indy. By the time I wrote this article, the latest stable version of Indy, 9.0.14,indy 10, has also entered the beta test phase.

Delphi 7 with the Indy 9. There are more than 100 Indy components installed on its palette. With these components you can develop TCP client and server applications based on a variety of protocols, and deal with related coding and security issues. You can identify the Indy component by the prefix ID.

Indy is a blocking type (Blocking).

When you use Winsock to develop a Web application, reading data from the socket or writing to the socket is asynchronous, which does not block the execution of other code in the program. When the data is received, Winsock sends the appropriate message to the application. This type of access is called a non-blocking connection, which requires you to respond to the event, set the state machine, and usually require a wait loop.

Unlike the usual Winsock programming method, Indy uses a blocking socket invocation method. Blocking access is more like file access. When you read the data or write the data, the read and write functions wait until the corresponding operation is complete before returning. For example, initiating a network connection simply calls the Connect method and waits for it to return, if the method succeeds, returns directly at the end, and throws the corresponding exception if it fails to execute successfully. Unlike file access, the socket call may take longer because the data to read or write may not be ready immediately (largely dependent on network bandwidth).

Blocking sockets are not demons (Evil)

For a long time, blocking sockets have been attacked for no reason. In fact, blocking sockets are not as scary as they usually say. This also has to say from the development of Winsock.

A serious problem occurs immediately when the socket is ported from UNIX to Windows. UNIX supports fork, and both clients and servers can fork new processes and start these processes, making it easy to use blocking sockets. Windows 3.x does not support fork or multithreading, and when a blocking socket is used, the user interface is "locked" and unable to respond to user input.

To overcome this flaw in Windows 3.x, Microsoft has added an asynchronous extension to the Winsock so that Winsock does not "lock" the application's main thread (also the only thread). However, this requires a completely different programming approach. So some people, in order to conceal this weakness, began to vigorously slander blocking sockets.

When Win32 appears, it can support threads well. However, the idea of the concept has become difficult to change, and the words can not be recalled, so the libel on the blocking socket continues to exist.

In fact, a blocking socket is still the only way UNIX can implement a socket, and it works fine.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.