Ver Mensaje Individual
  #7  
Antiguo 18-10-2013
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Reputación: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

Parece claro que esto:

Código Delphi [-]
sent := Client.Socket.SendBuf( Pointer(s)^, Length(s) - idx);

... no tiene mucho sentido. Funciona, porque, las pruebas las estoy haciendo "en local", y, en realidad TODO el texto se envía a la vez, porque, en realidad es lo que estamos haciendo ahí... es decir, la primera llamada tendría sentido, pero, una segunda llamada no lo tendría. El error no se ve (si no es a simple vista, pero, a mí se me escapa) si no se sabe que, en verdad, dicha instrucción se ejecuta una sola vez y no, como se espera, varias veces. Las sucesivas llamadas no tendrían sentido, pues estaríamos enviando de nuevo TODO el texto... peor aún, todo el texto "menos idx"...

Es decir, que el código de Remy:

Código Delphi [-]
sent := Client.Socket.SendBuf(PChar(s)+idx, Length(s)-idx);

... tiene otro sentido. No compila, seguramente, porque está escrito sin probarlo, pero, el sentido está claro: enviar a cada llamada únicamente el texto restante, o sea, el que no se haya enviado ya. Así que, tratando de remediar dicho asunto, he escrito algo como esto:

Código Delphi [-]
procedure TMainForm.SendFromClientButtonClick(Sender: TObject);

  // Just to get the text to send from our "a.txt" file
  function GetTextToSend() : string;
  var
    t : TStrings;
  begin
    t := TStringList.Create();
    try
      t.LoadFromFile( 'a.txt' );
      result := t.Text;
    finally
      t.Free();
    end;
  end;

var
  s : string;
  bytesSent : integer;
  totalBytesSent : integer;
  textToSendLen : integer;
  textToSend : TStringStream;
  buffer : array [0..1023] of byte;
begin
  s := GetTextToSend() + #6#7; 

  textToSend := TStringStream.Create( EmptyStr );
  try
    textToSendLen := Length( s );
    textToSend.WriteString( s );
    textToSend.Position := 0;

    totalBytesSent := 0;
    repeat
      FillChar( buffer, 1024, 0 );
      textToSend.Read( buffer, 1024 );
      Application.ProcessMessages();

      bytesSent := Client.Socket.SendBuf( buffer, SizeOf( buffer ) );

      if bytesSent = -1 then // Wait for writing
      begin
        Sleep( 50 );
        Continue;
      end;

      if bytesSent = 0 then // Disconnected
      begin
        Exit;
      end;

      Inc( totalBytesSent, bytesSent );

    until textToSendLen <= totalBytesSent;

  finally
    textToSend.Free();
  end;
end;

Y, aunque ahora sí se supone que estamos haciendo las cosas mejor, lo cierto es que tampoco me termina de convencer, puesto que en realidad estamos forzando el envío de 1024 bytes cada vez, sin contar acaso conque a veces podría enviarse más... y a veces también menos...

En definitiva no soy capaz de dar con la tecla. No digamos ya implementar el asunto utilizando "streams", a cuyo inicio añadamos el tamaño del mismo. Lo sigo intentando, pero, no hay manera.
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita