๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #4488

Re: TWinSocketStream->Write problem

#4488
From: Remy Lebeau (TeamB)
Date: Thu, 27 Jan 2005 19:19
75 lines
3238 bytes
"Eyyub Volkan ๏ฟฝektimur" <evcektimur@prokon.com.tr> wrote in message
news:41f8bc22@newsgroups.borland.com...

>
WritenBytes+=SocketStream->Write((WriteMemory+(int)WritenBytes),DataSize-Wri
> tenBytes);

You are not taking into account that Write() can return a negative value,
thus your WrittenBytes value can become out-of-sync.  You should do the
following instead:

    DWORD TotalBytes = 0;

    while( TotalBytes != DataSize ...)
    {
        WritenBytes = SocketStream->Write(&WriteMemory[TotalBytes],
DataSize-TotalBytes);
        if( WrittenBytes < 0 )
        {
            // socket error
            break;
        }
        if( WritenBytes == 0 )
        {
            // wait a moment and try again
            Sleep(250);
        }
        else
        {
            TotalBytes += WritenBytes;
            // no need to sleep here
        }
    }

Alternatively:

    char *WriteMemory = ...;

    while( DataSize > 0 ...)
    {
        WritenBytes = SocketStream->Write(WriteMemory, DataSize);
        if( WrittenBytes < 0 )
        {
            // socket error
            break;
        }
        if( WritenBytes == 0 )
        {
            // wait a moment and try again
            Sleep(250);
        }
        else
        {
            DataSize -= WritenBytes;
            WriteMemory += WritenBytes;
            // no need to sleep here
        }
    }

> when Write method returns smaller than DataSize (especially 0) , and the
> package re-writen , client side reads this package twice , supposing that
> server side writen this package twice

That is possible.  You did not specify what your TWinSocketStream timeouts
are set to.  If they are too small, Write() could be returning earlier than
the data is actually sent, but the data could still be sent afterwords since
Write() does not cancel the send before exiting.  TWinSocketStream uses
overlapped I/O on the socket.  When Write() times out, it returns 0
immediately, but the overlapped operation is still in progress.  If you set
your timeouts higher, there is less chance of Write() exiting prematurely
before the data is actually sent.


Gambit
Technical Details
Message-ID: <41f93e25$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!demon!news-lond.gip.net!news-raspail.gip.net!news.gsl.net!gip.net!grolier!194.74.65.66.MISMATCH!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41f8bc22@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,4dfffaaf64791146
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <41f8bc22@newsgroups.borland.com>
Subject: Re: TWinSocketStream->Write problem
Date: Thu, 27 Jan 2005 11:19:35 -0800
Lines: 77
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: <41f93e25$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1106853413 68.69.17.41 (27 Jan 2005 11:16:53 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!demon!news-lond.gip.net!news-raspail.gip.net!news.gsl.net!gip.net!grolier!194.74.65.66.MISMATCH!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail