Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 03-01-2006
miguel_e miguel_e is offline
Miembro
 
Registrado: ene 2006
Posts: 86
Poder: 19
miguel_e Va por buen camino
Duda con TStream.Readbuffer

Hola a todos si alguien pudiera ayudarme con una duda que tengo me resolveria un gran problema, el asunto es que estoy haciendo un broadcast con los componentes UDP de los Indy, hasta ahi todo bien, el problema es cuando intento leer los datos, es un hecho que llega el mensaje a todas las maquinas de la red pues se dispara el evento de que llego un mensaje, bien pero el problema es que el parametro del evento que contiene los datos es un TStream, este tiene un metodo que se llama readbuffer el problema es que no se como utilizar este metodo y no puedo obtener la informacion???'

espero haya podido quedar un poco claro si no entienen por favor diganmelo a ver si puedo intentar aclarar un poco esto disculpen la molestia

salu2
miguel_e
Responder Con Cita
  #2  
Antiguo 03-01-2006
Northern Northern is offline
Miembro
 
Registrado: ene 2006
Posts: 211
Poder: 19
Northern Va por buen camino
TStream.WriteBuffer/ReadBuffer

Esta respuesta la da Peter Below de los TeamB a un usuario....no se si está permitido poner este tipo de mensajes
pero como soy nuevo lo pongo y si alguien tiene algo que decir que lo diga, que lo borre y todos contentos y felices


In article <3f7fbccf@newsgroups.borland.com>, Bob McKinnon wrote:
> I would like to write out an array of long strings along with other data to
> a file. I tried to create a file records like I normally do but that did
> not work since I was using a string. I wonder if streams is the way to go?
> To make it simple, if I have a data object that contains two integers and
> an array of strings it easy to stream that out to file? Sample code would
> be great.
>
> Test = class (Tobject)
> x, x1 : Integer;
> StList : array [1..100] of string;
> End;

Since strings are variable-length data you need to write the length of a
string in addition to the characters to the stream, otherwise you cannot read
them back conveniently.

Lets add two methods to your Test class:
Código Delphi [-]
procedure Test.SaveToStream( aStream: TStream );
var
  len, i: Integer;
begin
  Assert( Assigned( aStream ));
  // Write the two integer fields first
  aStream.WriteBuffer( x, sizeof(x));
  aStream.WriteBuffer( x1, sizeof(x1));
  // Now write the string array. Since its size is fixed we
  // do not need to write the number of elements first.
  for i:= Low( StList ) to High( StList ) do begin
    len := Length( StList[i] );
    aStream.WriteBuffer( len, sizeof(len));
    if len > 0 then
      aStream.WriteBuffer( StList[i][1], len );
  end;
end;

procedure Test.LoadFromStream( aStream: TStream );
var
  len, i: Integer;
begin
  Assert( Assigned( aStream ));
  // Read the two integer fields first
  aStream.ReadBuffer( x, sizeof(x));
  aStream.ReadBuffer( x1, sizeof(x1));
  // Now read the string array. Since its size is fixed we
  // do not need to read the number of elements first.
  for i:= Low( StList ) to High( StList ) do begin
    aStream.ReadBuffer( len, sizeof(len));
    SetLength( StList[i], len );
    if len > 0 then
      aStream.ReadBuffer( StList[i][1], len );
  end;
end;
Since ReadBuffer and WriteBuffer use untyped parameters handing them an
Ansistring (which is a pointer type) is a bit unintuitive: you have to pass
the first character of the string to get the correct address across.

--
Peter Below (TeamB)

Última edición por vtdeleon fecha: 03-01-2006 a las 20:25:00.
Responder Con Cita
  #3  
Antiguo 04-01-2006
miguel_e miguel_e is offline
Miembro
 
Registrado: ene 2006
Posts: 86
Poder: 19
miguel_e Va por buen camino
Hola, muchas gracias por tu repuesta, ya le di solucion al problema así

salu2
miguel_e

Código:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
var
  StrLen: Integer;
  PBuf: PChar;
  cadena : string;
begin
  StrLen := AData.Size;
  GetMem(PBuf, StrLen);
  try
    AData.ReadBuffer(PBuf^, StrLen);
    SetString(cadena, PBuf, StrLen);
  finally
    FreeMem(PBuf, StrLen);
  end;
  ShowMessage(cadena);
end;
Responder Con Cita
Respuesta



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


La franja horaria es GMT +2. Ahora son las 18:10:28.


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