Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sending Custom Class by packet (to different program)  (Read 5087 times)

0 Members and 1 Guest are viewing this topic.

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Sending Custom Class by packet (to different program)
« on: December 07, 2021, 02:40:08 pm »
Hi, thank you Laurent and others for the tutorial on overriding the << >> operators to be able to send classes.

Before I jump into this, I wondered how this would work on the other end. That is, the receiving program.

The receiving server would also be made myself, and so I can include the .cpp and .h of the class I will be sending over. But I just wanted to check this is necessary. I.e the class has to be defined on the other end / the other exe.

Essentially I will be sending character data for a handful of characters with their statistics.

I know how to override the << >> but just wanted a small bit of help with how I get it at the receiving end. a line or two of code,

i.e the tutorial states
Packet >> Bob;
How then do I make this an object on the receiving side etc. like class instance = packet >> bob (to give a bad psuedo code example).

You can probably see I am quite new to this, but enjoying it. Any comments on what I'm asking, or what you think I'm trying to achieve will be helpful.

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sending Custom Class by packet (to different program)
« Reply #1 on: December 07, 2021, 03:08:58 pm »
As it stands, the best I've come upwith so far is creating the object again on the server side and instantiating with the data sent over in the packet.

   character john; // create new object on other exe.
   if (packet >> name >> age)
   {
      john.name = name;
                john.age = age;
   }

I wondered if there'd be another way such as a constructor, i.e

character john(packet) or
character john (packet >> name>>age>>location>>inventory);

etc.

Once again all help is appreciated, and / or other suggestions

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Sending Custom Class by packet (to different program)
« Reply #2 on: December 07, 2021, 08:09:21 pm »
I don't really get it.

You overload >> and << for your Character class.
Inside these 2 functions is where you want to put/retrieve the data to/from the packet.
Then you simply send your Character john with
packet << john
and retrieve it with
Character john; // creates john
packet >> john; // fills john with data from the packet

You don't extract every data of your character individually (well you can, but what's the point) you directly extract a character and the overloaded >> operator function takes care of extracting the individual data.

RyanPaul87

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sending Custom Class by packet (to different program)
« Reply #3 on: December 07, 2021, 10:12:06 pm »
Thanks for this, it was really helpful. For some reason I was doing this,

if (packet >> name >> age)
   {
      john.name = name;
      john.age = age;
   }

when it wasn't necessary. (the overloading already did this).

You've really helped me.

I wondered if you have any goto links for networking tutorials, ideally videos. There's not too many on youtube, although there's decent info about.

 

anything