473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

socket in C++

QQ
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!

Jul 23 '05 #1
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.
Jul 23 '05 #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));
...

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
Jul 23 '05 #3
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.
Jul 23 '05 #4
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
Jul 23 '05 #5
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.....
Jul 23 '05 #6

"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.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
4
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...
6
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: ...
4
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...
9
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...
2
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...
0
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...
3
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...
5
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...
4
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.