ClientSocket組件為客戶端組件
ServerSocket組件為服務器端組件
ServerSocket組件可以同時接受一個或多個ClientSocket組件的連接請求
一
本例包括一個服務器端程序和一個客戶端程序
本例的重點
type
client_record=record
CHandle: integer; //客戶端套接字句柄
CSocket:TCustomWinSocket; //客戶端套接字
CName:string; //客戶端計算機名稱
CAddress:string; //客戶端計算機IP地址
CUsed: boolean; //客戶端聯機標志
end;
利用這個記錄類型數據保存客戶端的信息
二
下面對組件ServerSocket和ClientSocket的屬性設置簡單說明
1
· Port
· ServerTypt
· ThreadCacheSize
其它屬性采用默認設置即可
2
· Port
· ClientType
· Host
其它屬性采用默認設置即可
三
1
unit uServerMain;
interface
uses
Windows
ScktComp
const
CMax=
type
client_record=record
CHandle: integer; //客戶端套接字句柄
CSocket:TCustomWinSocket; //客戶端套接字
CName:string; //客戶端計算機名稱
CAddress:string; //客戶端計算機IP地址
CUsed: boolean; //客戶端聯機標志
end;
type
TfrmServerMain = class(TForm)
ServerSocket: TServerSocket;
ControlBar
ToolBar
tbConnect: TToolButton;
tbClose: TToolButton;
tbDisconnected: TToolButton;
Edit
Memo
StatusBar: TStatusBar;
procedure tbConnectClick(Sender: TObject);
procedure tbDisconnectedClick(Sender: TObject);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketListen(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure tbCloseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ServerSocketGetSocket(Sender: TObject; Socket: Integer;
var ClientSocket: TServerClientWinSocket);
procedure ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
session: array[
Sessions: integer; //客戶端連接數
end;
var
frmServerMain: TfrmServerMain;
implementation
{$R *
//打開套接字連接
procedure TfrmServerMain
begin
ServerSocket
end;
//關閉套接字連接
procedure TfrmServerMain
begin
ServerSocket
StatusBar
end;
//從客戶端讀取信息
procedure TfrmServerMain
Socket: TCustomWinSocket);
var
i:integer;
begin
//將從客戶端讀取的信息添加到Memo
Memo
for i:=
begin
//取得匹配的客戶端
if session[i]
begin
session[i]
end;
end;
end;
//服務器端套接字進入監聽狀態
procedure TfrmServerMain
Socket: TCustomWinSocket);
begin
StatusBar
end;
//當客戶端連接到服務器端以後
procedure TfrmServerMain
Socket: TCustomWinSocket);
var
i
begin
j:=
for i:=
begin
//在原有的客戶端連接數組中有中斷的客戶端連接
if not session[i]
begin
session[i]
session[i]
session[i]
session[i]
session[i]
Break;
end;
j:=i;
end;
if j=sessions then
begin
inc(sessions);
session[j]
session[j]
session[j]
session[j]
session[j]
end;
StatusBar
end;
//當客戶端斷開連接時
procedure TfrmServerMain
Socket: TCustomWinSocket);
var
i:integer;
begin
for i:=
begin
if session[i]
begin
session[i]
session[i]
Break;
end;
end;
StatusBar
end;
//關閉窗口
procedure TfrmServerMain
begin
Close;
end;
procedure TfrmServerMain
begin
sessions :=
end;
procedure TfrmServerMain
var Action: TCloseAction);
begin
ServerSocket
end;
//當客戶端正在與服務器端連接時
procedure TfrmServerMain
Socket: Integer; var ClientSocket: TServerClientWinSocket);
begin
StatusBar
end;
//客戶端發生錯誤
procedure TfrmServerMain
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
StatusBar
ErrorCode :=
end;
end
2
unit uClientMain;
interface
uses
Windows
ScktComp
const
SocketHost =
type
TfrmClientMain = class(TForm)
ControlBar
ToolBar
tbConnected: TToolButton;
tbSend: TToolButton;
tbClose: TToolButton;
tbDisconnected: TToolButton;
ClientSocket: TClientSocket;
Edit
Memo
StatusBar: TStatusBar;
btnSend: TBitBtn;
procedure tbConnectedClick(Sender: TObject);
procedure tbDisconnectedClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure tbSendClick(Sender: TObject);
procedure tbCloseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmClientMain: TfrmClientMain;
implementation
{$R *
//打開套接字連接
procedure TfrmClientMain
begin
ClientSocket
end;
//關閉套接字連接
procedure TfrmClientMain
begin
ClientSocket
end;
//接受服務器端的回復
procedure TfrmClientMain
Socket: TCustomWinSocket);
begin
Memo
end;
//發送信息到服務器端
procedure TfrmClientMain
begin
ClientSocket
end;
procedure TfrmClientMain
begin
Close;
end;
//設置要連接的服務器端地址
procedure TfrmClientMain
begin
ClientSocket
end;
//已經連接到服務器端
procedure TfrmClientMain
Socket: TCustomWinSocket);
begin
tbSend
tbDisconnected
btnSend
StatusBar
end;
//正在連接到服務器端
procedure TfrmClientMain
Socket: TCustomWinSocket);
begin
StatusBar
end;
//當斷開與服務器端的連接時發生
procedure TfrmClientMain
Socket: TCustomWinSocket);
begin
tbSend
btnSend
tbDisconnected
StatusBar
end;
procedure TfrmClientMain
var Action: TCloseAction);
begin
ClientSocket
end;
//當與服務器端的連接發生錯誤時
procedure TfrmClientMain
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
StatusBar
ErrorCode :=
end;
end
四
上述方法是比較簡單的實現方法
五
服務器端窗體和客戶端窗體及組件的屬性設置隊應的DFM文件
uServerMain
object frmServerMain: TfrmServerMain
Left =
Top =
BorderIcons = [biSystemMenu
BorderStyle = bsSingle
Caption =
ClientHeight =
ClientWidth =
Color = clBtnFace
Font
Font
Font
Font
Font
OldCreateOrder = False
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch =
TextHeight =
object ControlBar
Left =
Top =
Width =
Height =
Align = alTop
AutoSize = True
TabOrder =
object ToolBar
Left =
Top =
Width =
Height =
ButtonHeight =
ButtonWidth =
Caption =
EdgeInner = esNone
EdgeOuter = esNone
Flat = True
ShowCaptions = True
TabOrder =
object tbConnect: TToolButton
Left =
Top =
Caption =
ImageIndex =
OnClick = tbConnectClick
end
object tbDisconnected: TToolButton
Left =
Top =
Caption =
ImageIndex =
OnClick = tbDisconnectedClick
end
object tbClose: TToolButton
Left =
Top =
Caption =
ImageIndex =
OnClick = tbCloseClick
end
end
end
object Edit
Left =
Top =
Width =
Height =
TabOrder =
Text =
end
object Memo
Left =
Top =
Width =
Height =
Align = alTop
TabOrder =
end
object StatusBar: TStatusBar
Left =
Top =
Width =
Height =
Panels = <
item
Width =
end>
SimplePanel = False
end
object ServerSocket: TServerSocket
Active = False
Port =
ServerType = stNonBlocking
OnListen = ServerSocketListen
OnGetSocket = ServerSocketGetSocket
OnClientConnect = ServerSocketClientConnect
OnClientDisconnect = ServerSocketClientDisconnect
OnClientRead = ServerSocketClientRead
OnClientError = ServerSocketClientError
Left =
end
end
uClientMain
object frmClientMain: TfrmClientMain
Left =
Top =
BorderIcons = [biSystemMenu
BorderStyle = bsSingle
Caption =
ClientHeight =
ClientWidth =
Color = clBtnFace
Font
Font
Font
Font
Font
OldCreateOrder = False
Position = poScreenCenter
OnClose = FormClose
OnShow = FormShow
PixelsPerInch =
TextHeight =
object ControlBar
Left =
Top =
Width =
Height =
Align = alTop
AutoSize = True
TabOrder =
object ToolBar
Left =
Top =
Width =
Height =
ButtonHeight =
ButtonWidth =
Caption =
EdgeInner = esNone
EdgeOuter = esNone
Flat = True
ShowCaptions = True
TabOrder =
object tbConnected: TToolButton
Left =
Top =
Caption =
ImageIndex =
OnClick = tbConnectedClick
end
object tbSend: TToolButton
Left =
Top =
Caption =
Enabled = False
ImageIndex =
OnClick = tbSendClick
end
object tbDisconnected: TToolButton
Left =
Top =
Caption =
Enabled = False
ImageIndex =
OnClick = tbDisconnectedClick
end
object tbClose: TToolButton
Left =
Top =
Caption =
ImageIndex =
OnClick = tbCloseClick
end
end
end
object Edit
Left =
Top =
Width =
Height =
TabOrder =
Text =
end
object Memo
Left =
Top =
Width =
Height =
Align = alTop
TabOrder =
end
object StatusBar: TStatusBar
Left =
Top =
Width =
Height =
Panels = <
item
Width =
end>
SimplePanel = False
end
object btnSend: TBitBtn
Left =
Top =
Width =
Height =
Caption =
Enabled = False
TabOrder =
OnClick = tbSendClick
end
object ClientSocket: TClientSocket
Active = False
ClientType = ctNonBlocking
Port =
OnConnecting = ClientSocketConnecting
OnConnect = ClientSocketConnect
OnDisconnect = ClientSocketDisconnect
OnRead = ClientSocketRead
OnError = ClientSocketError
Left =
end
end
From:http://tw.wingwit.com/Article/program/Delphi/201311/8511.html