Code: Detail

Delphi - (Portuguese)

Enviando foto por socket

Envia uma foto por socket utilizando TServerSocket e TClienteSocket

Last update: 11/12/2013
Delphi       
 
N/A
N/A
N/A
N/A
 

//----
//Código do aplicativo que ficará no server
//----
      if not Receiving then 
      begin 
        if pos(#0,s) > 0 then 
          stSize:=strtoint(copy(s,1,pos(#0,s)-1)) 
        else 
          exit; 
        Receiving:=True; 
        delete(s,1,pos(#0,s)); 
      end; 
   try 
    Stream.Write(s[1],length(s)); 
    while ( socket.receivelength > 0 ) or (Stream.Size <stSize> 0 then 
        begin 
          iLen := Socket.ReceiveLength; 
          GetMem(Bfr, iLen); 
          try 
            Socket.ReceiveBuf(Bfr^, iLen); 
            Stream.Write(Bfr^, iLen); 
          finally 
           FreeMem(Bfr); 
          end; 
        end; 
    end; 

    //Se a imagem está certa 
    if Stream.Size = stSize then 
    begin 
      Stream.Position:=0; 
      Receiving:=False; 
      stSize := 0; 
      JPEG:=TJPEGImage.Create; 
      JPEG.LoadFromStream(Stream); 
      Stream.Clear; 
      imagemPessoa.Picture.Assign(JPEG);      
      JPEG.Free; 
    end; 
    except 
      Stream.Clear; 
    end; 
   end 


//----
//Codigo do aplicativo que rodará no cliente
//----

    if JPEG <> nil then 
    begin 
      ms := TMemoryStream.Create; 
      try 
       JPEG.SaveToStream(ms); 
       ms.Position := 0; 
       sleep(1); 
       Socket.SendText(IntToStr(ms.Size) + #0); 
       Socket.SendStream(ms); 
       except 
         ms.Free; 
       end; 
    end; 

Source: http://www.activedelphi.com.br/forum/viewtopic.php?t=47655&postdays=0&postorder=asc&start=120&sid=c5
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.