Ver Mensaje Individual
  #16  
Antiguo 26-05-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 26
seoane Va por buen camino
No tengo mucha experiencia con los componentes que usas (TClientSocket y TServerSocket, corrigeme si me equivoco), ni si quiera lo tengo en la paleta de componentes Pero he estado haciendo unas pruebas y parece que no funciona mal del todo:

Código Delphi [-]
unit ufrmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ScktComp;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    Cliente: TClientSocket;
    Servidor: TServerSocket;
    Destino: TFileStream;
    procedure OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  Paquete = record
    Tipo: Integer;
    Tamano: Integer;
    Buffer: array[0..1024] of Byte;
  end;

Const
  Normal = 0;
  Comienzo = 1;
  
procedure TForm1.OnClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  P: Paquete;
begin
  Socket.ReceiveBuf(P, Sizeof(P));
  if P.Tipo = Comienzo then
  begin
    FreeAndNil(Destino);
    Destino:= TFileStream.Create('d:\2.jpg', fmCreate);
  end;
  Destino.WriteBuffer(P.Buffer,P.Tamano);
  if P.Tamano < Sizeof(P.Buffer) then
  begin
    FreeAndNil(Destino);
    ShowMessage('Recibido');
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Cliente:= TClientSocket.Create(Self);
  Servidor:= TServerSocket.Create(Self);
  Cliente.ClientType:= ctBlocking;
  Servidor.ServerType:= stNonBlocking;
  Servidor.OnClientRead:= OnClientRead;
  Servidor.Port:= 3000;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Servidor.Active:= TRUE;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  P: Paquete;
begin
  Cliente.Active:= FALSE;
  Cliente.Port:= 3000;
  Cliente.Host:= '127.0.0.1';
  Cliente.Active:= TRUE;
  with TFileStream.Create('d:\1.jpg', fmOpenRead) do
  try
    P.Tipo:= Comienzo;
    repeat
      Application.ProcessMessages;
      P.Tamano:= Read(P.Buffer,Sizeof(P.Buffer));
      Cliente.Socket.SendBuf(P,sizeof(P));
      P.Tipo:= Normal;
    until P.Tamano < Sizeof(P.Buffer);
  finally
    Free;
  end;
end;

end.

He utilizado el tipo NonBlocking aunque no me gusta este tipo de sockets, mas que nada porque me resultaba mas sencillo, ¿que tipo usas tu? Me gustaria echarle un vistazo a tu codigo.
Responder Con Cita