日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

(死亡歷程)Delphi7 自帶的控件 IdTCPClient,IdTCPServer 客戶端發(fā)送給服務(wù)端,服務(wù)端轉(zhuǎn)發(fā)給其它客戶端

 容心居 2021-05-02

客戶端

  private

    { Private declarations }

    FThread: TThread;

  public

    { Public declarations }

    iFileHandle:integer;

    i: integer;

    host,port,qm:string;

    sFile:TFileStream;

    F : Textfile;

    str,nbzy_bh:string;

  end;

  TReadThread = class(TThread)

  private

    FClient: TIdTCPClient;

    FMessage,nbzy_qm: string;

    FForm: TFormLjf2;

    procedure UpdateForm;

  protected

    procedure Execute; override;

  public

    constructor Create(AForm: TFormLjf2; qm: string; AClient: TIdTCPClient);

  end;

var

  FormLjf2: TFormLjf2;

implementation

uses ljf999;

{$R *.dfm}

procedure TFormLjf2.FormShow(Sender: TObject);

var

  i,j:Integer;

begin

  //得到要發(fā)送的客戶端IP

  i:=pos(')',Self.Caption); //查找分隔符

  host:=copy(Self.Caption,i+1,Length(Self.Caption)-i);

  //連接到服務(wù)器

  IdTCPClient2.Host:=FormLjf999.host;

  IdTCPClient2.Port:=StrToInt(FormLjf999.port);

  //得到用戶編號(hào)

  str:=Self.Caption;

  i:=pos('(',str);

  j:=pos(')',str);

  nbzy_bh:=copy(str,i+1,j-(i+1));

  qm:=copy(str,1,i-1);;

  Memo1.SetFocus;

  //這里是線程,2個(gè)參數(shù),一個(gè)是窗口,一個(gè)是接收信息的IdTCPClient 2017-1-5

  FThread := TReadThread.Create(Self, qm, IdTCPClient2);

end;

procedure TFormLjf2.SpeedButton1Click(Sender: TObject);

var

  Tid: DWord;

begin

  if Memo1.Text='' then

  begin

    ShowMessage('請(qǐng)輸入內(nèi)容');

    Memo1.SetFocus;

    Exit;

  end;

  if not IdTCPClient2.Connected then

  begin

    ShowMessage('連接未建立。');

    exit;

  end;

  IdTCPClient2.WriteLn(host+'-'+Memo1.Text); 

  Memo2.Lines.Add('我 '+DateTimeToStr(Now)+ #13#10 +' '+Memo1.Text);

  PostMessage(Memo2.Handle, WM_VSCROLL, SB_BOTTOM, 0);

  Memo1.Text:='';

  //Backspace鍵

  keybd_event(8,0,0,0);

  keybd_event(8,0,KEYEVENTF_KEYUP,0);

end;

procedure TFormLjf2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);

begin

  OutputDebugString('notify thead to quit.');

  FThread.Terminate;

  OutputDebugString('wait thead finish.');

  //FThread.WaitFor;

  OutputDebugString(PChar('thread finished.'));

  if IdTCPClient2.Connected then

  begin

    IdTCPClient2.Disconnect;

  end;

end;

procedure TFormLjf2.FormClose(Sender: TObject; var Action: TCloseAction);

begin

  //Action := caFree;

  Self.Free;

end;

{ TReadThread }

constructor TReadThread.Create(AForm: TFormLjf2; qm: string; AClient: TIdTCPClient);

begin

  FClient := AClient;

  FForm := AForm;

  nbzy_qm := qm;

  FreeOnTerminate := True;

  inherited Create(False);

end;

procedure TReadThread.Execute;

begin

  inherited;

  while not Terminated do

  begin

    if Terminated then

    begin

      Break;

    end;

    if not FClient.Connected then

    begin

      try

        FClient.Connect(100);

      except

      end;

      if not FClient.Connected then

      begin

        Sleep(100);

        continue;

      end;

    end;

    FMessage := FClient.ReadLn(#$D, 1);

    if Length(FMessage) > 0 then

      Synchronize(UpdateForm);

  end;

  if FClient.Connected then

    FClient.Disconnect;

end;

procedure TReadThread.UpdateForm;

begin

  FForm.Memo2.Lines.Add(nbzy_qm+' '+DateTimeToStr(Now));

  FForm.Memo2.Lines.Add(' '+FMessage);

end;

end.

服務(wù)端接收并轉(zhuǎn)發(fā)

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

var

  rbyte:array[0..4096] of byte;

  sFile:TFileStream;

  cmd,FileSize:integer;

  str,FileName:string;

  iFileHandle:integer;

  iFileLen,cnt:integer;

  buf:array[0..4096] of byte;

  ip,fileqm,sendStr:string;

  i,j:integer;

  sm:TStringStream;

  msize:Integer;

begin

  //while AThread.Connection.Connected do  //注意這里

  with AThread.Connection do

  begin

      Try

        str:=AThread.Connection.ReadLn;   //接收文件大小及文件名

        if POS('|',str)>0 then

        begin

          //

        end

        else

        begin

            i:=pos('-',str); //查找分隔符

            ip:=copy(str,1,i-1); //提取文件名

            sendStr:=copy(str,i+1,Length(str)-i);

            IdTCPServer1.Threads.UnlockList;

            with IdTCPServer1.Threads.LockList do

            begin

              try

                for i:=0 to Count - 1 do

                begin

                  try

                    AThread := Items[i];

                    if AThread.Connection.Socket.Binding.PeerIP = ip then

                    begin

                      AThread.Connection.WriteLn(sendStr);//這里是服務(wù)端發(fā)送給客戶端的

                    end;

                  except

                    AThread.Stop;

                  end;

                end;

              finally

                IdTCPServer1.Threads.UnlockList;

              end;

            end;

        end;

      Finally

        //Athread.Connection.Disconnect;

        //AThread.Connection.WriteBuffer('錯(cuò)誤',SizeOf('錯(cuò)誤'),True);

      end;

  end;

end;

procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);//客戶端退出觸發(fā) IdTCPServer1Disconnect

begin

  Memo1.Lines.Add(AThread.Connection.Socket.Binding.PeerIP+' 已退出');

  try

    IdTCPServer1.Threads.LockList.Remove(Athread);

  finally

    IdTCPServer1.Threads.UnlockList();

  end;

  //AThread.Connection.Disconnect;

end;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);

var

  strSql,qm,bh,str:string;

  i:Integer;

begin

  Memo1.Lines.Add(qm+' '+AThread.Connection.Socket.Binding.PeerIP+' 已連接');

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

  IdTCPServer1.Active:=False;

   Application.Terminate;

end;

procedure TForm1.IdTCPServer1Exception(AThread: TIdPeerThread;

  AException: Exception);

begin

  Memo1.Lines.Add('客戶端'+inttostr(athread.handle)+'異常斷開(kāi)');

  del(AThread);

  if AThread.Connection.Connected then

  AThread.Connection.Disconnect;

end;

————————————————

版權(quán)聲明:本文為CSDN博主「luojianfeng」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/luojianfeng/article/details/54097231

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多