Ver Mensaje Individual
  #21  
Antiguo 27-05-2006
Status Quo Status Quo is offline
Miembro
 
Registrado: may 2006
Posts: 23
Reputación: 0
Status Quo Va por buen camino
Red face no puedo extraer el nombre del fichero

He puesto un solo socket para envíar los paquetes y he puesto el record así:


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


No se si habrá algún problema con los strings pero el caso esque no me llega el nombre en el código de recepción...

Pongo todo el código con el que estoy trabajando para hacer las pruebas(luego lo pasaré al que estoy haciendo yo...)

No se lo que son los tipos NonBlocking yo utilizo de la paleta de componentes--> internet el componente clientsockect y serversocket...

Seoane, te pongo el código este de pruebas...Cuando lo terminte(por favor que sea pronto) te paso el programa total...(como tu me digas que te viene mejor...)


Este es el código que envía el fichero:


Código Delphi [-]
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
Paquete = record
Tipo: Integer;
Tamano: Integer;
TamaTotal:Integer;
NomFich:String;
Buffer: array[0..1024] of Byte;
end;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Edit3: TEdit;
Label3: TLabel;
OpenDialog1: TOpenDialog;
ServerSocket1: TServerSocket;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket2Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
Streamsize: TFileStream;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation


procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Open;

end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Connected to ' +Socket.RemoteHost); // Shows the address of the server you connect to
end;

procedure TForm1.ClientSocket2Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Sending File ' +Edit1.Text +' .........')
end;

procedure TForm1.Button2Click(Sender: TObject);
var
stream:TStream;


P: Paquete;

begin
if ServerSocket1.Active = True then

begin
OpenDialog1.Filter := 'All Files (*.*)'; // you can add more choices by adding | and followed by description and (*.extension)
OpenDialog1.FilterIndex := 1; // Here you follow which index number from above you want
if OpenDialog1.Execute then
begin
Edit1.Text := ExtractFileName(OpenDialog1.FileName); // To send as filename after

Streamsize := TFileStream.Create(OpenDialog1.FileName, fmopenread); // Stream created just to Calculate size
Edit2.Text := inttostr(Streamsize.Size);

Streamsize.Position := 0;
Streamsize.Free;
sleep(2000);


stream:=TFileStream.Create(OpenDialog1.FileName, fmopenRead);

P.Tipo:= 0;
repeat
Application.ProcessMessages;
P.Tamano:= stream.Read(P.Buffer,SizeOf(P.Buffer));
P.NomFich:=Edit1.Text;
P.TamaTotal:=Streamsize.Size;
ServerSocket1.Socket.Connections[0].SendBuf(P,sizeof(P));  //he quitado el -1
//leidos:=stream.Read(P.Buffer,SizeOf(P.Buffer));
P.Tipo:= 1;
until(P.Tamano=0);
stream.Free;

end;
end
else
MessageDlg('Error: You are not connected', mtError, [MbOK],0); // Error Check above code won't work until the socket is connected
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Text := '';
end;


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var IncommingText, StrippedData, CommandName: string;
begin
IncommingText := socket.ReceiveText; // Put all incoming data into this string
StrippedData := copy(IncommingText,6,length(IncommingText) ); // letters 6 to end is the actual data being sent
CommandName := copy(IncommingText,0,5); // letters 0 to 5 is the fixed letter space for the command name
if CommandName = 'DONE!' then // Expected command Done! note : This is case sensitive so match has to be the same case
begin
Memo1.Lines.Add('File ' +Edit1.Text +' ' +Edit2.Text +' was Recieved Successfully');

Edit1.Text := '';
Edit2.Text :='';
end;
end;

end.

Perdonar, no se porqué se me ha ido toda la indentación...

Última edición por Status Quo fecha: 28-05-2006 a las 16:49:16.
Responder Con Cita