[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Is this correct?

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
lwinkenb
Asked:
lwinkenb
  • 6
  • 3
  • 2
  • +2
3 Solutions
 
sunnycoderCommented:
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
 
Sjef BosmanGroupware ConsultantCommented:
Could also be that you use local variables, that are not initialized. Try using
    clientInt= 0;
before the memcpy.
0
 
Sjef BosmanGroupware ConsultantCommented:
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!

 
brettmjohnsonCommented:
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
 
lwinkenbAuthor Commented:
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
 
aakash_mandharCommented:
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
 
lwinkenbAuthor Commented:
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
 
lwinkenbAuthor Commented:
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
 
lwinkenbAuthor Commented:
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
 
sunnycoderCommented:
lwinkenb,

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

sunnycoder
0
 
lwinkenbAuthor Commented:
I'll PAQ it.  There's useful information here.
0
 
sunnycoderCommented:
thanks for the points, but I think you should know that it is possible to refund the question and still PAQ it.

:)
0
 
lwinkenbAuthor Commented:
I know that :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now