Solved

Is this correct?

Posted on 2004-08-08
13
270 Views
Last Modified: 2010-04-15
char buff[8];
int serverInt = 20;

memcpy(buff+4,&serverInt,4);
send(sd,buff,8);

...

int clientInt;
char buff[8];

recv(sd,buff,8);
memcpy(&clientInt,buff+4,4);
// clientInt does clientInt equal 20 now?

Both the client and server are running intel P4 processors.  I am trying something similar to the above, but the results are wrong (clientInt is equal to something like 8 million)
0
Comment
Question by:lwinkenb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 11745831
could be communication error ... are you checking return values ?

It is quite possible that you forgot the htonl socket number and because of that send and/or recv are failing ... the value dispalyed is junk value contained in uninitialized variable
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11745844
Could also be that you use local variables, that are not initialized. Try using
    clientInt= 0;
before the memcpy.
0
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 75 total points
ID: 11745856
Probably nonsense what I just wrote (and sizeof(int) is 4). You could print the buff's before and after the memcpy calls?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11747226
If the sender and receiver  have the same word-size and endian-ness,
it should work.  I agree with sunnycoder, check the returns from send
and recv, make sure that 8 bytes was sent and received.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11747868
Ok, here is my actuall code after it's been recv()

memcpy(&m_dataStruct.dataSize,buff+4,4);

I used the debugger to look at the memory of buff+4 right before the call+, and it had:
0D 00 00 00

This is 13 which is correct.  After the memcpy call though, m_dataStruct.dataSize holds the value:
6C 65 7F 31

How did that happen?
0
 

Assisted Solution

by:aakash_mandhar
aakash_mandhar earned 75 total points
ID: 11749837
Ok... There are two things you can do...

1)  send(sd,(char *)&servInt,sizeof(int));  // eliminates use of hardcoded size value and makes code platform independent
and


recv(sd,(char *)&clientInt,sizeof(int));

I believe this shoud solve your problem.... But if it does not solve your problem goto step 2...

2) Not the best way to code but is a workaround...
    Instead of memcpy convert the number to a string at server and back to number at the client side....
itoa and atoi can help... But use this only as the last resort because it wastes a lot of space but if nothing works u have no choice....


Regards
Aakash


0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11749881
Ok, after more investigation, I have this.

buff is equal to 0x00125F08.  That memory location is shown below.

00125F08  32 30 31 00 0D 00 00  
00125F0F  00 74 65 6D 70 20 74  
00125F16  69 74 6C 65 7F 31 7F

After running
memcpy(&m_dataStruct.dataSize,buff+4,4);
m_dataStruct.dataSize is equal to 0x317F656C

Looking at the memory above, shouldnt buff+4 be equal to 0x0000000D ??
It appears that m_dataStruct.dataSize is actually getting set to the value at buff+16 if I'm not mistaken.

Need some help here :)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11750055
Just to add, if I change it to:

memcpy(&m_dataStruct.dataSize,buff+1,4);

Then m_dataStruct.dataSize gets filled with the correct value.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11750066
Ack, I figured out the problem =/

As it turns out, I posted some bad information here.  The buffer was actually declared as void*, not as char.  

This means that doing something like buff+1 does not add 1 byte to the buffer address as I had originally thought.  It adds 1 unit of whatever size the buffer is declared as.  I learn something new every day :)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11750164
lwinkenb,

would you like me to refund this question for you ? Or do you intend to PAQ it  ?

sunnycoder
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11750318
I'll PAQ it.  There's useful information here.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11750325
thanks for the points, but I think you should know that it is possible to refund the question and still PAQ it.

:)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11750463
I know that :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question