Recent

Author Topic: Synapse TFTP file transfer  (Read 2844 times)

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse TFTP file transfer
« Reply #90 on: May 30, 2020, 06:02:50 pm »
That's not working...
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse TFTP file transfer
« Reply #91 on: May 31, 2020, 06:39:55 am »
I tried the BeginThread, but I's always leave with 'SIGSEGV' error.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.  
  10.   ComCtrls, strutils, FileUtil, laz_synapse, blcksock, cmem,
  11.   Interfaces;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     CloseServer: TButton;
  19.     ChecksumEdit: TEdit;
  20.     FolderPath: TEdit;
  21.     FolderLocateButton: TButton;
  22.     FilePath: TEdit;
  23.     FileLocateButton: TButton;
  24.     FileSendButton: TButton;
  25.     ConnectButton: TButton;
  26.     Log: TMemo;
  27.     OpenFileDialog: TOpenDialog;
  28.     PortEdit: TEdit;
  29.     IpEdit: TEdit;
  30.     HostButton: TButton;
  31.     OpenFolderDialog: TSelectDirectoryDialog;
  32.     GetTimer: TTimer;
  33.     ProgressBar1: TProgressBar;
  34.     procedure CloseServerClick(Sender: TObject);
  35.     procedure ConnectButtonClick(Sender: TObject);
  36.     procedure ChecksumEditChange(Sender: TObject);
  37.     procedure FileLocateButtonClick(Sender: TObject);
  38.     procedure FileSendButtonClick(Sender: TObject);
  39.     procedure FolderLocateButtonClick(Sender: TObject);
  40.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  41.     procedure FormCreate(Sender: TObject);
  42.     procedure HostButtonClick(Sender: TObject);
  43.     procedure GetTimerTimer(Sender: TObject);
  44.   private
  45.  
  46.   public
  47.  
  48.   end;
  49.  
  50. var
  51.   Form1: TForm1;
  52.  
  53. implementation
  54.  
  55. {$R *.lfm}
  56.  
  57. { TForm1 }
  58.  
  59. var MainServer:TTCPBlockSocket;
  60.     Connection:TTCPBlockSocket;
  61.     AcceptState:byte=1;
  62.     LogMessage:string;
  63.     connected:boolean;
  64.     filename:string;
  65.     mode:string;
  66.     checksum:string;
  67.     MainStream:TFileStream;
  68.     filesize:integer;
  69.     streambuffer:string;
  70.     threadcount:integer;
  71.  
  72. function ThreadRun(p:pointer):PtrInt;
  73. begin
  74.   Connection.RecvStreamRaw(MainStream, 60000);
  75. end;
  76.  
  77. procedure Logging;
  78. begin
  79.   Form1.Log.Lines.Add(LogMessage);
  80.   LogMessage:='';
  81. end;
  82.  
  83. procedure TForm1.FolderLocateButtonClick(Sender: TObject);
  84. begin
  85.   if OpenFolderDialog.execute then FolderPath.Text:=OpenFolderDialog.FileName;
  86.   SetCurrentDir(OpenFolderDialog.FileName);
  87. end;
  88.  
  89. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  90. begin
  91.   Halt;
  92. end;
  93.  
  94. procedure TForm1.FormCreate(Sender: TObject);
  95. begin
  96.   mode:='string';
  97. end;
  98.  
  99. procedure TForm1.FileLocateButtonClick(Sender: TObject);
  100.  var f:file;
  101.      s:string;
  102. begin
  103.   if OpenFileDialog.execute then FilePath.Text:=OpenFileDialog.FileName;
  104.   filename:=ExtractDelimited(WordCount(OpenFileDialog.FileName,[DirectorySeparator]),
  105.   OpenFileDialog.FileName,[DirectorySeparator]);
  106.  
  107.   Filesize:=FileUtil.FileSize(OpenFileDialog.FileName);
  108.  
  109.   showmessage(filename+'  '+IntToStr(filesize));
  110. end;
  111.  
  112. procedure TForm1.FileSendButtonClick(Sender: TObject);
  113. begin
  114.   mode:='filesend';
  115.   FilePath.Enabled:=false;
  116.   if connected=true then
  117.   if Connection.CanWrite(10) then
  118.    if checksum<>'' then
  119.      Connection.SendString('>StartSendFile'+':'+filename+':'+IntToStr(filesize)
  120.       +':'+checksum+#13#10)
  121.    else
  122.      Connection.SendString('>StartSendFile'+':'+filename+':'+IntToStr(filesize)+#13#10);
  123.  
  124.   MainStream:=TFileStream.Create(OpenFileDialog.FileName,fmOpenRead);
  125.   Connection.SendStreamRaw(MainStream);
  126.   MainStream.Free;
  127.   mode:='string';
  128.   FilePath.Enabled:=true;
  129. end;
  130.  
  131. procedure TForm1.ConnectButtonClick(Sender: TObject);
  132. begin
  133.   connection:=TTCPBlockSocket.Create;
  134.   Connection.CreateSocket;
  135.   Connection.Connect(IpEdit.Text,PortEdit.Text);
  136.   if Connection.LastError=0 then begin
  137.     LogMessage:=('Successfully connected to ('+Connection.GetRemoteSinIP+')');
  138.     connected:=true;
  139.   end;
  140.  
  141.   Logging;
  142. end;
  143.  
  144. procedure TForm1.CloseServerClick(Sender: TObject);
  145. begin
  146.   AcceptState:=0;
  147.   FreeAndNil(MainServer);
  148.   LogMessage:='Server Stopped.';
  149.   Logging;
  150. end;
  151.  
  152. procedure TForm1.ChecksumEditChange(Sender: TObject);
  153. begin
  154.   checksum:=ChecksumEdit.Text;
  155. end;
  156.  
  157. procedure TForm1.HostButtonClick(Sender: TObject);
  158. begin
  159.   MainServer:=TTCPBlockSocket.Create;
  160.   MainServer.CreateSocket;
  161.   MainServer.Bind(IpEdit.Text,PortEdit.Text);
  162.   MainServer.Listen;
  163.   LogMessage:='Server Run.';
  164.   Logging;
  165.   AcceptState:=1;
  166.  
  167.   while AcceptState=1 do begin
  168.          Application.ProcessMessages;
  169.          if Assigned(MainServer) then
  170.          if MainServer.CanRead(100) then begin
  171.            Connection:=TTCPBlockSocket.Create;
  172.            Connection.CreateSocket;
  173.            Connection.Socket:=MainServer.Accept;
  174.            LogMessage:=('Connection accepted from ('+Connection.GetRemoteSinIP+')');
  175.            if LogMessage<>'' then Logging;
  176.            connected:=true;
  177.          end;
  178.     end;
  179. end;
  180.  
  181. procedure TForm1.GetTimerTimer(Sender: TObject);
  182.  var messagein:String;
  183.      infile_name:string;
  184.      infile_size:string;
  185.      infile_checksum:string;
  186.      f:file;
  187.      n:integer;
  188.      finished:LongInt;
  189. begin
  190.   //TEXT MODE
  191.  if connected then begin
  192.  
  193.     if mode='string' then begin
  194.        messagein:=Connection.RecvString(10);
  195.     if (messagein<>'') and (messagein[1]<>'>') then begin
  196.      LogMessage:=messagein;
  197.       Logging;
  198.     end
  199.     else
  200.       if (messagein<>'') and (messagein[1]='>') then begin
  201.           mode:='fileaccept';
  202.  
  203.           infile_name:=ExtractDelimited(2,messagein,[':']);
  204.           infile_size:=ExtractDelimited(3,messagein,[':']);
  205.           infile_checksum:=ExtractDelimited(4,messagein,[':']);
  206.  
  207.           LogMessage:='Accepting file: '+infile_name+' Size: '+
  208.            infile_size+' Checksum: '+infile_checksum;
  209.           logging;
  210.         end;
  211.   end;
  212.   //FILE MODE
  213.   if mode='fileaccept' then begin
  214.    FolderPath.Enabled:=false;
  215.  
  216.    MainStream:=TFileStream.Create(infile_name,fmCreate);
  217.    if threadcount=0 then begin
  218.      if BeginThread(@ThreadRun,pointer(threadcount))=1 then
  219.      EndThread(1);
  220.      inc(threadcount,1);
  221.    end;
  222.  
  223.    while FileUtil.FileSize(OpenFileDialog.FileName)>=StrToInt(infile_size) do begin
  224.      MainStream.Free;
  225.      mode:='string';
  226.      FolderPath.Enabled:=true;
  227.  
  228.    end;
  229.  
  230.   end;
  231.  
  232.  
  233.  end;
  234. end;
  235.  
  236. end.
  237.  
  238.  
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse TFTP file transfer
« Reply #92 on: May 31, 2020, 08:51:40 am »
How can I transport my global variables between units?
(I can't use it:)

Code: Pascal  [Select][+][-]
  1. procedere someName(variables);
  2.  

because I wrote the thread.execute in other unit and the socket is't here, but it's necessary.
« Last Edit: May 31, 2020, 08:53:56 am by Jake012345 »
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4157
Re: Synapse TFTP file transfer
« Reply #93 on: May 31, 2020, 05:26:29 pm »
How can I transport my global variables between units?
(I can't use it:)

Code: Pascal  [Select][+][-]
  1. procedere someName(variables);
  2.  

because I wrote the thread.execute in other unit and the socket is't here, but it's necessary.
You shouldn't use global variables.
I gather you wanted to make multiple connections to different clients at the same time. Then you'll better make the variables locall to the class (and not global).

Further you can pass a pointer of a socket to a thread with the create function. But... You shouldn't use the socket without building in some thread-safety (sockets are not thread safe and you can't use then from several threads at the same time).

Read about synchronize for that.

 

TinyPortal © 2005-2018