| Exchanging Data over the Network using Delphi | ||||||||||||||||||||||||||||||
| In this article we'll examine two Delphi components: TServerSocket and TClientSocket, both designed to let you read and write information over a TCP/IP connection - thus enabling you to write network-aware applications. | ||||||||||||||||||||||||||||||
Delphi provides numerous objects to allow you write applications that exchange data over the network (Internet, intranet, local). In this article we'll examine two Delphi components: TServerSocket and TClientSocket, both designed to let you read and write information over a TCP/IP connection.
On Winsock and Delphi socket components
Delphi socket components (wrappers for the WinSock) let you create an application that can communicate with other systems using TCP/IP and related protocols. Using sockets, you can read and write over connections to other machines without worrying about the details of the underlying networking software. How to reach a particular service on a specific network On Ports and Hosts To start, fire up Delphi twice, one project for the server application and one for the client.
The server side of the story In the OnCreate event for the form add the next code:
Next, the OnClose should look like:
The client side of the story
The code pretty much describes itself: when a client clicks a button, the text specified inside the Edit1 component will be send to the server with specified port and host address.
Back to server!
To easy? Well no of course not - it works! But, what if you have more than one client sending data to the server? In such situations you'll need a little more to code:
That's all. 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. See it in action
![]() p.s. For a more complex project be sure to explore the Delphi\Demos\Internet\Chat project - a simple network chat application - it uses one form (project) for both the server and the client. In the next part of this article I'll show you how to use Socket components to send raw data (files) across the network! As always I encourage you to post your views, comments, questions and doubts to this article on the Delphi Programming Forum. Discuss! |
||||||||||||||||||||||||||||||

On a form drop one TServerSocket component, and one TMemo component. Let it look like:
For the client application, add a TClientSocket, a TEdit and a TButton components to a form.
It could look something like:
And, here's all the code you need on the client:

