使用TServerSocket和TClientSocet控件时候,可以方便的使用客户端向服务器发送数据信息,如何使用TServerSocket向指定的用户发送信息啊!特指的用户,不能向所有的用户发送然后判断,为的是防止截包!!
  希望大家指点!

解决方案 »

  1.   

    以下的代码基于ServerSocket和ClientSocket都是NonBlocking的:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ScktComp;type
      TForm1 = class(TForm)
        ServerSocket1: TServerSocket;
        ClientSocket1: TClientSocket;
        procedure ServerSocket1ClientConnect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ClientSocket1Connect(Sender: TObject;
          Socket: TCustomWinSocket);
        procedure ServerSocket1ClientRead(Sender: TObject;
          Socket: TCustomWinSocket);
      private
        { Private declarations }
        FSocketList: TList;
        FClientList: TStringList;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      FSocketList.Add(Socket);
      FClientList.Add('');
    end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      Socket.SendText('唯一性标识')
    end;procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    var
      iIndex: Integer;
    begin
      //在这里要判断是否发送的是标识信息
      iIndex := FSocketList.IndexOf(Socket);
      if (iIndex <> -1) and (FClientList[iIndex] = '') then
        FClientList[iIndex] := Socket.ReceiveText;
    end;end.然后要发送消息给特定Client时:
    procedure SendToClient(const ClientID: String);
    var
      iIndex: Integer;
    begin
      iIndex := FClientList.IndexOf(ClientID);
      if iIndex <> -1 then
        ServerSocket1.Socket.Connections[iIndex].Sendxxx(xxx...);
    end;如果时Blocking的就要在线程里面处理,Blocking我也弄得不很清楚。