|  | reading byte by byte from memory |  | |
| | | Ashit Vora |  |
| Posted: Sat Sep 06, 2008 4:55 am Post subject: reading byte by byte from memory |  |
Hi, I had one small query. I 'm coping data from an unsigned short to a char*.
code is
#include<stdio.h>
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2); }
here, how to I cehck if the data has been successfully copied or not. the actual purpose is....... this is a client prog and I wanna send the value of buf at the server. At server I wanna check if the value received is 0xFE10 pr not.
Can anyone please help me with this? 'm new to memory level C prog.
Thanks in advance. |
| |
| | | Martin Ambuhl |  |
| Posted: Sat Sep 06, 2008 4:55 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
Ashit Vora wrote:
| Quote: | Hi, I had one small query.
|
You should stop asking and start reading.
| Quote: | I 'm coping data from an unsigned short to a char*.
|
No you are not.
You have been told about <stdlib.h>, but you're too damn smart to listen.
| Quote: | int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512); ^^^^^^^ |
Not only is this a gross error, but if it had actually been a legal cast it would have been stupid. You have been told this, and you have been told why, but you're too damn smart to listen.
| Quote: | memset(buf,0,512);
|
You also should know better than hard-coding constants like 512.
| Quote: | //copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2); ^^^^ ^^^ |
(1) (2) (1) type is an unsigned short. The second argument to memcpy is a pointer-to-void. Did you ever bother even looking at the description of memcpy.
(2) If you're going to make implementation-specific assumptions about, for example, byte order, you are on your own. There is no reason to think that copying two bytes
| Quote: | }
here, how to I cehck if the data has been successfully copied or not.
|
Of course, it hasn't. Don't be such a fool.
| Quote: | the actual purpose is....... this is a client prog and I wanna send the value of buf at the server.
|
You have no business trying to write code. You haven't learned a damn thing either from the replies to your previous questions or from the programming course that you obviously failed,
| Quote: | At server I wanna check if the value received is 0xFE10 pr not.
Can anyone please help me with this? 'm new to memory level C prog. No one can help you, since you ignore everything people tell you. |
|
| |
| | | CBFalconer |  |
| Posted: Sat Sep 06, 2008 4:55 am Post subject: Re: reading byte by byte from memory |  |
Martin Ambuhl wrote:
| Quote: | Ashit Vora wrote:
.... snip ...
here, how to I cehck if the data has been successfully copied or not.
Of course, it hasn't. Don't be such a fool.
the actual purpose is....... this is a client prog and I wanna send the value of buf at the server.
You have no business trying to write code. You haven't learned a damn thing either from the replies to your previous questions or from the programming course that you obviously failed,
At server I wanna check if the value received is 0xFE10 pr not. Can anyone please help me with this? 'm new to memory level C prog.
No one can help you, since you ignore everything people tell you.
|
I gather you don't consider his code exemplary? :-)
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. |
| |
| | | Ashit Vora |  |
| Posted: Sat Sep 06, 2008 5:10 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
On Sep 5, 9:55 pm, Ashit Vora <a.k.v...@gmail.com> wrote:
| Quote: | Hi, I had one small query. I 'm coping data from an unsigned short to a char*.
code is
#include<stdio.h
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2);
}
here, how to I cehck if the data has been successfully copied or not. the actual purpose is....... this is a client prog and I wanna send the value of buf at the server. At server I wanna check if the value received is 0xFE10 pr not.
Can anyone please help me with this? 'm new to memory level C prog.
Thanks in advance.
|
Here is the actual code 'm using... If it makes the picture more clear.
Client's piece of code.
//total_buf_size is the sum of header plus data total_buf_size=10; buf =(char*) malloc (total_buf_size);
if(buf==NULL){ printf("Malloc Failed\n"); }
memset(buf,0, total_buf_size); memcpy(buf,&type,2); memcpy(&buf[2],&offset,4); memcpy(&buf[6],&data_length,4);
/*now sending the data BYTE AT A TIME*/ for(i=0;i<10;i++){ if(send(client_sock,&buf[i],1,0)==-1){ perror("send"); exit(1); } }
/**************************************************/ Server's piece of Code is:
for(i=0;i<10;i++){ if(recv(new_fd,&buf[i],1, 0)==-1){ perror("receive"); exit(1); } printf("%02x \t",buf[i]); } printf("\n"); memcpy(&type,buf,2); buf+=2; memcpy(&offset,buf,4); buf+=4; memcpy(&data_length,buf,4);
printf("TYPE: %s\n",(char*)type); if(type == 0xFE10) printf("RECEIVED"); else printf("NOT RECEIVED"); }
NOTE: type at client was unsigned short type=0xFE10
Thanks |
| |
| | | pete |  |
| Posted: Sat Sep 06, 2008 5:14 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
Ashit Vora wrote:
| Quote: | Hi, I had one small query. I 'm coping data from an unsigned short to a char*.
code is
#include<stdio.h
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2); }
here, how to I cehck if the data has been successfully copied or not. the actual purpose is....... this is a client prog and I wanna send the value of buf at the server. At server I wanna check if the value received is 0xFE10 pr not.
Can anyone please help me with this? 'm new to memory level C prog.
|
BEGIN new.c output
buf[0] is 0x16 buf[1] is 0x254
END new.c output
Feel free to ask any questions about new.c:
/* BEGIN new.c */
#include <stdio.h> #include <string.h>
int main(void) { unsigned short type = 0xFE10; unsigned char buf[sizeof type] = {0}; unsigned byte; /* ** copying 2 bytes of type which is 0xFE10 to buf */ memcpy(buf, &type, sizeof type != 1 ? 2 : 1); puts("BEGIN new.c output\n"); for (byte = 0; byte != sizeof type; ++byte) { printf("buf[%u] is 0x%u\n", byte, buf[byte]); } puts("\nEND new.c output"); return 0; }
/* END new.c */
-- pete |
| |
| | | pete |  |
| Posted: Sat Sep 06, 2008 5:28 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
pete wrote:
| Quote: | Ashit Vora wrote: Hi, I had one small query. I 'm coping data from an unsigned short to a char*.
code is
#include<stdio.h
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2); }
here, how to I cehck if the data has been successfully copied or not. the actual purpose is....... this is a client prog and I wanna send the value of buf at the server. At server I wanna check if the value received is 0xFE10 pr not.
Can anyone please help me with this? 'm new to memory level C prog.
BEGIN new.c output
buf[0] is 0x16 buf[1] is 0x254
END new.c output
|
That's not really what I wanted. I'll try again:
BEGIN new.c output
buf[0] is 0x10 buf[1] is 0xfe
END new.c output
/* BEGIN new.c */
#include <stdio.h> #include <string.h>
int main(void) { unsigned short type = 0xfe10; unsigned char buf[sizeof type] = {0}; unsigned byte; /* ** copying all bytes of type which is 0xfe10 to buf */ memcpy(buf, &type, sizeof type); puts("BEGIN new.c output\n"); for (byte = 0; byte != sizeof type; ++byte) { printf("buf[%u] is 0x%x\n", byte, buf[byte]); } puts("\nEND new.c output"); return 0; }
/* END new.c */
-- pete |
| |
| | | Bartc |  |
| Posted: Sat Sep 06, 2008 7:43 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
Ashit Vora wrote:
| Quote: | #include<stdio.h
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2);
}
here, how to I cehck if the data has been successfully copied or not. the actual purpose is....... this is a client prog and I wanna send the value of buf at the server. At server I wanna check if the value received is 0xFE10 pr not.
Client's piece of code.
//total_buf_size is the sum of header plus data total_buf_size=10; buf =(char*) malloc (total_buf_size);
if(buf==NULL){ printf("Malloc Failed\n"); }
|
If buf_size is really only 10 bytes, you could just use char buf[10];
| Quote: | memset(buf,0, total_buf_size); memcpy(buf,&type,2); memcpy(&buf[2],&offset,4); memcpy(&buf[6],&data_length,4);
|
You seem to be filling a struct with values of 2,4 and 4 bytes. You might be better off defining:
struct { unsigned short sig; int offset, data_length; } header = {0xFE10, 0, 0};
BUT only if you can persuade your compiler not to leave a 2-byte gap between ..sig and .offset.
Then instead of memcpy:
header.offset=offset; header.data_length=data_length;
| Quote: | /*now sending the data BYTE AT A TIME*/ for(i=0;i<10;i++){ if(send(client_sock,&buf[i],1,0)==-1){ perror("send"); exit(1);
|
Accessing buf (now header) a byte at a time is a little more tricky:
for (i=0; i<sizeof(header); ++i){ if send(client_sock, ((char*)&header+i),1,0)...
(Why can't you send all bytes at once? If you can do that, you can also send 0xFE10 as a 2-byte block, and offset and data_length as two 4-byte blocks, you don't need the struct or buf)
Do something similar in the server code and check whether .sig or .sig is 0xFE10
-- Bartc |
| |
| | | Nick Keighley |  |
| Posted: Sat Sep 06, 2008 11:44 am Post subject: Re: reading byte by byte from memory |  |
| |  | |
On Sep 6, 5:55 am, Ashit Vora <a.k.v...@gmail.com> wrote:
| Quote: | I had one small query. I 'm coping data from an unsigned short to a char*.
code is
#include<stdio.h
int main(void){ unsigned short type=0xFE10; char * buf = char(*) malloc(512);
|
the cast is syntactically wrong and unnecessary
| Quote: | memset(buf,0,512);
//copying 2 bytes of type which is 0xFE10 to buf memcpy(buf,type,2);
}
here, how to I cehck if the data has been successfully copied or not.
|
why wouldn't it be? use printf(), or memcmp() or a debugger
| Quote: | the actual purpose is.......
|
....... isn't standard punctuation
| Quote: | this is a client prog and I wanna send the value of buf at the server.
|
"to the server"? For socket stuff try comp.unix.programmer (I think).
| Quote: | At server I wanna check if the value received is 0xFE10 pr not.
|
memcmp()?
| Quote: | Can anyone please help me with this? 'm new to memory level C prog.
|
I'm not sure what "memory level programming" is
-- Nick Keighley |
| |
|
|