Quick Links


General
• Home
• Teachers
• Module Organiser
• Timetable
• Exams
• Activities (Weekly)
• Activities
• Ask a Question
• Index

Emulators
• Router
• Switch
• UNIX
• Wireless
• 5-router
• Router/switch
• Modem
• Host

Material
• Notes
• Presentations
• Tutorials
• Challenge
• Worksheets

Tests
• On-line tests
• Millionaire Test
• Request a re-assess

Quizzes
• Pub Challenge
• OSI Model  
• Bits, bytes and frames
• Hangman
• Subnetting
• IQ Test

Free Updates

Network Emulators
CNDS site


WinSock client/server (Delphi server)

details

You may use Visual Basic, Java or Delphi for the implementation of Client/Server programming:

Java client/server
VB client/server
Delphi client
Delphi server

related links

Delphi Client/Server ZIP

how to?

A server differs for a client in that it waits for a connection from a client. When a client and server are used together it is known as a client-server architecture. A sample design with the server component is given in Figure 1. Initially the server goes into a listening mode with the following:

Setting server into listen mode

procedure TForm1.FormCreate(Sender: TObject);
var port:integer;
begin
port:=StrToInt(Edit1.Text);
ServerSocket1.Port:=port;
ServerSocket1.Active:=True;
edit4.text:='Listening ...';
end;

This will listen on the port defined by the port variable. When a client connects the Accept event is called. Example code for this is:

Reacting to a connection request

procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Checkbox1.Checked:=true;
edit2.text:=Socket.RemoteAddress;
edit3.Text:=Socket.RemoteHost;
edit5.text:=Socket.LocalAddress;
edit4.text:='Connected';
end;

which displays that the connection has been made.

Figure 1: Sample design for a server

The ClientWrite event is called when the client sends data to the server. An example of the code is:

Reacting to a receiving data

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

 

and the server can send data to the server using the following:

Sending data to the client

ServerSocket1.Socket.Connections[0].SendText(edit6.text);

An example program (which should work with the client from the previous section) is:

Example program

unit server01;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
CheckBox1: TCheckBox;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
GroupBox2: TGroupBox;
Edit6: TEdit;
Button1: TButton;
Memo1: TMemo;
Label7: TLabel;
Label8: TLabel;
Button2: TButton;
ServerSocket1: TServerSocket;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ServerSocket1ClientWrite(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var port:integer;
begin
port:=StrToInt(Edit1.Text);
ServerSocket1.Port:=port;
ServerSocket1.Active:=True;
edit4.text:='Listening ...';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Socket.Connections[0].SendText(edit6.text);
edit6.text:='';
end;

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

procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Checkbox1.Checked:=true;
edit2.text:=Socket.RemoteAddress;
edit3.Text:=Socket.RemoteHost;
edit5.text:=Socket.LocalAddress;
edit4.text:='Connected';
end;

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

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

end.

 


Figure 3 Sample run

The client and server can be tested by running each of them on the same machine, and using the address of 127.0.0.1 (which specifies the local address). Once the program is working, the client can be run on one machine and the server on another. The client must know the IP address (or host name) of the server. This address can be viewed from the server application.


Ref: Extract from Chapter 13, Mastering Delphi, W.Buchanan, Palgrave, 2002.