• C

Is this correct?

char buff[8];
int serverInt = 20;



int clientInt;
char buff[8];

// 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)
Who is Participating?

Improve company productivity with a Business Account.Sign Up

sunnycoderConnect With a Mentor Commented:
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
Sjef BosmanGroupware ConsultantCommented:
Could also be that you use local variables, that are not initialized. Try using
    clientInt= 0;
before the memcpy.
Sjef BosmanConnect With a Mentor Groupware ConsultantCommented:
Probably nonsense what I just wrote (and sizeof(int) is 4). You could print the buff's before and after the memcpy calls?
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

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.
lwinkenbAuthor Commented:
Ok, here is my actuall code after it's been recv()


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?
aakash_mandharConnect With a Mentor Commented:
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

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


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
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 :)
lwinkenbAuthor Commented:
Just to add, if I change it to:


Then m_dataStruct.dataSize gets filled with the correct value.
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 :)

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

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

lwinkenbAuthor Commented:
I know that :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.