🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: please help me..socket file transfer

#4643
From: Remy Lebeau (TeamB)
Date: Tue, 11 Jan 2005 18:41
144 lines
4748 bytes
"billaras" <billaras1234@hotmail.com> wrote in message
news:41e3b756@newsgroups.borland.com...

> ServerSocket1->Socket->Connections[0]->SendStream(file);

The client will have no way of knowing how large the file is going to be,
thus will not know how much data to receive.  You should be sending the
file's size before the file's actual data.

Also, if you are using the ServerSocket in non-blocking mode, SendStream()
will most likely not be able to send the whole file in one send anyway.  You
need to call SendStream() in a loop until the full stream has been sent.

Also, your code is leaking the file stream on both sides.  You never close
or free the streams.

Use this code instead.

    bool __fastcall SendRaw(TCustomWinSocket *Socket, LPVOID Buffer, int
BufSize)
    {
        int sent;
        LPBYTE pBuf = (LPBYTE) Buffer;

        while( BufSize > 0 )
        {
            sent = Socket->SendBuf(pBuf, BufSize);
            if( sent == -1 )
            {
                if( WSAGetLastError() != WSAEWOULDBLOCK )
                    return false;
            }
            else
            {
                pBuf += sent;
                BufSize -= sent;
            }
        }

        return true;
    }

    bool __fastcall ReadRaw(TCustomWinSocket *Socket, LPVOID Buffer, int
BufSize)
    {
        int read;
        LPBYTE pBuf = (LPBYTE) Buffer;

        while( BufSize > 0 )
        {
            read = Socket->ReceiveBuf(pBuf, BufSize);
            if( read == -1 )
            {
                if( WSAGetLastError() != WSAEWOULDBLOCK )
                    return false;
            }
            else
            {
                pBuf += read;
                BufSize -= read;
            }
        }

        return true;
    }

    bool __fastcall SendStream(TCustomWinSocket *Socket, TStream *Stream)
    {
        int Size = (Stream->Size - Stream->Position);
        int BytesToSend = (Size - Stream->Position);

        if( !SendRaw(Socket, &BytesToSend, sizeof(int)) )
            return false;

        while( Stream->Position < Size )
        {
            if( !Socket->SendStream(file) )
                return false;
        }

        return true;
    }

    bool __fastcall ReadStream(TCustomWinSocket *Socket, TStream *Stream)
    {
        BYTE buf[1024];
        int BufSize;

        int BytesToRead = 0;
        if( !ReadRaw(Socket, &BytesToRead, sizeof(int)) )
            return false;

        while( BytesToRead > 0 )
        {
            if( BytesToRead >= 1024 )
                BufSize = 1024;
            else
                BufSize = BytesToRead;

            if( !ReadRaw(Socket, buf, BufSize) )
                return false;

            Stream->Write(buf, BufSize);
            BytesToRead -= BufSize;
        }

        return true;
    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if( OpenDialog1->Execute() )
        {
            TFileStream* file = new TFileStream(OpenDialog1->FileName,
fmOpenRead);
            try
            {
                if( !SendStream(ServerSocket1->Socket->Connections[0],
file) )
                    ShowMessage("Could not send file");
                }
            }
            __finally {
                delete file;
            }
        }
    }

    void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
    {
        TFileStream *file = new TFileStream("SomeFileNameHere.ext",
fmCreate);
        try {
            ReadStream(Socket, file);
        }
        __finally {
            delete file;
        }
    }


Gambit
Technical Details
Message-ID: <41e41d6a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news2.euro.net!216.196.110.149.MISMATCH!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
References: <41e3b756@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>
Subject: Re: please help me..socket file transfer
Date: Tue, 11 Jan 2005 10:41:28 -0800
Lines: 146
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: <41e41d6a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1105468778 68.69.17.41 (11 Jan 2005 10:39:38 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news2.euro.net!216.196.110.149.MISMATCH!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