WinSocket網絡通信之 TClientSocket & TServerSocket

TClientSocket 類管理 TCP/IP客戶端 Socket連接, 繼承於 TObject->TPersistent->TComponent->TAbstractSocket->TCustomSocket.

屬性:
1. property ClientType: TClientType;
客戶端Socket是否讀寫Socket同步連接時信息.
type TClientType = (ctNonBlocking, ctBlocking);
非阻塞方式: 當客戶端連接服務器時, 會自動保持連接;
阻塞方式: 需要多線程方式響應客戶端;

2. property Socket: TClientWinSocket;
客戶端Socket連接節點

3. property Active: Boolean;
是否打開連接

4. property Address: string;
Server端 IP 地址. Socket打開時不可設置

5. property Host: string;
Server端名稱, 類似: http://www.wSite.Com . Socket打開時不可設置

6. property Port: Integer;
Sever端的 Port ID.打開時不可設置

7. property Service: string;
Socket連接的服務類型名稱. 如:ftp, http, finger, and time. Socket打開時不可設置
一般地, Port 已經即指示出服務類型.

方法:
1. procedure Close;
關閉 Socket 連接. 設置 Active=False.

2. procedure Open;
打開 Socket 連接. 設置 Active=True;


一個通過Socket響應服務器的線程執行示例:

procedure TMyClientThread.Execute;
var
  TheStream: TWinSocketStream;
  buffer: string;
begin
  // 創建一個 TWinSocketStream 進行Socket 讀寫
  TheStream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
  try
    // 獲取並處理命令指定連接或者線程終止
    while (not Terminated) and (ClientSocket1.Active) do
    begin
      try
        GetNextRequest(buffer); { GetNextRequest must be a thread-safe method }
        // 寫向服務器的請求
        TheStream.Write(buffer, Length(buffer) + 1);
        // 讀從服務器發來的數據的處理
        ...
      except
        if not(ExceptObject is EAbort) then
          Synchronize(HandleThreadException); // 必須使用異常處理
      end;
    end;
  finally
   TheStream.Free;
  end;
end;

procedure TMyServerThread.ClientExecute;
var
  Stream : TWinSocketStream;
  Buffer : array[0 .. 9] of Char;
begin
  // 當連接Ok時
  while (not Terminated) and ClientSocket.Connected do
  begin
    try
      Stream := TWinSocketStream.Create(ClientSocket, 60000);
      try
        FillChar(Buffer, 10, 0); // 初始化 buffer
        // 給定客戶端 60秒時間開始寫
        if Stream.WaitForData(60000) then 
        begin
          if Stream.Read(Buffer, 10) = 0 then // 如果60s時間內沒有讀取到數據
            ClientSocket.Close;               // 關閉連接
          //  其它處理
          ...
        end
        else
          ClientSocket.Close; // 客戶端沒有開始
      finally
        Stream.Free;
      end;
    except
      HandleException;
    end;
  end;
end;


 

 TServerSocket 類管理 TCP/IP客戶端 Socket連接, 繼承於 TObject->TPersistent->TComponent->TAbstractSocket->TCustomSocket->TCustomServerSocket.

屬性:
1. property Socket: TServerWinSocket;
監聽連接端的 TServerWinSocket 對象;
示例:

procedure TChatForm.Memo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_Return then  // 回車
    if IsServer then // 是 Server or Client , 則發送最近Memo中輸入的一行字符串.
      ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
    else
      ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.Count - 1]);
end;


2. property ServerType: TServerType;
Sever端連接方式 : 非阻塞方式, 線程阻塞方式
type TServerType = (stNonBlocking, stThreadBlocking);

3. property ThreadCacheSize: Integer;
可以重用於客戶端連接的線程最大數 , 默認值為 10. 用於線程阻塞方式.

4. property Active: Boolean;
打開連接

5. property Port: Integer;
連接端口

6. property Service: string;
服務類型

方法:
1. procedure Close;
關閉連接

2. procedure Open;
打開連接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值