🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Socket problem setting port and connecting.

#4017
From: Egi2kaZz
Date: Wed, 27 Apr 2005 08:47
182 lines
6091 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:426eaf63@newsgroups.borland.com...
>
> "Egi2kaZz" <egi2kas@gmail.com> wrote in message
> news:426e9298@newsgroups.borland.com...
>
>> Well I'm using your code posted 2005 01 11  "please help
>> me..socket file transfer "
>
> Ok.
>
>> And if don't use:
> <snip>
>> Client's program crashes with no responding :(
>
> You need to be more specific.  What does all of the client's reading code
> look like?  What is happening when it crashes?  Are there any error
> messages?  Please show code for both parties that actually compiles.
client's code:

         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 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::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
  TFileStream *file = new TFileStream("film.avi",fmCreate);
        try {
            ReadStream(Socket, file);
        }
        __finally {
            delete file;

        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ClientSocket1->Host = "192.168.0.1";
ClientSocket1->Address = "192.168.0.22";
ClientSocket1->Active = true;


}
//---------------------------------------------------------------------------

Server's code:
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 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;
    }
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::FormCreate(TObject *Sender)
{

ServerSocket1->Active = true;

}
//---------------------------------------------------------------------------

first I start server's program, then client's..I can see that file is 
transfering if I send
big file 700mb(it's takes more time:)), in client pc it's growing, BUT in 
the end program stop responding
with no errors and then I close it,file became 0,0kb size..
As I mentioned, I if I place:
try {
            ReadStream(Socket, file);
     }
__finally {
            delete file;
            ClientSocket1->Active=false; <----
             }
then program work's fine, but every time I send file, have to disactive, 
active..:(

e. 
Technical Details
Message-ID: <426f51a5@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42638bd3$1@newsgroups.borland.com> <4263a52c@newsgroups.borland.com> <426abd97@newsgroups.borland.com> <426bf467$1@newsgroups.borland.com> <426e472d$1@newsgroups.borland.com> <426e737e$1@newsgroups.borland.com> <426e9298@newsgroups.borland.com> <426eaf63@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,87ef1514521c3f8d
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Egi2kaZz" <egi2kas@gmail.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <42638bd3$1@newsgroups.borland.com> <4263a52c@newsgroups.borland.com> <426abd97@newsgroups.borland.com> <426bf467$1@newsgroups.borland.com> <426e472d$1@newsgroups.borland.com> <426e737e$1@newsgroups.borland.com> <426e9298@newsgroups.borland.com> <426eaf63@newsgroups.borland.com>
Subject: Re: Socket problem setting port and connecting.
Date: Wed, 27 Apr 2005 11:47:42 +0300
Lines: 184
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 193.219.160.135
Message-ID: <426f51a5@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1114591653 193.219.160.135 (27 Apr 2005 01:47:33 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail