Jeg håbede der var et eller andet komponent der kunne det jeg har brug for...
Der kommer kontakt, overførslen sker, men indholdet er tomt.
Client koden er:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
tBuf = array[1..2048] of Char;
tbufobj = object
buf : tbuf;
fil : string[10];
pos : integer;
end;
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
ClientSocket1.Active:=true;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var b : tbufobj;
begin
b.fil:=extractfilename(OpenDialog1.FileName);
b.pos:=-1;
socket.SendBuf(b,sizeof(b));
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var st : string;
b : tbufobj;
f : file;
NumRead,i : integer;
begin
st := socket.ReceiveText;
if st='slut' then
else
begin
i:=strtoint(st);
memo1.Lines.add(inttostr(i));
assignfile(f,opendialog1.filename);
reset(f,1);
if i*sizeof(b.buf)>=filesize(f) then
begin
b.pos:=-2;
socket.SendBuf(b,sizeof(b));
closefile(f);
exit;
end;
seek(f,i*sizeof(b.buf));
b.pos:=i;
// if i=100 then exit;
BlockRead(F, b.Buf, SizeOf(b.Buf), NumRead);
b.fil:=extractfilename(OpenDialog1.FileName);
socket.SendBuf(b,sizeof(b));
closefile(f);
end;
end;
end.
Server kode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
tBuf = array[1..2048] of Char;
tbufobj = object
buf : tbuf;
fil : string[10];
pos : integer;
end;
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Active:=true;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var b: tbufobj;
count : integer;
fromf : file;
NumRead, NumWritten: Integer;
begin
try
socket.ReceiveBuf(b,sizeof(b));
except end;
memo1.Lines.add(b.fil);
memo1.Lines.add(inttostr(b.pos));
if b.pos=-1 then
begin
memo1.Lines.add(extractfilepath(paramstr(0))+'fil\\'+b.fil);
if FileExists(extractfilepath(paramstr(0))+'fil\\'+b.fil) then
begin
assignfile(fromf,extractfilepath(paramstr(0))+'fil\\'+b.fil);
reset(fromf,1);
try
socket.SendText(inttostr(round(filesize(fromf)/sizeof(b.buf))));
except end;
memo1.Lines.add('EKSISTERER '+inttostr(round(filesize(fromf)/sizeof(b.buf))));
closefile(fromf);
end else
begin
assignfile(fromf,extractfilepath(paramstr(0))+'fil\\'+b.fil);
rewrite(fromf,1);
closefile(fromf);
try
socket.SendText('0');
except end;
memo1.Lines.add('ny fil');
end;
end
else if b.pos=-2 then
begin
movefile(pchar(extractfilepath(paramstr(0))+'fil\\'+b.fil),pchar(extractfilepath(paramstr(0))+'f\\'+b.fil));
try
socket.SendText('slut');
except end;
end else
begin
AssignFile(FromF, extractfilepath(paramstr(0))+'fil\\'+b.fil);
Reset(FromF, 1);
seek(FromF,sizeof(b.buf)*b.pos);
numread := sizeof(b.buf);
BlockWrite(fromF, b.buf, NumRead, NumWritten);
try
socket.SendText(inttostr(b.pos+1));
except end;
CloseFile(FromF);
memo1.Lines.add('Skriv til fil');
end;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.add('connect');
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.add('disconnect');
end;
end.