Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Internet
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Colaboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #2  
Antiguo 04-07-2011
Avatar de Enan0
Enan0 Enan0 is offline
Miembro
 
Registrado: may 2004
Ubicación: Argentina
Posts: 565
Poder: 23
Enan0 Va por buen camino
Hola Maxinito, Te comento, esto es muy sencillo, Creo que en D2009 ya viene con los componentes Indy.

La teoria es la siguiente. (por lo menos asi lo Hago yo).

Primero Mando un mensaje desde el origen, con El nombre del archivo y el Tamaño.
en el Receptor tomo el nombre del archivo y el Tamaño.
Luego envio un Stream desde el Origen.
En el receptor, Recibo el Stream hasta que el tamaño sea el mismo que en el Cliente.


El codigo esta hecho para componentes Indy 10

saludos


Código Delphi [-]
procedure Cliente.SendFile(Sender: TObject);
var
  St:TFileStream;
begin
try
//SendFile es un FileListBox

  SendFile.InitialDir:=ExtractFilePath(ParamStr(0));
  SendFile.Execute;
  If SendFile.FileName<>'' then begin
    st:=TFileStream.Create(SendFile.FileName,fmopenread);
   // en esta linea envio el nombre del arhicvo y el tamaño. 
    Client.WriteLn('CMD#SNDFile#'+ExtractFileName(SendFile.FileName)+'#'+IntToStr(st.Size));
   // luego de enviar el mensaje, leo el Buffer para ver si respondio que podia recibir el archivo.
    if Client.ReadLn='RCVFile#Ready' then begin
      Try
// envio el Stream
        client.WriteStream(st,true,true,st.Size);
      except
      end;
    end else
      ShowMessage('Cant Send the File');
  end;
finally
  St.Free;
end;
end;


Código Delphi [-]

//las Variables RcvFile, FileName y Size son globales
procedure Server.ServerExecute(AThread: TIdPeerThread);
begin
if RcvFile then begin
  Try
    if st.Sizethen begin
      // Mientras que el tamaño del Stream sea menor que el Tamaño especificado sigue recibiendo como Stream
      AThread.Connection.ReadStream(st);
    end;
// Si el Tamaño de Stream es igual al que se habia informado al comienzo es que termino de Recibir y pone la clausula rcvFile en false
    if st.Size=size then begin
      RcvFile:=false;
      size:=0;
      st.free;
// Le dice al Cliente que el archivo fue ok.
      AThread.Connection.WriteLn('RESP#File received OK');
      FileName:='';
      exit;
    end;
  except
    AThread.Connection.WriteLn('RESP#File Not Received');
    RcvFile:=false;
    size:=0;
    st.free;
    if FileExists(FileName) then
      DeleteFile(FileName);
    FileName:='';
    exit;
  end;
end;
   If pos('SNDFile',s)<>0 then begin
        Delete(s,1,pos('#',s));
        Filename:=copy(s,1,pos('#',s)-1);
        delete(s,1,pos('#',s));
        size:=StrToInt(s);
        st:=TFileStream.Create(FileName,fmCreate);
        AThread.Connection.WriteLn('RCVFile#Ready');
        RcvFile:=true;
      end

Este Codigo envia esos mensajes y utiliza laClausula RcvFile ya que utilizo el mismo Socket para todo.

Otra opcion seria abrir una nueva comunicacion por un puerto distinto y se podria continuar recibiendo mensajes mientras se recibe el archivo, en el caso que se trate de un Chat.

saludos y espero que sea util.

Última edición por Enan0 fecha: 04-07-2011 a las 02:21:54.
Responder Con Cita
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Enviar y recibir archivos con TCPCliente y TCPServer victork_py Internet 4 16-07-2015 01:42:01
archivos binarios kazcote Varios 5 05-12-2005 04:02:35
Enviar y Recibir Archivos (Codigo para hacerlo) juliopag1 Internet 0 17-05-2005 21:22:44
¿Cómo puedo enviar y recibir archivos vía IRC con el componente TIdIRC? DarkByte Internet 4 26-06-2004 17:54:05
archivos exe y/o binarios fcran Varios 2 26-06-2003 14:34:21


La franja horaria es GMT +2. Ahora son las 01:44:44.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi