 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kaffa Guest
|
Posted: Thu Dec 11, 2003 11:30 pm Post subject: TClientSocket-TServerSocket Send / Receive file problem.. |
|
|
Hi,
I'm trying to make a little send-receive file example with TClientSocket and
TServerSocket. Everything is fine with the 0-10KB files but with bigger
files, when it transferred i see that it's corrupted or didn't send
completely. I'm not sure about where i'm wrong.. I tried to send 7-8KB image
files and there is no problem with that sized files.. Here is my code.. Can
you tell me where is the problem ?
Kind Regards,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Client: TClientSocket;
Server: TServerSocket;
btnConnect: TButton;
btnSendRec: TButton;
btnSendFile: TButton;
OpenDialog1: TOpenDialog;
procedure btnConnectClick(Sender: TObject);
procedure ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure btnSendFileClick(Sender: TObject);
private
{ Private declarations }
public
procedure ReceiveFile(Socket: TCustomWinSocket);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnConnectClick(Sender: TObject);
begin
with Client do begin
Host := '127.0.0.1';
Port := 381;
Active := True;
end;
end;
procedure TForm1.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
Caption := 'Connected..';
end;
procedure TForm1.ServerClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
ReceiveFile(Socket);
end;
procedure TForm1.btnSendFileClick(Sender: TObject);
var
Strm: TMemoryStream;
begin
Strm := TMemoryStream.Create;
if OpenDialog1.Execute then
Strm.LoadFromFile(OpenDialog1.FileName);
Client.Socket.SendBuf(Strm.Memory^, Strm.Size);
end;
procedure TForm1.ReceiveFile(Socket: TCustomWinSocket);
var
Ptr: Pointer;
Strm: TMemoryStream;
Sz: integer;
begin
Strm := TMemoryStream.Create;
// Strm.Position := 0;
Sz := Socket.ReceiveLength;
GetMem(Ptr, Sz);
Socket.ReceiveBuf(Ptr^, Sz);
Strm.WriteBuffer(Ptr^, Sz);
FreeMem(Ptr, Sz);
Strm.SaveToFile('C:Temp.zip');
Strm.Free;
end;
end.
|
|
| Back to top |
|
 |
Paul Nicholls Guest
|
Posted: Thu Dec 11, 2003 11:40 pm Post subject: Re: TClientSocket-TServerSocket Send / Receive file problem. |
|
|
I think your problem is that files 0-10Kb in size are being received as one
packet so they are saved fine, but for larger files it is going to be
arriving as more than one packet so you have to take care of that...I'n not
sure how myself though...
you could try tacking on a header to the beginning of the file that contains
the filesize so when reading at the other end it doesn't just take the
received packet and make a complete file out of it, but adds it to the end
if it is needed...
"Kaffa" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Hi,
I'm trying to make a little send-receive file example with TClientSocket
and
TServerSocket. Everything is fine with the 0-10KB files but with bigger
files, when it transferred i see that it's corrupted or didn't send
completely. I'm not sure about where i'm wrong.. I tried to send 7-8KB
image
files and there is no problem with that sized files.. Here is my code..
Can
you tell me where is the problem ?
Kind Regards,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Client: TClientSocket;
Server: TServerSocket;
btnConnect: TButton;
btnSendRec: TButton;
btnSendFile: TButton;
OpenDialog1: TOpenDialog;
procedure btnConnectClick(Sender: TObject);
procedure ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure btnSendFileClick(Sender: TObject);
private
{ Private declarations }
public
procedure ReceiveFile(Socket: TCustomWinSocket);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnConnectClick(Sender: TObject);
begin
with Client do begin
Host := '127.0.0.1';
Port := 381;
Active := True;
end;
end;
procedure TForm1.ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
Caption := 'Connected..';
end;
procedure TForm1.ServerClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
ReceiveFile(Socket);
end;
procedure TForm1.btnSendFileClick(Sender: TObject);
var
Strm: TMemoryStream;
begin
Strm := TMemoryStream.Create;
if OpenDialog1.Execute then
Strm.LoadFromFile(OpenDialog1.FileName);
Client.Socket.SendBuf(Strm.Memory^, Strm.Size);
end;
procedure TForm1.ReceiveFile(Socket: TCustomWinSocket);
var
Ptr: Pointer;
Strm: TMemoryStream;
Sz: integer;
begin
Strm := TMemoryStream.Create;
// Strm.Position := 0;
Sz := Socket.ReceiveLength;
GetMem(Ptr, Sz);
Socket.ReceiveBuf(Ptr^, Sz);
Strm.WriteBuffer(Ptr^, Sz);
FreeMem(Ptr, Sz);
Strm.SaveToFile('C:Temp.zip');
Strm.Free;
end;
end.
|
|
|
| Back to top |
|
 |
Kaffa Guest
|
Posted: Fri Dec 12, 2003 12:07 am Post subject: Re: TClientSocket-TServerSocket Send / Receive file problem. |
|
|
Thank you for your reply.. Actually i was thinking the same, headers needed
For to make it i'll develop my own structure but i was playing on it for
to understand it's secrets.. Because i should be sure about the best packet
size that i'll use. Do you have any idea about how can i decide it ? For
example if my header size would be 200Byte and packet size is 5KB, how can i
be sure that this size is small enough for to receive in one time ?
|
|
| Back to top |
|
 |
Paul Nicholls Guest
|
Posted: Fri Dec 12, 2003 12:27 am Post subject: Re: TClientSocket-TServerSocket Send / Receive file problem. |
|
|
I think that just comes down to experimentation or being omnipotent! <G>
"Kaffa" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Thank you for your reply.. Actually i was thinking the same, headers
needed
For to make it i'll develop my own structure but i was playing on it
for
to understand it's secrets.. Because i should be sure about the best
packet
size that i'll use. Do you have any idea about how can i decide it ? For
example if my header size would be 200Byte and packet size is 5KB, how can
i
be sure that this size is small enough for to receive in one time ?
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Dec 12, 2003 1:29 am Post subject: Re: TClientSocket-TServerSocket Send / Receive file problem. |
|
|
"Kaffa" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Thank you for your reply.. Actually i was thinking the same,
headers needed
|
That is preferred, but that still doesn't address the underlying problem.
If you are using the socket in non-blocking mode, then when the OnRead event
occurs you can only read what is actually available, buffer it somewhere,
and if you need more data than you have to wait until the OnRead event is
triggered again to get the rest of the data.
| Quote: | For example if my header size would be 200Byte and packet size
is 5KB, how can i be sure that this size is small enough for to receive
in one time ?
|
You can't. You shold not rely on receving everything at one time, because
that is not how sockets work in general. You do need to anticipate both 1)
packets being split into smaller transmissions, and 2) multiple packets
being merged into a single transmission.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|