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. | | | | re: reading byte by byte from memory
On Sep 5, 9:55*pm, Ashit Vora <a.k.v...@gmail.comwrote: 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 | | | | re: reading byte by byte from memory
Martin Ambuhl wrote: Quote:
Ashit Vora wrote:
>
.... snip ... Quote:
> 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.
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. | | | | 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 | | | | re: reading byte by byte from memory
pete wrote: Quote:
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
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 | | | | re: reading byte by byte from memory
Ashit Vora wrote: Quote: 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.
Quote:
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 | | | | re: reading byte by byte from memory
On Sep 6, 5:55*am, Ashit Vora <a.k.v...@gmail.comwrote: 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,382 network members.
|