Maybe you have been impressed by the powerful functions and advanced technology of Internet application software such as Microsoft's IE browser. Maybe you are not yet capable of developing such a complex large-scale system, but don't worry, here can give you a chance to practice. , is to learn to use Delphi's Winsocket to write TCP/ip-based applications. It is assumed here that you are already familiar with Delphi development tools.
We can imagine that if you write a program to communicate with a remote computer from the very beginning, you must have a comprehensive familiarity and mastery of the relevant network protocols, underlying knowledge of the system, and network software and hardware technology. However, this can only be achieved in a short time. It is not an easy task for most people.
Fortunately, Delphi's network component library provides us with components for implementing network communication, which encapsulate the complex implementation details. The ClientSocket and ServerSocket components allow us to easily write our own network communication and resource sharing programs.
The steps for writing Winsocket programs in Delphi 4.0 are as follows:
1) Winsocket component property settings;
2) Establish a connection with the remote computer;
3) Data transmission between computers;
These three steps are explained in detail below.
one. Setting Winsocket properties
In Delphi 4.0, Winsocket is subdivided into two components: ClientSocket and ServerSocket. They serve as client-side and server-side components respectively. That is, the ClientSocket component is used in the client program, and the ServerSocket component is used on the server side. Through communication between these two components, coupled with auxiliary application code, a simple communication program can be implemented. Of course, if ServerSocket is introduced into the client program, the client program can act as a server and respond to requests from other client programs.
If you are writing a server-side program, you must set the Port property of the ServerSocket component. The reason for setting this parameter is because there may be multiple server programs running on the same computer, and they may be constantly accepting connection requests from remote client programs. If you are trying to send an email and the system connects your request to another server program, your email will definitely not be sent properly. Therefore, a suitable port number Port must be set for the ServerSocket component on the server program. You can also set the Service attribute, which indicates the type of service provided by ServerSocket, such as: FTP, HTTP, etc. Then set the Active property to True.
If you are writing a client program, there are more properties to set the ClientServer component. The Port attribute should be set to be consistent with the value of the Port attribute on the server side. In addition, the Host property must be set correctly. It is a read-only property and is not available at design time. It indicates the host name of the remote server to which the client program wants to connect. You can also set the Address attribute, which is the IP address of the remote host. If both attributes are set, the system will use the Host attribute, and the IP address set by the Address attribute will become invalid.
two. Establish a connection to a remote computer
To transfer data between remote computer systems, a connection must first be established between the two communicating hosts.
The ServerSocket component on the server side calls the Open method to initialize the Socket connection. At the same time, it sets the Active attribute to True, sets the ServerSocket component to listening mode, and detects whether there is a connection request at any time.
If the server accepts the connection request from the client program, the OnAccept event is triggered. The following code is what the server program needs to do after accepting the connection.
PRocedure Myform..ServerSocketAccept(Sender: TObject, Socket: TCustomWinSocket);
begin
IsServer := True;
end;
In the client program, the ClientSocket component sets necessary properties such as Port and Host, and then sets the Active property to True to make a connection request.
three. Data transfer between computers
Once the server accepts the connection request from the client, the client can send data. At this time, there is a Socket between the client and the server, and communication is achieved through this Socket. Therefore, the Socket attribute is very important, and it has many methods. Using a few simple methods, you can send and receive data.
The client side uses the following form: ClientSocket1.socket.sendtext('string you want to send');
On the server side, it takes the following form: ServerSocket1.socket.recievetext(str: string); This function returns the length of the received string and stores the string in the variable str.
The above is the simplest example of data transmission. You can also use other methods provided by the Socket property to implement complex data transmission.
Although it is very convenient to use the Winsocket component in Delphi 4.0, if you want to be truly proficient in using it to develop network applications, you must carefully read its help files and sample programs. Compare the advantages of this method in continuous learning, master its programming features, and then master the programming skills of other network components provided in Delphi, such as: NMPOP3, NMUDP, NMFTP, etc. By then, you will be able to participate in the development of real network applications, rather than just taking other people's network application software, setting it up, and operating it skillfully.