Article View: borland.public.cppbuilder.internet.socket
Article #4649Re: please help me..socket file transfer
From: Remy Lebeau (TeamB)
Date: Tue, 11 Jan 2005 21:36
Date: Tue, 11 Jan 2005 21:36
59 lines
3285 bytes
3285 bytes
"Hans Galema" <notused@notused.nl> wrote in message
news:41e44159@newsgroups.borland.com...
> There will be no leaking as deleting the stream is forbidden
> if you use ->SendStream (stream ) for a TCustomWinSocket.
<snip>
> <from help>
The help is incomplete. If the socket is not connected when entering
SendStream(), it returns immediately and does not free the stream. If the
stream is not sent in full because the socket blocked, SendStream() returns
as soon as the socket blocks and does not free the stream.
Since there is no way to test whether the stream gets freed or not (unless
you write your own stream class that produces a notification the application
can receive when the stream is freed), then it is highly recommended that
you avoid SendStream() altogether. It's behavior is inconsistent.
In fact, there is another reason to avoid SendStream() altogether - it sets
up Disconnect() to produce an EAccessViolation because SendStream() can call
Disconnect(), which always frees the provided stream, and then SendStream()
can free it a second time after calling Disconnect().
> Question: Would the stream only be deleted if SendStream succeeded ?
It is freed by SendStream() when the entire stream has been sent, or when a
socket error occurs. It is also freed by Disconnect().
> One would think yes. As otherwise a next call for the
> rest of the data in the stream was impossible.
All the more reason to avoid SendStream(). It has obscure behavior in its
memory management. It would be better to read and send the stream data
manually instead:
bool __fastcall SendStream(TCustomWinSocket *Socket, TStream *Stream)
{
BYTE buf[1024];
int BytesToSend = (Stream->Size - Stream->Position);
if( !SendRaw(Socket, &BytesToSend, sizeof(int)) )
return false;
while( BytesToSend > 0 )
{
int read = Stream->Read(buf, sizeof(buf));
if( read < 1 ) // unexpected end-of-stream
return false;
if( !SendRaw(Socket, buf, read) )
return false;
BytesToSend -= read;
}
return true;
}
Gambit
Technical Details
Message-ID:
<41e44664$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41e3b756@newsgroups.borland.com> <41e41d6a$1@newsgroups.borland.com> <41e44159@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,399582d307fda7fe X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <41e3b756@newsgroups.borland.com> <41e41d6a$1@newsgroups.borland.com> <41e44159@newsgroups.borland.com> Subject: Re: please help me..socket file transfer Date: Tue, 11 Jan 2005 13:36:17 -0800 Lines: 61 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <41e44664$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1105479268 68.69.17.41 (11 Jan 2005 13:34:28 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail