【正文】
連接可能要由服務(wù)器端來關(guān)閉。 聊天客戶端程序代碼如下:procedure (Sender: TObject)。// 。 :=false。 :=true。end。 Socket: TCustomWinSocket)。正在連接... 39。end。 Socket: TCustomWinSocket)。連接到 39。end。begin close。procedure (Sender: TObject)。*^amp。)。 end。斷開連接39。 :=true。 :=true。 。procedure (Sender: TObject。begin if key=13 then if then 。procedure (Sender: TObject)。 i : integer。39。發(fā)送消息不能為空!39。 exit。if =true then begin for i:=0 to do begin if [i] then begin chatname:= [i]。你對39。悄悄說:39。 //。 end end。amp。39。$$39。%amp。%amp。)。 end else begin (+39。+)。 end。procedure (Sender: TObject)。39。請輸入用戶名!39。 。 end。39。請輸入密碼!39。 。 end。$%$%$%39。 usename:=。end。 Socket: TCustomWinSocket)。 //chatname: string。begin temptext:=。$%^amp。,temptext)0 then //回送密碼 begin temptext:=copy(temptext,1,length(temptext)6)。登陸成功,歡迎進入本聊天室!39。 (usename)。 :=false。 :=false。 end else begin showmessage(39。)。*^amp。)。~{}()amp。,temptext)0 then // 他人登陸信息 begin temptext:=copy(temptext,1,length(temptext)9)。 進入聊天室...39。 (temptext)。 end else if pos(39。,temptext)0 then //off line begin temptext:=copy(temptext,1,length(temptext)6)。 離開聊天室...39。// showmessage(inttostr((temptext)))。 end else if pos(39。*{}39。 (mssge)。end。begin 。procedure (Sender: TObject)。end。 var Action: TCloseAction)。*^amp。)。 end。procedure (Sender: TObject)。end. 服務(wù)器sockets允許指定想提供的服務(wù)或想用來監(jiān)聽客戶請求時使用的端口。每個服務(wù)器socket控件使用一個單一的服務(wù)器windows socket Object(TServerWinSocket)去應(yīng)答在服務(wù)器端監(jiān)聽到的連接。使用服務(wù)器sockets去: 可以使用Port property來指定這個端口。能夠使用Service property直接指定端口號。property是一個好的主意,能夠減少設(shè)置端口號時的錯誤。 一旦在serversocket控件上設(shè)置好了端口號,就能夠通過在運行時通過調(diào)用Open方法來監(jiān)聽一個連接。 當沒次收到通知時,OnClientConnetc事件將發(fā)生。 一但服務(wù)器socket打開了監(jiān)聽連接,就能夠使用與服務(wù)器socket控件相關(guān)聯(lián)的服務(wù)器windows socket object來取得關(guān)于連接的信息。使用Handle屬性去存取windows通過socket連接收到的信息。 每個活動的,連接到客戶應(yīng)用是通過服務(wù)、客戶windows socket bject(TServerClientWinSocket)封裝的。這些server client windows socket object有些屬性能夠決定哪些地址和端口號給連接的兩端客戶和服務(wù)器socket使用。也可以使用Handle屬性去訪問windows從socket連接處得來的信息。 這將關(guān)閉所有打開著的,連接到客戶應(yīng)用的連接,取消任何尚未同意的連接,接著關(guān)閉監(jiān)聽連接以便服務(wù)socket控件不在接受任何新的連接。 聊天服務(wù)器端程序代碼如下:procedure (Sender: TObject。begin :=39。 counter:=0。procedure (Sender: TObject。begin :=39。+ 。procedure (Sender: TObject。begin := +39。end。 Socket: TCustomWinSocket)。 check :string。 chattext:string。 member : string。begin tmptext := 。%amp。%amp。,tmptext)0 then // 當有39。%amp。39。 signpos:= pos(39。amp。,tmptext)。 member:=copy(tmptext, signpos+2,pos(39。,tmptext)2signpos)。$$39。$$39。 for i:=0 to counter1 do begin if member=chatname[i] then begin [i].SendText(usename+39。+chattext)。 end。 endelse if pos(39。,tmptext)0 the