Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Thread não recebendo SendText na parte Servidor.

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Duvida Thread não recebendo SendText na parte Servidor.

    Boa tarde pessoal,

    estou com um problema aqui pra fazer o upload de arquivo via Sockets e me encontro no seguinte cenário:

    Aplicação Cliente (a que recebe o arquivo enviado) =>

    Código:
    var 
    Tamanho: Integer; 
    Recebendo: Boolean; 
    nome: string; 
    
    procedure TForm1.ClientSocket1Read(Sender: TObject; 
      Socket: TCustomWinSocket); 
    var 
    dados: string; 
    
    begin 
    dados := Socket.ReceiveText; 
    
    //============== EDIT1 RECEBE O CAMINHO ONDE SERÁ SALVO O ARQUIVO UPADO =============== 
    if Copy(dados,1,6)='<up>' then 
    Begin 
    Delete(dados, 1, 6) ; 
    button1.Click; 
    Edit1.Text:= dados; 
    nome:= dados; 
    exit; 
    end; 
    //===================================================================================== 
    
    
    //===================================================== 
    //                                                     | 
    // BOTÃO ENVIANDO STRING '<Arquivo>' PARA A THREAD   | 
    // QUE FAZ ENVIO DO STREAM COM O ARQUIVO DE UPLOAD     | 
    //                                                     | 
    //===================================================== 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    ClientSocket3.Socket.SendText('<Arquivo>'); 
    end; 
    //====================================================== 
    
    //====================================================== 
    //                                                      | 
    // AQUI É ONDE O ARQUIVO UPADO PELO SERVIDOR É RECEBIDO | 
    // E SALVO NO DIRETORIO DEFINIDO NO EDIT1               | 
    //                                                      | 
    //====================================================== 
    procedure TForm1.ClientSocket3Read(Sender: TObject; 
      Socket: TCustomWinSocket); 
    
    var 
     Stream: TMemoryStream; 
      s: string; 
    begin 
      s := Socket.ReceiveText; 
    
      if Recebendo = False then begin 
        if Pos(#0, s)>0 then begin 
    
          Tamanho := StrToInt(Copy(s, 1, Pos(#0, s)-1)); 
          Delete(s, 1, Pos(#0, s)); 
          Stream := TMemoryStream.Create; 
          Pb1.Max := tamanho; 
          Recebendo := true; 
        end; 
      end; 
      try 
        Stream.Write(s[1], Length(s)); 
        PB1.Position := Stream.Size; 
    
        if Stream.Size >= Tamanho then begin 
          Stream.Position := 0; 
    
         if nome = edit1.Text then begin 
            Stream.SaveToFile(nome); 
            Application.MessageBox('Salvo com sucesso!', 'Aviso', 64); 
         end; 
          Recebendo := false; 
          Stream.Free; 
    end; 
      except 
        Stream.Free; 
        Recebendo := false; 
      end; 
    //============================================================= 
    
    end;
    Aplicação Servidor (a que envia o arquivo para o cliente) =>

    Código:
    type 
      TServ_Sock3 = class(TThread) 
        private 
          procedure Execute; override; 
        public 
          Socket: TCustomWinSocket; 
    end; 
    
    var 
    caminho: string; 
    
    procedure TForm1.EnviarArquivo1Click(Sender: TObject); 
    begin 
    
     If not Assigned(LV.Selected) then exit; 
    if LV.Selected.Checked = false then exit; 
    
      if OpenDialog1.Execute then begin 
      caminho:= opendialog1.FileName;  // C:\arquivo a ser enviado.txt 
      
      SS.Active:= true; 
      SS2.Socket.Connections[Form1.LV.ItemIndex].SendText('<up>'+caminho); 
    
    end; 
    end; 
    //====================================================== 
    
    //====================================================== 
    //                                                      | 
    //    AQUI QUE É O GRANDE PROBLEMA, O ClientSocket3     | 
    //    na parte Cliente deste projeto, envia a string    | 
    //    '<Arquivo>', mas essa execução de               | 
    //    Thread não recebe e não consegue enviar o arquivo | 
    //                                                      | 
    //===================================================== 
    procedure TServ_Sock3.Execute; 
    var 
      s: String; 
      Stream: TMemoryStream; 
    begin 
      inherited; 
    
      while not Terminated and Socket.Connected do begin 
        if Socket.ReceiveLength>0 then begin 
          s := Socket.ReceiveText; 
    
          if Pos('<Arquivo>', s)>0 then begin 
    
              Stream := TMemoryStream.Create; 
    
               Stream.LoadFromFile(caminho); 
    
              Stream.Position := 0; 
              Socket.SendText(IntToStr(Stream.Size)+#0); 
              Sleep(1000); 
              Socket.SendStream(Stream); 
            end; 
          end; 
    
        end; 
        Sleep(10); 
    end; 
    //======================================================== 
    
    //=========================================================== 
    //                                                           | 
    //  PARTE ONDE CHAMA A EXECUÇÃO DA TRHEAD QUE FAZ O UPLOAD   | 
    //   DE ARQUIVO                                              | 
    //                                                           | 
    //=========================================================== 
    procedure TForm1.SSAccept(Sender: TObject; Socket: TCustomWinSocket); 
    var 
      Conex: TServ_Sock3; 
    begin 
      Conex := TServ_Sock3.Create(true); 
      Conex.Socket := Socket; 
      Conex.FreeOnTerminate := True; 
      Conex.Resume; 
    
    end; 
    //============================================================
    Não faço a menor ideia do que possa estar acontecendo, o único problema é o texto enviado a partir do ClientSocket3, presente na parte cliente da aplicação, que não é recebido pela Thread (na parte servidor) que envia o arquivo para o ClientSocket3Read, presente na parte cliente.

    Alguém por favor, poderia me ajudar?

    Agradeço a todos.
    Similar Threads

  • Font Size
    #2
    esta um pouco confuso seu Código, esta usando 3 sockts, nao consegui entender direito, usa 2 sockts só, 1 cliente e o outro server, vai ficar até mais fácil de trabalhar, reparei que vc esta usando 2 cliente, um para upload e outro para comando, nao tem necessidade, apenas um cliente e um server faz tudo! abraço..
    __________________________________________________ ____
    "A imaginação é mais importante que o conhecimeno"

    Comment

    X
    Working...
    X