Recent

Author Topic: Synapse send file  (Read 3256 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Synapse send file
« on: May 22, 2015, 03:15:15 pm »
hello guys, I need to understand how to send files with my class. It works well for sending strings. But I must also send and receive files. Who tells me how to do?

thanks


Code: [Select]
unit Unit_Client_TCP;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, blcksock;

type

   { TTCPClient }

   TTCPClient=class
       private
              sock: TTCPBlockSocket;
              buffer: String;
              ErrorStr: string;
       public
         constructor Create(Host: string; Port: string);
         function GetErrorStr(): string;
         function SendString(str: string): boolean;
         function GetString(): string;
         destructor Free();
   end;

implementation

{ TTCPClient }

constructor TTCPClient.Create(Host: string; Port: string);
begin
  Self.Buffer:='';
  sock := TTCPBlockSocket.Create;
  sock.Connect(Host, Port);
  // Was there an error?
  if sock.LastError <> 0 then
  begin
       Self.ErrorStr:=sock.LastErrorDesc;
  end;
end;

function TTCPClient.GetErrorStr: string;
begin
     result:=Self.ErrorStr;
end;

function TTCPClient.SendString(str: string): boolean;
begin
     sock.SendString(str + #13#10);
end;

function TTCPClient.GetString: string;
begin
     buffer := sock.RecvPacket(2000);
     result:=buffer;
end;

destructor TTCPClient.Free;
begin
     Self.sock.CloseSocket;
     Self.sock.Free;
end;

end.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Synapse send file
« Reply #1 on: May 22, 2015, 03:16:32 pm »
And also if anyone knows how to encrypt the connection safely.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

balazsszekely

  • Guest
Re: Synapse send file
« Reply #2 on: May 22, 2015, 05:29:27 pm »
Open blcksock.pas. You will find the following methods:
   -SendStream
   -SendStreamRaw
   -SendStreamIndy
   -RecvStream
   -RecvStreamRaw
   -RecvStreamIndy
  Load the file to stream then send it through socket. If you want a progressbar send the files by chunk.

 

TinyPortal © 2005-2018