DELPHI SOKET威尼斯人6799.com 编制程序–使用TServerSocket和TClientSocket

本文接纳delphi七+TServerSocket+TClientSocket;

正文选拔delphi柒+TServerSocket+TClientSocket;

 

 

笔者在工作中境遇对局域网中各工作站与服务器之间举办Socket通讯的题材。以往将自己计算出来的TServerSocket和TClientSocket三个零件的骨干用法写出来,希望与你分享。

小编在工作中蒙受对局域网中各工作站与服务器之间进行Socket通讯的题材。现在将自身总计出来的TServerSocket和TClientSocket七个零件的为主用法写出来,希望与你分享。

  ClientSocket组件为客户端组件。它是通信的请求方,也便是说,它是前仆后继地与劳务器端建立连接。

  ClientSocket组件为客户端组件。它是通讯的请求方,也正是说,它是积极地与服务器端建立连接。

  ServerSocket组件为劳动器端组件。它是通讯的响应方,也正是说,它的动作是监听以及被动接受客户端的连年请求,并对请求实行苏醒。

  ServerSocket组件为服务器端组件。它是通讯的响应方,相当于说,它的动作是监听以及被动接受客户端的接连请求,并对请求进行还原。

  ServerSocket组件可以而且接受一个或五个ClientSocket组件的几次三番请求,并与种种ClientSocket组件建立单独的连日,实行独立的通讯。由此,3个劳动器端可以为四个客户端服务。

  ServerSocket组件能够而且接受3个或八个ClientSocket组件的接连请求,并与种种ClientSocket组件建立独立的延续,举行独立的通讯。因而,3个劳动器端能够为多少个客户端服务。

  设计思路

  设计思路

  本例包罗贰个劳务器端程序和2个客户端程序。客户端程序能够放置多个计算机上运维,同时与服务器端进行三番五次通讯。

  本例包罗二个劳务器端程序和八个客户端程序。客户端程序能够放置多个总计机上运营,同时与服务器端实行再三再四通讯。

  本例的基本点,1是出现说法客户端与服务器端怎么着通讯;二是当有多个客户端同时连接受服务器端时,服务器端怎么样鉴定分别每一个客户端,并对请求提交相应的恢复生机。为了确定保障一个客户端断开连接时不影响其余客户端与劳动器端的通讯,同时保证服务器端能够科学回复客户端的伸手,在本例中声称了1个记下类型:

  本例的最首要,壹是出现说法客户端与服务器端如何通讯;贰是当有八个客户端同时连接受服务器端时,服务器端怎么着识别每一种客户端,并对请求提交相应的东山再起。为了保障多个客户端断开连接时不影响别的客户端与劳务器端的通讯,同时确定保证服务器端能够科学回复客户端的伸手,在本例中申明了一个笔录类型:

  type
 client_record=record
 CHandle: integer; //客户端套接字句柄
 CSocket:TCustomWinSocket; //客户端套接字
 CName:string; //客户端总括机名称
 CAddress:string; //客户端计算机IP地址
 CUsed: boolean; //客户端联机标志
end;

  type
 client_record=record
 CHandle: integer; //客户端套接字句柄
 CSocket:TCustomWinSocket; //客户端套接字
 CName:string; //客户端总结机名称
 CAddress:string; //客户端总括机IP地址
 CUsed: boolean; //客户端联机标志
end;

  利用那些记录类型数据保存客户端的消息,同时保留当前客户端的接连意况。当中,CHandle保存客户端套接字句柄,以便规范定位种种与服务器端保持一连的客户端;Csocket保存客户端套接字,通过它能够对客户端实行还原。Cused记录当前客户端是或不是与劳动器端保持一而再。

  利用这几个记录类型数据保存客户端的新闻,同时保留当前客户端的连年情状。当中,CHandle保存客户端套接字句柄,以便规范定位每个与劳动器端保持延续的客户端;Csocket保存客户端套接字,通过它能够对客户端进行还原。Cused记录当前客户端是否与劳动器端保持连续。

 

 

 

 

上边对组件ServerSocket和ClientSocket的天性设置不难说明。

上边对组件ServerSocket和ClientSocket的性质设置简单表明。

  ServerSocket的属性:

  ServerSocket的属性:

  · Port,是通讯的端口,必须安装。在本例中设置为十二五;

  · Port,是通讯的端口,必须安装。在本例中设置为10二5;

  ·
ServerTypt,服务器端读写新闻项目,设置为stNonBlocking表示异步读写音讯,本例中使用那种办法。

  ·
ServerTypt,服务器端读写音讯项目,设置为stNonBlocking表示异步读写消息,本例中央银行使那种办法。

  ·
ThreadCacheSize,客户端的最浦那接数,正是劳务器端最多允许多少客户端同时连接。本例选取暗中同意值十。

  ·
ThreadCacheSize,客户端的最明斯克接数,正是服务器端最多允许多少客户端同时连接。本例选拔暗中认可值10。

  其它性质选取暗中认可设置即可。

  其余性质接纳私下认可设置即可。

  ClientSocket的属性:

  ClientSocket的属性:

  · Port,是通讯的端口,必须与劳动器端的安装同样。在本例中装置为10二5;

  · Port,是通讯的端口,必须与服务器端的装置同样。在本例中设置为十二伍;

  ·
ClientType,客户端读写音信项目,应该与劳动器端的安装同样,为stNonBlocking表示异步读写消息。

  ·
ClientType,客户端读写消息连串,应该与服务器端的装置同一,为stNonBlocking表示异步读写消息。

  ·
Host,客户端要连接的服务器的IP地址。必须设置,当然也能够在代码中动态设置。

  ·
Host,客户端要接二连三的服务器的IP地址。必须设置,当然也足以在代码中动态设置。

  其余性质采取暗许设置即可。

  别的性质选取暗中认可设置即可。

  程序源代码:

  程序源代码:

  · 服务器端源码(uServerMain.pas):

  · 服务器端源码(uServerMain.pas):

  unit uServerMain;
interface
 uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons;
 const
  C马克斯=十; //客户端最达累斯萨Lamb接数
 type
  client_record=record
  CHandle: integer; //客户端套接字句柄
  CSocket:TCustomWinSocket; //客户端套接字
  CName:string; //客户端计算机名称
  CAddress:string; //客户端总结机IP地址
  CUsed: boolean; //客户端联机标志
 end;
 type
  TfrmServerMain = class(TForm)
  ServerSocket: TServerSocket;
  ControlBar1: TControlBar;
  ToolBar1: TToolBar;
  tbConnect: TToolButton;
  tbClose: TToolButton;
  tbDisconnected: TToolButton;
  Edit1: TEdit;
  Memo1: TMemo;
  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[0..CMax] of client_record; //客户端连接数组
  Sessions: integer; //客户端连接数
 end;
 var
  frmServerMain: TfrmServerMain;
  implementation
  {$R *.DFM}
  //打开套接字连接,并使套接字进入监听状态
  procedure TfrmServerMain.tbConnectClick(Sender: TObject);
 begin
  ServerSocket.Open ;
 end;
 //关闭套接字连接,不再监听客户端的央浼
 procedure TfrmServerMain.tbDisconnectedClick(Sender: TObject);
 begin
  ServerSocket.Close;
  StatusBar.Panels[0].Text
:=’服务器套接字连接已经倒闭,不可能接受客户端的接连请求.’;
 end;
 //从客户端读取新闻
 procedure TfrmServerMain.ServerSocketClientRead(Sender:
TObject;Socket: TCustomWinSocket);
 var
  i:integer;
 begin
  //将从客户端读取的音讯添加到Memo第11中学
  Memo1.Lines.Add(Socket.ReceiveText);
  for i:=0 to sessions do
  begin
   //取得格外的客户端
   if session[i].CHandle = Socket.SocketHandle then
   begin
    session[i].CSocket.SendText(‘回复客户端’+session[i].CAddress+’
==> ‘+Edit1.Text);
   end;
  end;
 end;
 //服务器端套接字进入监听状态,以便监听客户端的连年
 procedure TfrmServerMain.ServerSocketListen(Sender: TObject;Socket:
TCustomWinSocket);
 begin
  StatusBar.Panels[0].Text :=’等待客户端连接…’;
 end;
 //当客户端连接到服务器端今后
 procedure TfrmServerMain.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
 var
  i,j:integer;
 begin
  j:=-1;
  for i:=0 to sessions do
  begin
   //在原本的客户端连接数组中有抛锚的客户端连接
   if not session[i].CUsed then
   begin
    session[i].CHandle := Socket.SocketHandle
;//客户端套接字句柄
    session[i].CSocket := Socket; //客户端套接字
    session[i].CName := Socket.RemoteHost ; //客户端总计机名称
    session[i].CAddress := Socket.RemoteAddress
;//客户端总结机IP
    session[i].CUsed := True; //连接数组当前地点已经占据
    Break;
   end;
  j:=i;
 end;
 if j=sessions then
 begin
  inc(sessions);
  session[j].CHandle := Socket.SocketHandle ;
  session[j].CSocket := Socket;
  session[j].CName := Socket.RemoteHost ;
  session[j].CAddress := Socket.RemoteAddress ;
  session[j].CUsed := True;
 end;
 StatusBar.Panels[0].Text := ‘客户端 ‘+Socket.RemoteHost + ‘
已经接二连三’;
end;
//当客户端断开连接时
procedure TfrmServerMain.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
 i:integer;
begin
 for i:=0 to sessions do
 begin
  if session[i].CHandle =Socket.SocketHandle then
  begin
   session[i].CHandle :=0;
   session[i].CUsed := False;
   Break;
  end;
 end;
 StatusBar.Panels[0].Text :=’客户端 ‘+Socket.RemoteHost + ‘
已经断开’;
end;
//关闭窗口
procedure TfrmServerMain.tbCloseClick(Sender: TObject);
begin
 Close;
end;
procedure TfrmServerMain.FormCreate(Sender: TObject);
begin
 sessions := 0;
end;
procedure TfrmServerMain.FormClose(Sender: TObject;var Action:
TCloseAction);
begin
 ServerSocket.Close ;
end;
//当客户端正在与劳务器端连接时
procedure TfrmServerMain.ServerSocketGetSocket(Sender: TObject;
Socket: Integer; var ClientSocket: TServerClientWinSocket);
begin
 StatusBar.Panels[0].Text :=’客户端正在连接…’;
end;
//客户端爆发错误
procedure TfrmServerMain.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
 StatusBar.Panels[0].Text :=’客户端’+Socket.RemoteHost
+’发生错误!’;
 ErrorCode := 0;
end;
end.

  unit uServerMain;
interface
 uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  ScktComp, ToolWin, ComCtrls, ExtCtrls, StdCtrls, Buttons;
 const
  C马克斯=10; //客户端最菲尼克斯接数
 type
  client_record=record
  CHandle: integer; //客户端套接字句柄
  CSocket:TCustomWinSocket; //客户端套接字
  CName:string; //客户端计算机名称
  CAddress:string; //客户端总结机IP地址
  CUsed: boolean; //客户端联机标志
 end;
 type
  TfrmServerMain = class(TForm)
  ServerSocket: TServerSocket;
  ControlBar1: TControlBar;
  ToolBar1: TToolBar;
  tbConnect: TToolButton;
  tbClose: TToolButton;
  tbDisconnected: TToolButton;
  Edit1: TEdit;
  Memo1: TMemo;
  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[0..CMax] of client_record; //客户端连接数组
  Sessions: integer; //客户端连接数
 end;
 var
  frmServerMain: TfrmServerMain;
  implementation
  {$R *.DFM}
  //打开套接字连接,并使套接字进入监听状态
  procedure TfrmServerMain.tbConnectClick(Sender: TObject);
 begin
  ServerSocket.Open ;
 end;
 //关闭套接字连接,不再监听客户端的请求
 procedure TfrmServerMain.tbDisconnectedClick(Sender: TObject);
 begin
  ServerSocket.Close;
  StatusBar.Panels[0].Text
:=’服务器套接字连接已经倒闭,不恐怕接受客户端的连接请求.’;
 end;
 //从客户端读撤销息
 procedure TfrmServerMain.ServerSocketClientRead(Sender:
TObject;Socket: TCustomWinSocket);
 var
  i:integer;
 begin
  //将从客户端读取的新闻添加到Memo第11中学
  Memo1.Lines.Add(Socket.ReceiveText);
  for i:=0 to sessions do
  begin
   //取得格外的客户端
   if session[i].CHandle = Socket.SocketHandle then
   begin
    session[i].CSocket.SendText(‘回复客户端’+session[i].CAddress+’
==> ‘+Edit1.Text);
   end;
  end;
 end;
 //服务器端套接字进入监听状态,以便监听客户端的总是
 procedure TfrmServerMain.ServerSocketListen(Sender: TObject;Socket:
TCustomWinSocket);
 begin
  StatusBar.Panels[0].Text :=’等待客户端连接…’;
 end;
 //当客户端连接到服务器端现在
 procedure TfrmServerMain.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
 var
  i,j:integer;
 begin
  j:=-1;
  for i:=0 to sessions do
  begin
   //在原来的客户端连接数组中有停顿的客户端连接
   if not session[i].CUsed then
   begin
    session[i].CHandle := Socket.SocketHandle
;//客户端套接字句柄
    session[i].CSocket := Socket; //客户端套接字
    session[i].CName := Socket.RemoteHost ; //客户端总计机名称
    session[i].CAddress := Socket.RemoteAddress
;//客户端总括机IP
    session[i].CUsed := True; //连接数组当前地点已经占据
    Break;
   end;
  j:=i;
 end;
 if j=sessions then
 begin
  inc(sessions);
  session[j].CHandle := Socket.SocketHandle ;
  session[j].CSocket := Socket;
  session[j].CName := Socket.RemoteHost ;
  session[j].CAddress := Socket.RemoteAddress ;
  session[j].CUsed := True;
 end;
 StatusBar.Panels[0].Text := ‘客户端 ‘+Socket.RemoteHost + ‘
已经延续’;
end;
//当客户端断开连接时
procedure TfrmServerMain.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
 i:integer;
begin
 for i:=0 to sessions do
 begin
  if session[i].CHandle =Socket.SocketHandle then
  begin
   session[i].CHandle :=0;
   session[i].CUsed := False;
   Break;
  end;
 end;
 StatusBar.Panels[0].Text :=’客户端 ‘+Socket.RemoteHost + ‘
已经断开’;
end;
//关闭窗口
procedure TfrmServerMain.tbCloseClick(Sender: TObject);
begin
 Close;
end;
procedure TfrmServerMain.FormCreate(Sender: TObject);
begin
 sessions := 0;
end;
procedure TfrmServerMain.FormClose(Sender: TObject;var Action:
TCloseAction);
begin
 ServerSocket.Close ;
end;
//当客户端正在与劳动器端连接时
procedure TfrmServerMain.ServerSocketGetSocket(Sender: TObject;
Socket: Integer; var ClientSocket: TServerClientWinSocket);
begin
 StatusBar.Panels[0].Text :=’客户端正在连接…’;
end;
//客户端产生错误
procedure TfrmServerMain.ServerSocketClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
 StatusBar.Panels[0].Text :=’客户端’+Socket.RemoteHost
+’爆发错误!’;
 ErrorCode := 0;
end;
end.

 

 

· 客户端源码(uClientMain.pas):

· 客户端源码(uClientMain.pas):

 

 

unit uClientMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ScktComp, ComCtrls, ToolWin, ExtCtrls, StdCtrls, Buttons;
const
 SocketHost = ’17二.16.1.陆’; //服务器端地址
type
 TfrmClientMain = class(TForm)
 ControlBar1: TControlBar;
 ToolBar1: TToolBar;
 tbConnected: TToolButton;
 tbSend: TToolButton;
 tbClose: TToolButton;
 tbDisconnected: TToolButton;
 ClientSocket: TClientSocket;
 Edit1: TEdit;
 Memo1: TMemo;
 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 *.DFM}
  //打开套接字连接
  procedure TfrmClientMain.tbConnectedClick(Sender: TObject);
  begin
   ClientSocket.Open ;
  end;
  //关闭套接字连接
  procedure TfrmClientMain.tbDisconnectedClick(Sender: TObject);
  begin
   ClientSocket.Close;
  end;
  //接受劳务器端的上升
  procedure TfrmClientMain.ClientSocketRead(Sender: TObject;Socket:
TCustomWinSocket);
  begin
   Memo1.Lines.Add(Socket.ReceiveText);
  end;
  //发送新闻到劳动器端
  procedure TfrmClientMain.tbSendClick(Sender: TObject);
  begin
   ClientSocket.Socket.SendText(Edit1.Text);
  end;
  procedure TfrmClientMain.tbCloseClick(Sender: TObject);
  begin
   Close;
  end;
  //设置要一而再的服务器端地址
  procedure TfrmClientMain.FormShow(Sender: TObject);
  begin
   ClientSocket.Host := SocketHost;
  end;
  //已经连接到服务器端
  procedure TfrmClientMain.ClientSocketConnect(Sender: TObject;Socket:
TCustomWinSocket);
  begin
   tbSend.Enabled := True;
   tbDisconnected.Enabled :=True;
   btnSend.Enabled := True;
   StatusBar.Panels[0].Text := ‘已经一而再到 ‘+ Socket.RemoteHost ;
  end;
  //正在连接到劳动器端
  procedure TfrmClientMain.ClientSocketConnecting(Sender:
TObject;Socket: TCustomWinSocket);
  begin
   StatusBar.Panels[0]威尼斯人6799.com,.Text := ‘正在连接到劳动器… ‘ ;
  end;
  //当断开与劳务器端的连年时发出
  procedure TfrmClientMain.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
  begin
   tbSend.Enabled := False;
   btnSend.Enabled := False;
   tbDisconnected.Enabled := False;
   StatusBar.Panels[0].Text := ‘已经断开与 ‘+ Socket.RemoteHost +’
的连接’;
  end;
  procedure TfrmClientMain.FormClose(Sender: TObject;
var Action: TCloseAction);
  begin
   ClientSocket.Close ;
  end;
  //当与服务器端的连日发生错误时
  procedure TfrmClientMain.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;var ErrorCode:
Integer);
  begin
   StatusBar.Panels[0].Text := ‘与劳动器端的三番五次发生错误’;
   ErrorCode := 0;
  end;
 end.

unit uClientMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ScktComp, ComCtrls, ToolWin, ExtCtrls, StdCtrls, Buttons;
const
 SocketHost = ‘172.1陆.1.6’; //服务器端地址
type
 TfrmClientMain = class(TForm)
 ControlBar1: TControlBar;
 ToolBar1: TToolBar;
 tbConnected: TToolButton;
 tbSend: TToolButton;
 tbClose: TToolButton;
 tbDisconnected: TToolButton;
 ClientSocket: TClientSocket;
 Edit1: TEdit;
 Memo1: TMemo;
 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 *.DFM}
  //打开套接字连接
  procedure TfrmClientMain.tbConnectedClick(Sender: TObject);
  begin
   ClientSocket.Open ;
  end;
  //关闭套接字连接
  procedure TfrmClientMain.tbDisconnectedClick(Sender: TObject);
  begin
   ClientSocket.Close;
  end;
  //接受劳动器端的死灰复燃
  procedure TfrmClientMain.ClientSocketRead(Sender: TObject;Socket:
TCustomWinSocket);
  begin
   Memo1.Lines.Add(Socket.ReceiveText);
  end;
  //发送音讯到劳动器端
  procedure TfrmClientMain.tbSendClick(Sender: TObject);
  begin
   ClientSocket.Socket.SendText(Edit1.Text);
  end;
  procedure TfrmClientMain.tbCloseClick(Sender: TObject);
  begin
   Close;
  end;
  //设置要连接的服务器端地址
  procedure TfrmClientMain.FormShow(Sender: TObject);
  begin
   ClientSocket.Host := SocketHost;
  end;
  //已经连接到劳动器端
  procedure TfrmClientMain.ClientSocketConnect(Sender: TObject;Socket:
TCustomWinSocket);
  begin
   tbSend.Enabled := True;
   tbDisconnected.Enabled :=True;
   btnSend.Enabled := True;
   StatusBar.Panels[0].Text := ‘已经三番五次到 ‘+ Socket.RemoteHost ;
  end;
  //正在连接到劳动器端
  procedure TfrmClientMain.ClientSocketConnecting(Sender:
TObject;Socket: TCustomWinSocket);
  begin
   StatusBar.Panels[0].Text := ‘正在连接到劳动器… ‘ ;
  end;
  //当断开与劳务器端的连天时发出
  procedure TfrmClientMain.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
  begin
   tbSend.Enabled := False;
   btnSend.Enabled := False;
   tbDisconnected.Enabled := False;
   StatusBar.Panels[0].Text := ‘已经断开与 ‘+ Socket.RemoteHost +’
的总是’;
  end;
  procedure TfrmClientMain.FormClose(Sender: TObject;
var Action: TCloseAction);
  begin
   ClientSocket.Close ;
  end;
  //当与服务器端的连接产生错误时
  procedure TfrmClientMain.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;var ErrorCode:
Integer);
  begin
   StatusBar.Panels[0].Text := ‘与服务器端的连日发生错误’;
   ErrorCode := 0;
  end;
 end.

http://blog.csdn.net/yanjiaye520/article/details/6321133

http://blog.csdn.net/yanjiaye520/article/details/6321133