?? clientunit.~pas
字號:
unit ClientUnit;
//說明: 這是一個簡單的實例, 僅僅表明屬性設(shè)置, 事件編程, 連接及連接情況,
// 發(fā)送數(shù)據(jù),接收數(shù)據(jù)的全過程. 其中的發(fā)送和接收使用的方法
// SendText和ReceiveText 都是針對字符串的,
// 現(xiàn)實中的數(shù)據(jù)應(yīng)該是二進(jìn)制數(shù)據(jù), 應(yīng)該使用
// Sendbuf和Receivebuf ,處理過程也要復(fù)雜得多
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ScktComp, ExtCtrls;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket; //客戶 Socket 控件
BitBtn1: TBitBtn; //連接按鈕
Label1: TLabel; //顯示連接情況
Memo1: TMemo; //鍵入發(fā)送內(nèi)容的文本編輯器
Button1: TButton; //發(fā)送按鈕
Memo2: TMemo; //存放接收到的內(nèi)容
Label2: TLabel; //顯示收到字符數(shù)
Button2: TButton; //斷開按鈕
Button3: TButton; //打開按鈕
LabeledEdit1: TLabeledEdit; //編輯IP地址
LabeledEdit2: TLabeledEdit;
Label3: TLabel; //編輯Port號
procedure BitBtn1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormShow(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject); //連接按鈕
begin
ClientSocket1.ClientType:=ctNonBlocking; //選擇無阻塞方式
ClientSocket1.Address:=LabeledEdit1.Text; //連接前設(shè)定IP地址
ClientSocket1.Port:=strtoint(Labelededit2.Text); //連接前設(shè)定 端口號
ClientSocket1.Active:=true; //連接的動作
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket); //連接成功的事件
begin
label1.Caption:='連接 OK!'; //連接成功的提示信息
Button1.Enabled:=true; //將發(fā)送按鈕設(shè)為可用
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket); //連接失敗事件
begin
label1.Caption:='無連接'; //連接失敗時提示信息
Button1.Enabled:=false; //將發(fā)送按鈕設(shè)為不能用
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText(memo1.text); //發(fā)送memo1中的數(shù)據(jù)
memo1.Lines.Clear; //清理memo1
label3.Caption:='字符數(shù)'+inttostr(length(memo1.text));
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket); //讀數(shù)據(jù)事件
var ss:string;
begin
ss:=ClientSocket1.Socket.ReceiveText; //及時獲得服務(wù)器送來的信息
memo2.Lines.Add(ss); //將獲得的字符串放進(jìn)memo1,使之成為可見
end;
procedure TForm1.FormShow(Sender: TObject); //屏幕初始事件
begin
LabeledEdit2.Text:=inttostr(ClientSocket1.Port); //屏幕上獲得客戶端口號
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ClientSocket1.Close; //關(guān)閉連接
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientSocket1.ClientType:=ctNonBlocking; //選擇無阻塞方式
ClientSocket1.Address:=LabeledEdit1.Text; //連接前設(shè)定IP地址
ClientSocket1.Port:=strtoint(Labelededit2.Text); //連接前設(shè)定 端口號
ClientSocket1.Open; //打開連接
//同 ClientSocket1.Active:=true 意思相同
end;
end.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -