Write Network-Aware Applications With Delphi

Businesswoman using laptop in office
Morsa Images/Taxi/Getty Images

Of all the components that Delphi provides to support applications that exchange data over a network (internet, intranet, and local), two of the most common are TServerSocket and TClientSocket, both of which are designed to support read and write functions over a TCP/IP connection.

Winsock and Delphi Socket Components

Windows Sockets (Winsock) provides an open interface for network programming under the Windows operating system.

It offers a set of functions, data structures, and related parameters required to access the network services of any protocol stacks. Winsock acts as a link between network applications and underlying protocol stacks.

Delphi socket components (wrappers for the Winsock) streamline the creation of applications that communicate with other systems using TCP/IP and related protocols. With sockets, you can read and write over connections to other machines without worrying about the details of the underlying networking software.

The internet palette on the Delphi components toolbar hosts the TServerSocket and TClientSocket components as well as TcpClient, TcpServer, and TUdpSocket.

To start a socket connection using a socket component, you must specify a host and a port. In general, host specifies an alias for the IP address of the server system; port specifies the ID number that identifies the server socket connection.

A Simple One-Way Program to Send Text

To build a simple example using the socket components provided by Delphi, create two forms—one for the server and one for the client computer. The idea is to enable the clients to send some textual data to the server.

To start, open Delphi twice, creating one project for the server application and one for the client.

Server Side:

On a form, insert one TServerSocket component and one TMemo component. In the OnCreate event for the form, add the next code:

procedure TForm1.FormCreate(Sender: TObject);
begin
 ServerSocket1.Port := 23;
 ServerSocket1.Active := True;
end;

The OnClose event should contain:

procedure TForm1.FormClose
(Sender: TObject; var Action: TCloseAction);
begin
 ServerSocket1.Active := false;
end;

Client Side:

For the client application, add a TClientSocket, TEdit, and TButton component to a form. Insert the following code for the client:

procedure TForm1.FormCreate(Sender: TObject);
begin
 ClientSocket1.Port := 23;
 //local TCP/IP address of the server
 ClientSocket1.Host := '192.168.167.12'; 
 ClientSocket1.Active := true;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 ClientSocket1.Active := false;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if ClientSocket1.Active then
 ClientSocket1.Socket.SendText(Edit1.Text);
end;

The code pretty much describes itself: when a client clicks a button, the text specified inside the Edit1 component will be sent to the server with specified port and host address.

Back to the Server:

The final touch in this sample is to provide a function for the server to "see" the data the client is sending.

The event we are interested in is OnClientRead—it occurs when the server socket should read information from a client socket.

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
 Socket: TCustomWinSocket);
begin
 Memo1.Lines.Add(Socket.ReceiveText);
end;

When more than one client sends data to the server, you'll need a little more to code:

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
 Socket: TCustomWinSocket);
var
 i:integer;
 sRec : string;
begin
 for i := 0 to ServerSocket1.Socket.ActiveConnections-1 do
 begin
 with ServerSocket1.Socket.Connections[i] do
 begin
 sRec := ReceiveText;
 if sRecr '' then
 begin
 Memo1.Lines.Add(RemoteAddress + ' sends :') ;
 Memo1.Lines.Add(sRecr);
 end;
 end;
 end;
end;

When the server reads information from a client socket, it adds that text to the Memo component; both the text and the client RemoteAddress are added, so you'll know which client sent the information.

In more sophisticated implementations, aliases for known IP addresses can serve as a substitute.

For a more complex project that uses these components, explore the Delphi > Demos > Internet > Chat project. It's a simple network chat application that uses one form (project) for both the server and the client.