Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Recorrer un pchar (https://www.clubdelphi.com/foros/showthread.php?t=32008)

Status Quo 27-05-2006 18:02:20

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...

Status Quo 27-05-2006 18:03:34

Este es el código que recibe el fichero. Lo he puesto tal cual me dijo Seoane:) esque ya no me fío de lo que pongo yo...:o



Código Delphi [-]
 unit Unit1;
 
interface
 
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, ComCtrls, idglobal, ExtCtrls, ShellAPI;
 
type
Paquete = record
Tipo: Integer;
Tamano: Integer;
TamaTotal:Integer;
NomFich:String;
Buffer: array[0..1024] of Byte;
end;
 
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
GroupBox1: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
ProgressBar1: TProgressBar;
Timer1: TTimer;
StatusBar1: TStatusBar;
ClientSocket1: TClientSocket;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
 
private
{ Private declarations }
IncommingStream: TFileStream;
TimeTaken: integer;
public
{ Public declarations }
end;
 
var
Form1: TForm1;
 
implementation
 
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Address:=Edit3.Text;
ClientSocket1.Open;
 
end;
 
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('Client connected From '+Socket.RemoteHost)
 
end;
 
 
 
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
IncommingLen, RecievedLen: integer;
Filepath: string;
P: Paquete;
begin
 
Timer1.Enabled := True;
ClientSocket1.Socket.ReceiveBuf(P, Sizeof(P));
if p.Tipo=0 then
begin
freeAndNil(IncommingStream);
Edit1.Text:=p.NomFich;
IncommingStream := TFileStream.Create(Edit1.Text, fmCREATE or fmOPENWRITE and fmsharedenywrite);
end;
 
IncommingStream.WriteBuffer(p.Buffer,p.Tamano);
 
if p.Tamano< Sizeof(p.Buffer) then
begin
freeAndNil(IncommingStream);
memo1.Lines.Add('File '+Edit1.Text +' Recieved Successfuly');
memo1.Lines.Add('Time Taken to Recieve File ' +IntToStr(TimeTaken)+' seconds');
ClientSocket1.Socket.SendText('DONE!');
Edit1.Text := '';
Edit2.Text := '';
ProgressBar1.Position := 0;
Timer1.Enabled := False;
TimeTaken := 0;
 
if Messagedlg('Would you Like to open the recieved file?', mtConfirmation, [MbYes,MbNo],0) = MrYes then
begin
ShellExecute(Form1.Handle, 'open', pchar(Filepath),nil, nil, SW_NORMAL);
end;
 
end;
end;
 
 
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Text := '';
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(TimeTaken,1);
 
end;
 
end.





Creo que ya me va quedando poco para conseguirlo...pero aún necesito un poco más de ayudita...:o

Status Quo 27-05-2006 18:20:34

Solucionado lo del string
 
Me acabo de solucionar lo del stream:

He puesto en el record en el campo de nombre de fichero esto:

NomFich: String[50];


y ahora si coge el string en el receptor.:o pero... me da problemas en otras cosas....:(

Voy a mirármelo un poco más...

Status Quo 27-05-2006 19:07:55

Ya se cual es el problema ahora
 
:( Ahora no me pasa bien ni el tamaño del fichero ni el tipo!!!!

Se le ocurre a alguien por qué?


Gracias


La franja horaria es GMT +2. Ahora son las 23:44:30.

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