Solved

Is this correct?

Posted on 2004-08-08
13
234 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now