socket in C++

Discussion in 'C++' started by QQ, May 18, 2005.

  1. QQ

    QQ Guest

    Hi I never used C++ before, so if the question is too basic,
    please forgive me.

    I am trying to implement a socket program in C++.
    I know in C program when I receive a packet with some structure,
    I need to parse it so as to evaluate every elements of the structure.
    for example
    struct A
    {
    int a;
    int b;
    }

    so when I get the packet I need to parse it
    memcpy(a,msg,sizeof(int));
    ....

    So I wonder whether I need to do it in C++
    or I can just claim

    class C *exC = (class C*) msg

    Thanks a lot!
    QQ, May 18, 2005
    #1
    1. Advertising

  2. QQ wrote:
    > Hi I never used C++ before, so if the question is too basic,
    > please forgive me.
    >
    > I am trying to implement a socket program in C++.
    > I know in C program when I receive a packet with some structure,
    > I need to parse it so as to evaluate every elements of the structure.
    > for example
    > struct A
    > {
    > int a;
    > int b;
    > }
    >
    > so when I get the packet I need to parse it
    > memcpy(a,msg,sizeof(int));


    Yes, but don't forget to also copy 'b'.

    > ...
    >
    > So I wonder whether I need to do it in C++
    > or I can just claim
    >
    > class C *exC = (class C*) msg


    NO

    >
    > Thanks a lot!
    >


    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
    Larry I Smith, May 18, 2005
    #2
    1. Advertising

  3. QQ wrote:
    > Hi I never used C++ before, so if the question is too basic,
    > please forgive me.
    >
    > I am trying to implement a socket program in C++.
    > I know in C program when I receive a packet with some structure,
    > I need to parse it so as to evaluate every elements of the structure.
    > for example
    > struct A
    > {
    > int a;
    > int b;
    > }
    >
    > so when I get the packet I need to parse it
    > memcpy(a,msg,sizeof(int));
    > ...
    >
    > So I wonder whether I need to do it in C++
    > or I can just claim
    >
    > class C *exC = (class C*) msg


    'class' is superfluous here. And where did C come from? You just used
    'A' a few lines back.

    Anyway, it depends entirely on the structure (pun is not intended) of
    the packet itself. Are you sure the memory pointed to by 'msg' has
    the same byte layout as an A object? If so, I'd recommend

    A a;
    memcpy(&a, msg, sizeof(A));

    to start.

    V
    Victor Bazarov, May 18, 2005
    #3
  4. Victor Bazarov wrote:
    > QQ wrote:
    >> Hi I never used C++ before, so if the question is too basic,
    >> please forgive me.
    >>
    >> I am trying to implement a socket program in C++.
    >> I know in C program when I receive a packet with some structure,
    >> I need to parse it so as to evaluate every elements of the structure.
    >> for example
    >> struct A
    >> {
    >> int a;
    >> int b;
    >> }
    >>
    >> so when I get the packet I need to parse it
    >> memcpy(a,msg,sizeof(int));
    >> ...
    >>
    >> So I wonder whether I need to do it in C++
    >> or I can just claim
    >>
    >> class C *exC = (class C*) msg

    >
    > 'class' is superfluous here. And where did C come from? You just used
    > 'A' a few lines back.
    >
    > Anyway, it depends entirely on the structure (pun is not intended) of
    > the packet itself. Are you sure the memory pointed to by 'msg' has
    > the same byte layout as an A object? If so, I'd recommend
    >
    > A a;
    > memcpy(&a, msg, sizeof(A));
    >
    > to start.
    >
    > V


    Shouldn't 'A' be declared as:

    extern "C" struct A { int a; int b; };

    to ensure that it is a plain 'C' style struct
    with no vtable, etc? Or is that the default if
    one does not declare any member methods?

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
    Larry I Smith, May 18, 2005
    #4
  5. Larry I Smith wrote:
    > Victor Bazarov wrote:
    >
    >>QQ wrote:
    >>
    >>>Hi I never used C++ before, so if the question is too basic,
    >>>please forgive me.
    >>>
    >>>I am trying to implement a socket program in C++.
    >>>I know in C program when I receive a packet with some structure,
    >>>I need to parse it so as to evaluate every elements of the structure.
    >>>for example
    >>>struct A
    >>>{
    >>> int a;
    >>> int b;
    >>>}
    >>>

    >
    >
    > Shouldn't 'A' be declared as:
    >
    > extern "C" struct A { int a; int b; };
    >
    > to ensure that it is a plain 'C' style struct
    > with no vtable, etc? Or is that the default if
    > one does not declare any member methods?


    struct A in its original form *is* a POD and *can* be used as the
    destination in 'memcpy', so there is no need to dance around with
    "C" or 'extern'. Besides, you're not really achieving that effect
    with 'extern "C"', it does not suddenly make a non-POD structure POD.

    Of course, if later somebody adds a constructor or private section
    or a virtual function to it, POD-ness of it will disappear, but in
    that case the type should be simply well-documented.

    V
    Victor Bazarov, May 18, 2005
    #5
  6. QQ

    Andre Kostur Guest

    Victor Bazarov <> wrote in news:1sKie.74746
    $01.us.to.verio.net:

    > Larry I Smith wrote:
    >> Victor Bazarov wrote:
    >>
    >>>QQ wrote:
    >>>
    >>>>Hi I never used C++ before, so if the question is too basic,
    >>>>please forgive me.
    >>>>
    >>>>I am trying to implement a socket program in C++.
    >>>>I know in C program when I receive a packet with some structure,
    >>>>I need to parse it so as to evaluate every elements of the structure.
    >>>>for example
    >>>>struct A
    >>>>{
    >>>> int a;
    >>>> int b;
    >>>>}
    >>>>

    >>
    >>
    >> Shouldn't 'A' be declared as:
    >>
    >> extern "C" struct A { int a; int b; };
    >>
    >> to ensure that it is a plain 'C' style struct
    >> with no vtable, etc? Or is that the default if
    >> one does not declare any member methods?

    >
    > struct A in its original form *is* a POD and *can* be used as the
    > destination in 'memcpy', so there is no need to dance around with
    > "C" or 'extern'. Besides, you're not really achieving that effect
    > with 'extern "C"', it does not suddenly make a non-POD structure POD.
    >
    > Of course, if later somebody adds a constructor or private section
    > or a virtual function to it, POD-ness of it will disappear, but in
    > that case the type should be simply well-documented.


    And descending into platform-specific stuff:

    - As previously noted, you'll need to ensure that both sides are using
    the same structure padding
    - Also, if you're passing around floats/doubles, they aren't necessarily
    represented the same way on each side
    - Also, you may have to worry about byte-ordering issues. (Hint: a 2-
    byte short isn't the same byte order on a sparc vs. an intel CPU....)
    - We're only talking about PODs here... if it's not a POD, then it's not
    safe to do this at all.....
    Andre Kostur, May 18, 2005
    #6
  7. "The truth of the matter is that you always know the right thing to do. The
    hard part is doing it"
    - General H. Norman Schwartzoff
    "Larry I Smith" <> wrote in message
    news:8nKie.7270$6d.4041@trnddc04...
    > Victor Bazarov wrote:
    > > QQ wrote:
    > >> Hi I never used C++ before, so if the question is too basic,
    > >> please forgive me.
    > >>
    > >> I am trying to implement a socket program in C++.
    > >> I know in C program when I receive a packet with some structure,
    > >> I need to parse it so as to evaluate every elements of the structure.
    > >> for example
    > >> struct A
    > >> {
    > >> int a;
    > >> int b;
    > >> }
    > >>
    > >> so when I get the packet I need to parse it
    > >> memcpy(a,msg,sizeof(int));
    > >> ...
    > >>
    > >> So I wonder whether I need to do it in C++
    > >> or I can just claim
    > >>
    > >> class C *exC = (class C*) msg

    > >
    > > 'class' is superfluous here. And where did C come from? You just used
    > > 'A' a few lines back.
    > >
    > > Anyway, it depends entirely on the structure (pun is not intended) of
    > > the packet itself. Are you sure the memory pointed to by 'msg' has
    > > the same byte layout as an A object? If so, I'd recommend
    > >
    > > A a;
    > > memcpy(&a, msg, sizeof(A));
    > >
    > > to start.
    > >
    > > V

    >
    > Shouldn't 'A' be declared as:
    >
    > extern "C" struct A { int a; int b; };
    >
    > to ensure that it is a plain 'C' style struct
    > with no vtable, etc? Or is that the default if
    > one does not declare any member methods?
    >

    extern "C" does not mean compile as C, it just means no name mangling.


    > Regards,
    > Larry
    >
    > --
    > Anti-spam address, change each 'X' to '.' to reply directly.
    Jesper Madsen, May 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,761
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    944
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    526
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    644
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    698
Loading...

Share This Page