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! 6 1389
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.
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 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 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 <v.********@comAcast.net> wrote in news:1sKie.74746
$N********@newsread1.mlpsca01.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.....
"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" <la***********@verizon.net> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: simon place |
last post by:
Spent some very frustrating hours recoding to find a way of closing a server
socket, i'd not thought it would be any problem,
however, after complete failure and as a last resort, i looked at the...
|
by: DreJoh |
last post by:
I've read many articles on the subject and the majority of them give the same
solution that's in article 821625 on the MSDN website. I'm using the
following code and when a the client disconnects...
|
by: roger beniot |
last post by:
I have a program that launches multiple threads with a ThreadStart
method like the following (using System.Net.Sockets.Socket for UDP
packet transfers to a server):
ThreadStart pseudo code:
...
|
by: Chris Tanger |
last post by:
Context:
C#
System.Net.Sockets
Socket created with constructor prarmeters Internetwork, Stream and TCP
everything else is left at the default parameters and options except linger
may be changed...
|
by: Macca |
last post by:
Hi,
I have a synchronous socket server which my app uses to read data from
clients.
To test this I have a simulated client that sends 100 byte packets.
I have set up the socket server so...
|
by: Macca |
last post by:
My app has an asynchronous socket server. It will have 20 clients connected
to the server. Each client sends data every 500 millisecondsThe Connections
once established will not be closed unless...
|
by: Macca |
last post by:
Hi,
I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.
Each connection has a...
|
by: BuddyWork |
last post by:
Hello,
Could someone please explain why the Socket.Send is slow to send to the same
process it sending from. Eg. Process1 calls Socket.Send which sends to the
same IP address and port, the...
|
by: darthghandi |
last post by:
I've created a class to listen to all interfaces and do a
BeginAccept(). Once it gets a connection, it passes the connected
socket off and stores it in a List. Next, it continues to listen for...
|
by: O.B. |
last post by:
I have a socket configured as TCP and running as a listener. When I
close socket, it doesn't always free up the port immediately. Even
when no connections have been made to it. So when I open...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |