🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by Jesper Fri, 09 Dec 2005 19:46
TCP
#2996
Author: Jesper
Date: Fri, 09 Dec 2005 19:46
25 lines
1942 bytes
Hi! I just want to say that i'm a newbie when it commes to programming. 
Having said that, I have some questions.

I'm writing my own FileTransfer program, using the "IdTCPClient" and 
"IdTCPServer" components in BCB6. I know how to create a connection to the 
'other side', but I can't send any files.

Here's the code:

IdTCPClient1->BoundIP = Edit1->Text;                           //The IP of 
the other computer
IdTCPClient1->Port = Edit2->Text.ToIntDef(0);               //The Port is 
set to 3000, but can be changed
IdTCPClient1->Connect();
IdTCPClient1->WriteFile("test\\test.txt");
if(IdTCPClient1->Connected() == true)
Label3->Caption ="Connected to the other side";

My problem is IdTCPClient1->WriteFile.
If you look at the HELP text, all you get is: '' function WriteFile(AFile: 
String; const AEnableTransferFile: boolean): cardinal; virtual; ''

I have no idea how to continue.

/J
Re: TCP
#2998
Author: Remy Lebeau (TeamB)
Date: Fri, 09 Dec 2005 22:22
65 lines
2876 bytes
"Jesper" <jesper@safholm.com> wrote in message
news:4399dee8@newsgroups.borland.com...

> IdTCPClient1->BoundIP = Edit1->Text; //The IP of the other computer

That is not correct.  The BoundIP property is used to specify the IP of the
*local* machine that the socket will bind to.  Use the Host property instead
to specify the IP of the *remote* machine that the server is running on.

> IdTCPClient1->WriteFile("test\\test.txt");

I would not suggest using WriteFile() in this situation.  It does not
provide any means of telling the other party how much data is actually being
sent.  I would suggest using WriteStream() instead, ie:

    TFileStream *FStream = new TFileStream("test\\test.txt", fmOpenRead |
fmShareDenyWrite);
    try
    {
        IdTCPClient.WriteStream(FStream, true, true, 0);
    }
    __finally {
        delete FStream;
    }

Then you can use ReadStream() on the TIdTCPServer to receive the data.
ReadStream() can read the stream size ahead of time in order to optimize the
reading:

    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
    {
        AnsiString FileName = "some file name here";
        TFileStream *FStream = new TFileStream(FileName, fmCreate);
        try
        {
            try
            {
                AThread->Connection->ReadStream(FStream, -1, false);
            }
            __finally
            {
                delete FStream;
            }
        }
        catch(const Exception &)
        {
            DeleteFile(FileName);
        }
    }

> if(IdTCPClient1->Connected() == true)

Why are you checking that after calling WriteFile() instead of after calling
Connect()?

In any case, you don't need to check it at all anyway since Connect() will
throw an exception if it fails.

> My problem is IdTCPClient1->WriteFile.

What is your actual problem with it?  Please be more specific.


Gambit
Re: TCP
#2999
Author: Jesper
Date: Sat, 10 Dec 2005 13:02
81 lines
3254 bytes
Thanx for all the help!
I will try it anc come back and ask more questions later! :-)
And I wont SPAM this time, promise :-)

Cheerz
/J

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> skrev i meddelandet 
news:439a03e0$1@newsgroups.borland.com...
>
> "Jesper" <jesper@safholm.com> wrote in message
> news:4399dee8@newsgroups.borland.com...
>
>> IdTCPClient1->BoundIP = Edit1->Text; //The IP of the other computer
>
> That is not correct.  The BoundIP property is used to specify the IP of 
> the
> *local* machine that the socket will bind to.  Use the Host property 
> instead
> to specify the IP of the *remote* machine that the server is running on.
>
>> IdTCPClient1->WriteFile("test\\test.txt");
>
> I would not suggest using WriteFile() in this situation.  It does not
> provide any means of telling the other party how much data is actually 
> being
> sent.  I would suggest using WriteStream() instead, ie:
>
>    TFileStream *FStream = new TFileStream("test\\test.txt", fmOpenRead |
> fmShareDenyWrite);
>    try
>    {
>        IdTCPClient.WriteStream(FStream, true, true, 0);
>    }
>    __finally {
>        delete FStream;
>    }
>
> Then you can use ReadStream() on the TIdTCPServer to receive the data.
> ReadStream() can read the stream size ahead of time in order to optimize 
> the
> reading:
>
>    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
>    {
>        AnsiString FileName = "some file name here";
>        TFileStream *FStream = new TFileStream(FileName, fmCreate);
>        try
>        {
>            try
>            {
>                AThread->Connection->ReadStream(FStream, -1, false);
>            }
>            __finally
>            {
>                delete FStream;
>            }
>        }
>        catch(const Exception &)
>        {
>            DeleteFile(FileName);
>        }
>    }
>
>> if(IdTCPClient1->Connected() == true)
>
> Why are you checking that after calling WriteFile() instead of after 
> calling
> Connect()?
>
> In any case, you don't need to check it at all anyway since Connect() will
> throw an exception if it fails.
>
>> My problem is IdTCPClient1->WriteFile.
>
> What is your actual problem with it?  Please be more specific.
>
>
> Gambit
>
> 
Re: TCP
#3001
Author: Jesper
Date: Sat, 10 Dec 2005 18:12
129 lines
4496 bytes
Hmm, I can't make it work. I get the error: "Cannot assign requested 
address".
Here's my code:

void __fastcall TForm1::IdTCPClient1Connected(TObject *Sender)
{
IdTCPClient1->Host = Edit1->Text;
IdTCPClient1->Port = Edit2->Text.ToIntDef(0);
TFileStream *FStream = new TFileStream("test\\test.txt", fmOpenRead |
fmShareDenyWrite);
    try
    {
        IdTCPClient1->WriteStream(FStream, true, true);
    }
    __finally {
        delete FStream;
    }


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

void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
{
AnsiString FileName = "bhabhabh.txt";
        TFileStream *FStream = new TFileStream(FileName, fmCreate);
        try
        {
            try
            {
                AThread->Connection->ReadStream(FStream, -1, false);
            }
            __finally
            {
                delete FStream;
            }
        }
        catch(const Exception &)
        {
            DeleteFile(FileName);
        }
    }

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
IdTCPClient1->Connect();
}







"Remy Lebeau (TeamB)" <no.spam@no.spam.com> skrev i meddelandet 
news:439a03e0$1@newsgroups.borland.com...
>
> "Jesper" <jesper@safholm.com> wrote in message
> news:4399dee8@newsgroups.borland.com...
>
>> IdTCPClient1->BoundIP = Edit1->Text; //The IP of the other computer
>
> That is not correct.  The BoundIP property is used to specify the IP of 
> the
> *local* machine that the socket will bind to.  Use the Host property 
> instead
> to specify the IP of the *remote* machine that the server is running on.
>
>> IdTCPClient1->WriteFile("test\\test.txt");
>
> I would not suggest using WriteFile() in this situation.  It does not
> provide any means of telling the other party how much data is actually 
> being
> sent.  I would suggest using WriteStream() instead, ie:
>
>    TFileStream *FStream = new TFileStream("test\\test.txt", fmOpenRead |
> fmShareDenyWrite);
>    try
>    {
>        IdTCPClient.WriteStream(FStream, true, true, 0);
>    }
>    __finally {
>        delete FStream;
>    }
>
> Then you can use ReadStream() on the TIdTCPServer to receive the data.
> ReadStream() can read the stream size ahead of time in order to optimize 
> the
> reading:
>
>    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
>    {
>        AnsiString FileName = "some file name here";
>        TFileStream *FStream = new TFileStream(FileName, fmCreate);
>        try
>        {
>            try
>            {
>                AThread->Connection->ReadStream(FStream, -1, false);
>            }
>            __finally
>            {
>                delete FStream;
>            }
>        }
>        catch(const Exception &)
>        {
>            DeleteFile(FileName);
>        }
>    }
>
>> if(IdTCPClient1->Connected() == true)
>
> Why are you checking that after calling WriteFile() instead of after 
> calling
> Connect()?
>
> In any case, you don't need to check it at all anyway since Connect() will
> throw an exception if it fails.
>
>> My problem is IdTCPClient1->WriteFile.
>
> What is your actual problem with it?  Please be more specific.
>
>
> Gambit
>
> 
Re: TCP
#3003
Author: Hans Galema
Date: Mon, 12 Dec 2005 08:22
10 lines
1327 bytes
Jesper wrote:
> Hmm, I can't make it work. I get the error: "Cannot assign requested 
> address".
> Here's my code:

But you did not tell which line caused the error.

Please trim your quotes. You twice reposted old stuff.

Hans.
Re: TCP
#3005
Author: Remy Lebeau (TeamB)
Date: Mon, 12 Dec 2005 17:42
67 lines
2969 bytes
"Jesper" <jesper@safholm.com> wrote in message
news:439b1a46$1@newsgroups.borland.com...

> Hmm, I can't make it work. I get the error:
> "Cannot assign requested address".

That should only happen if you are still using the BoundIP property, and
specifying an invalid local IP address, or are specifying a
BoundIP/BoundPort combination that is already in use elsewhere.

> void __fastcall TForm1::IdTCPClient1Connected(TObject *Sender)
> {
> IdTCPClient1->Host = Edit1->Text;
> IdTCPClient1->Port = Edit2->Text.ToIntDef(0);

Why are you changing the Host and Port in the OnConnected event?  You should
not be doing that.  Do that in the button OnClick event that is calling
Connect().

Also, why are you sending the file in the OnConnected event as well?  Indy
is not an event-driven library.  You should send the file in the same code
that is calling Connect(), ie:

    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
    {
        AnsiString FileName = "bhabhabh.txt";
        TFileStream *FStream = new TFileStream(FileName, fmCreate);
        try
        {
            try
            {
                AThread->Connection->ReadStream(FStream, -1, false);
            }
            __finally
            {
                delete FStream;
            }
        }
        catch(const Exception &)
        {
            DeleteFile(FileName);
        }
    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if( IdTCPClient1->Connected() )
            IdTCPClient1->Disconnect();

        IdTCPClient1->Host = Edit1->Text;
        IdTCPClient1->Port = Edit2->Text.ToIntDef(0);
        IdTCPClient1->Connect();
        TFileStream *FStream = new TFileStream("test\\test.txt", fmOpenRead
| fmShareDenyWrite);
        try
        {
            IdTCPClient1->WriteStream(FStream, true, true, 0);
        }
        __finally {
            delete FStream;
        }
        //...
    }


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads