troubleshooting Question

How to send a stream through a socket?

Avatar of Zannah
Zannah asked on
C++
7 Comments1 Solution1189 ViewsLast Modified:
What I am trying to do is send the contents of a listbox from the server listbox to the client, and have it put that content into the client listbox.

Using Borland Builder 6.0


Server looks like this:

void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
        int Client;
        int Total = ServerSocket1->Socket->ActiveConnections;

        for (Client = 0; Client < Total; Client++)
             if (ServerSocket1->Socket->Connections[Client] == Socket)
                  break;

        String Reply = Socket->ReceiveText();
        String User = Reply.SubString( 4, Reply.Length() );
       
        if ( Reply.SubString(1, 3) == "ID " )
        {
            {
            ListBox1->Items->Add(User);

            RichEdit2->SelAttributes->Color = clGreen;
            RichEdit2->Lines->Add(User + " joined the chat");
            }

            for (int x = 0; x < Total; x++)
                 if (x != Client)
                {
//// Here is where I need input!! ////
                    TMemoryStream* Users = new TMemoryStream();
                    ListBox1->Items->SaveToStream(Users);
                    Users->Position = 0;
                    ServerSocket1->Socket->Connections[x]->SendStream(Users);
                    delete Users;
                }
        }
       
        else
        {
            RichEdit2->Lines->Add(Reply);

            for (int x = 0; x < Total; x++)
                 if (x != Client)
                    ServerSocket1->Socket->Connections[x]->SendText(Reply);
        }
}


Client looks like this:

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
String Reply = Socket->ReceiveText();
String User = Reply.SubString( 4, Reply.Length() );


        //if ( Reply.SubString(1, 3) == "ID " )
        //{
//// Here is where I need input!! ////
            ListBox1->Items->LoadFromStream(Users);
            RichEdit2->SelAttributes->Color = clGreen;
            RichEdit2->Lines->Add(User + " joined the chat");
        //}
        //else
        //{
        //    RichEdit2->Lines->Add(Reply);
        //}


Now, disregard all the "ID" stuff, from what i've read it seems I have to use SendBuf before SendStream, aswell as ReceiveBuf before using LoadStream to get this to work.

Am I thinking totally wrong here?

I am looking for a really basic and easy to understand code example of how this would compile and work, just add the lines in the code if you know the answer.

Thanks!
ASKER CERTIFIED SOLUTION
Mindphaser

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros