?? delphiqq.txt
字號(hào):
TCP/IP協(xié)議的傳輸即面向點(diǎn)到點(diǎn)的傳輸方式!
1.創(chuàng)建應(yīng)用程序。
選擇“NEW”菜單下的“Application”選項(xiàng),創(chuàng)建一個(gè)普通的應(yīng)用程序。
2.創(chuàng)建所需控件。
首先在控件欄的Win32頁(yè)中選擇ImageList控件和CoolBar控件,再?gòu)腤in32欄選擇ToolBar控件放置到CoolBar
控件上。用鼠標(biāo)右鍵單擊“ImageList”控件,在彈出的菜單中選擇“ImageList Editer...”選項(xiàng),彈出
“ImageList Editer”對(duì)話框,單擊“Add...”按鈕,選擇5幅位圖。在對(duì)象管理器Object Inspector中將
ToolBar控件的Image屬性設(shè)為“ImageList1”,用鼠標(biāo)右鍵單擊“ToolBar”控件,選“New Button”選項(xiàng),
總共創(chuàng)建5個(gè)ToolButton,在 對(duì)象管理器中將5個(gè)ToolButton的ImageIndex屬性分別設(shè)置為0,1,2,3,4。
此時(shí)ImageList控件中的5幅位圖將會(huì)分別顯示在Toolbutton上,將5個(gè)ToolButton控件的ShowHint屬性全都設(shè)
置為“ture”,并將它們的Hint屬性分別設(shè)置為“監(jiān)聽(tīng)”、“連接”、“斷開(kāi)連接”、“更改你的昵稱(chēng)”和
“退出聊天程序”。
然后在窗體中放置一個(gè)Edit控件、Memo控件、StatusBar控件和一個(gè)Label控件。將Label控件的Caption屬性
設(shè)置為“輸入框”。
最后,也是最關(guān)鍵的,在控件欄的Internet頁(yè)中選擇SeverSocket控件和ClientSocket控件放置在窗體中,將
SeverSocket控件和ClientSocket控件的Port屬性設(shè)置為“1100”。SeverSocket控件是基于TCP/IP協(xié)議傳輸
的服務(wù)器方的控件,它的主要作用是用來(lái)監(jiān)聽(tīng)其它基于TCP/IP傳輸計(jì)算機(jī)的連接請(qǐng)求,并在收到連接請(qǐng)求時(shí)
建立連接,進(jìn)行數(shù)據(jù)傳輸,ClientSocket控件是基于TCP/IP傳輸?shù)目蛻舴降目丶闹饕饔檬窍虮O(jiān)聽(tīng)
TCP/IP傳輸?shù)姆?wù)器發(fā)出連接請(qǐng)求,在收到服務(wù)器的允許連接的響應(yīng)后,建立連接,并傳輸數(shù)據(jù),之所以在
窗體中同時(shí)創(chuàng)建ServerSocket和ClientSocket控件,是因?yàn)閼?yīng)用程序既可作為服務(wù)器,又可作為客戶端使用。
3.Serversocket和ClientSocket之間的連接
首先設(shè)置兩個(gè)全局變量:
NickName:string;
b_Client:boolean;
其中NickName用于放聊天人的名稱(chēng),b_Client用于表明應(yīng)用程序是否作為客戶端進(jìn)行數(shù)據(jù)傳輸。
在窗體Form1的Oncreate事件中初始化變量,代碼如下:
procedure TForm1.FormCreate(Sender:TObject);
begin
NickName:+="我的昵稱(chēng)";
b_Client:=ture;
end;
雙擊ToolButton1,編寫(xiě)服務(wù)器監(jiān)聽(tīng)代碼如下:
procedure TForm1.Toolbutton1Click(Sender:TObject);
begin
ClientSocket1.close;
ServerSocket1.open;
StatusBar1.SimpleText:='開(kāi)始監(jiān)聽(tīng)’;
end;
雙擊ToolButton2,編寫(xiě)客戶的申請(qǐng)連接,代碼如下:
procedure TForm1.ToolButton2Click(Sender:TObject);
var s:string;
begin
if Clientsocket1.Active then
ClientSocket1.close;
if InputQuery('連接到計(jì)算機(jī)','要連接的計(jì)算機(jī)名稱(chēng)或IP地址:',s) then
if Length(s)>0 then
with ClientSocket1 do
begin
Host:=s;
open;
end;
end;
在對(duì)象管理器中,雙擊ClientSocket事件頁(yè)的OnConnecting事件,編寫(xiě)處理客戶等待連接請(qǐng)求,代碼
如下:
procedure TForm1.ClientSocket1Connecting(Sender:TObject;Socket:TCustomWinSocket);
begin
StatusBar1.SimpleText:='等待來(lái)自'+Socket.RemoteAddress+'的連接允許響應(yīng)...';
end;
在對(duì)象管理器中,雙擊SeverSocket事件頁(yè)的OnAccept事件,處理服務(wù)器響應(yīng)連接事件,代碼如下:
procedure TForm1.SeverSocket1Accept(Sender:TObject;Socket:TCustomWinSocket);
begin
b_Client:=false;
StatusBar1.SimpleText:='連接到'+Socket.RemoteAddress;
end;
在對(duì)象管理器中,雙擊ClientSocket事件頁(yè)的OnConnect事件,OnConnect事件在連接成功時(shí)被調(diào)用,代碼如下:
procedure TForm1.ClientSocket1Connect(Sender:TObject;Socket:TCustomWinSocket);
begin
b_Client:=ture;
StatusBar1.SimpleText:='連接成功';
end;
4.ServerSocket和ClientSocket之間的數(shù)據(jù)傳輸
聊天的內(nèi)容是通過(guò)Edit控件輸入并在敲回車(chē)鍵后顯示在Memo控件中,再傳輸?shù)脚c之連接的計(jì)算機(jī)中。
Edit的OnKeyDown事件代碼如下:
procedure TForm1.Edit1KeyDown(Sender:TObject;var Key:Word;Shift:TShiftState);
begin
if Key=VK_Return then
begin
Memo1.Lines.Add(NickName+':'+Edit1.Text0;
if b_Client then
ClientSocket1.Socket.SendText(Memo1.Lines[Memo1.lines.Count-1])
else
ServerSocket1.Socket.Connections[0].SendText(Memo1.Lines[Memo1.lines.Count-1]);
end;
end;
在ServerSocket控件的onread事件中編寫(xiě)服務(wù)器接收到數(shù)據(jù)后的動(dòng)作,代碼如下:
procedure TForm1.ServerSocket1ClientRead(Sender:TObject;Socket:TCustomWinSocket);
begin
Memo1.lines.Add(Socket.ReceiveText);
end;
在ClientSocket控件的onread事件中編寫(xiě)客戶端接收到數(shù)據(jù)后的動(dòng)作,代碼如下:
procedure TForm1.ClientSocket1Read(Sender:TObject;Socket:TCustomWinSocket);
begin
Memo1.lines.Add(Socket.ReceiveText);
end;
5.斷開(kāi)Serversocket和ClientSocket之間的連接
雙擊ToolButton3,編寫(xiě)客戶端斷開(kāi)的處理過(guò)程,代碼如下:
procedure TForm1.ToolButton3Click(Sender:TObject);
begin
ClientSocket1.close;
StatusBar1.SimpleText:='斷開(kāi)連接';
end;
編寫(xiě)服務(wù)器響應(yīng)客戶端斷開(kāi)的處理過(guò)程,代碼如下:
procedure TForm1.ServerSocket1ClientDisconnect(Sender:TObject;Socket:TCustomWinSocket);
begin
SeverSocket1.close;
StatusBar1.SimpleText:='斷開(kāi)連接';
end;
6.更改聊天者的昵稱(chēng)
雙擊Toolbutton4,編寫(xiě)更改昵稱(chēng)代碼如下:
procedure TForm1.ToolButton4Click(sender:TObject);
var
s:string;
begin
if InputQuery('更改昵稱(chēng)','你的新昵稱(chēng)',s) then
if Length(s)>0 then
NickName:=s;
end;
7.退出應(yīng)用程序
雙擊Toolbutton5,編寫(xiě)退出應(yīng)用程序代碼如下:
procedure TForm1.ToolButton5Click(sender:TObject);
ClientSocket1.close;
ServerSocket1.close;
Form1.close;
end;
8.保存并運(yùn)行應(yīng)用程序
最好在網(wǎng)上運(yùn)行該程序,如果沒(méi)聯(lián)網(wǎng),但你的計(jì)算機(jī)支持TCP/IP協(xié)議(可以通過(guò)網(wǎng)絡(luò)鄰居安裝TCP/IP協(xié)議),
你可以在你的計(jì)算機(jī)上從“我的電腦”中運(yùn)行該應(yīng)用程序的兩個(gè)實(shí)例。運(yùn)行后,將一個(gè)聊天程序作為服務(wù)器監(jiān)
聽(tīng),另一個(gè)聊天程序作為客戶與服務(wù)器連接并聊天。局域網(wǎng)中同樣可以運(yùn)行!!!
http://www.myfaq.com.cn/Dev/index.html
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -