Как переслать файл через nonBlocking сокет

Советы » Файлы и Интернет » Как переслать файл через nonBlocking сокет

//  а форме ServerSocket1, ClientSocket1 : (Active := False, //  Host := localhost, Port := 2001, xType := xNonBlocking), //  OpenDialog1, Button1, Memo1.
procedure

TfmMain.FormCreate(Sender: TObject); begin


  ServerSocket1.Active:=true

;   ClientSocket1.Active:=true

; end

;

{--- Server ---}
procedure

TfmMain.Button1Click(Sender: TObject); var


  sStream : TMemoryStream; begin


  sStream := TMemoryStream.Create;   if

not

OpenDialog1.Execute then

    Exit;   sStream.LoadFromFile(OpenDialog1.FileName);   ServerSocket1.Socket.Connections[0].SendStreamThenDrop(sStream); end

;

{--- Client ---}
const


  MAX_BUF_SIZE = $4095;
var


  fStream: TFileStream;
{OnConnect} procedure

TfmMain.ClientSocket1Connect(Sender: TObject;   Socket: TCustomWinSocket); begin


  fStream:= TFileStream.Create('Receive.fil', fmCreate); end

;

{OnRead} procedure

TfmMain.ClientSocket1Read(Sender: TObject;   Socket: TCustomWinSocket); var


  count :Integer;
  buffer: Array

[0..MAX_BUF_SIZE] of

Char; begin


  repeat


    Socket.Lock;     count:= Socket.ReceiveBuf(buffer,SizeOf(buffer));     if

count > 0 then

      fStream.WriteBuffer(buffer,count);     Socket.Unlock;    until

(count <= 0);
   Memo1.Lines.Add(IntToStr(fStream.Size)); end

;

{OnDisconnect} procedure

TfmMain.ClientSocket1Disconnect(Sender: TObject;   Socket: TCustomWinSocket); begin


   fStream.Free; end

;
 

Другое по теме:

Категории

Статьи

Советы

Copyright © 2022 - All Rights Reserved - www.delphirus.com