Morning Fellas,
Need some pointers from you folks. Am stuck with a simple issue.
On the server i have a struct defined as:
struct sendbuff { int count; char toprint[10000]; };
My objective is to pack this buffer across the n/w and recv it on the client. count is a simple counter which i am usign to keep track of the lost/unordered packets is any. My recv system call on the client is as follows. mybuff on the client has been declared of type struct sendbuff.
while(1) //Beggining of While {
while(bytesrcvd<sizeof(mybuff)) {
if ((numbytes=recv(sockfd, (char *)&mybuff, sizeof(mybuff), 0)) == -1) { perror("recv"); exit(1); } bytesrcvd=numbytes+bytesrcvd; }
printf("mybuff.toprint is %s\n",mybuff.toprint); //Problematic printf("mybuff.count is %d\n",mybuff.count); //Problematic
.....etc
} //End of While
On the client mybuff is of type sendbuff. The whole problem is that i am unable to interpret the contents of mybuff which is being used to store the contents of the recv() system call.
Have outlined the two printf() which i need to work out as //problematic. I know this issue centers around the little/big eindian format issue. Am just going round and round in cirles and would apperciate f/b in helping obtain and print the following:
mybuff.toprint and mybuff.count
Also one last thing. As the buffer(toprint) size changes the charactersitics of the problem i face increases..:D.
Thanks in advance folks.
Trevor
|------|____________________________________|------| ( >- / Scaling FreeSoftware & OpenSource \ -< ) /~\ / In the Enterprise \ /~\ | ) \ | www.fsf.org | www.opensource.org | / (/ | |_|_ ____________________________________/ _|_|
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Sometime on Sep 4, TW cobbled together some glyphs to say:
struct sendbuff { int count; char toprint[10000]; };
[snip]
while(1) //Beggining of While {
while(bytesrcvd<sizeof(mybuff)) {
possible bug here. you're not taking into account word alignment. The actual size of a struct in memory is not the same as the sum of its elements. It may be larger to account for aligning variables on word boundaries.
You need to have serialize and deserialize functions for your struct that convert it into a byte stream and back.
--- Philip Tellis philip.tellis@gmx.net wrote:
You need to have serialize and deserialize functions for your struct that convert it into a byte stream and back.
[snip]
Yep, agree with you on that. Do you have any sample code that i can look at or some URL that i can read up on. Alas...what keyworks should i google for???.
Thanks dear for all the help.
Trevor
|------|____________________________________|------| ( >- / Scaling FreeSoftware & OpenSource \ -< ) /~\ / In the Enterprise \ /~\ | ) \ | www.fsf.org | www.opensource.org | / (/ | |_|_ ____________________________________/ _|_|
______________________________________________________ Click here to donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/
Sometime Today, TW cobbled together some glyphs to say:
Yep, agree with you on that. Do you have any sample code that i can look at or some URL that i can read up on. Alas...what keyworks should i google for???.
Read the libyahoo2 sources. You can copy the serialisation and deserialisation portions if your code is GPLed.
Philip
--- Philip Tellis philip.tellis@gmx.net wrote:
Read the libyahoo2 sources. You can copy the serialisation and deserialisation portions if your code is GPLed.
[snip]
Thanks Philip.
Trevor
Philip
|------|____________________________________|------| ( >- / Scaling FreeSoftware & OpenSource \ -< ) /~\ / In the Enterprise \ /~\ | ) \ | www.fsf.org | www.opensource.org | / (/ | |_|_ ____________________________________/ _|_|
______________________________________________________ Click here to donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/
Trevor,
Disclaimer: Might work. This is somewhere I am also stuck at this time with a similar problem.
What you seek is called "marshalling the data".
On Mon, 2005-09-05 at 10:18, Trevor Warren wrote:
On the server i have a struct defined as:
struct sendbuff { int count; char toprint[10000]; };
For ease of use let the structure be: typedef struct sendbuff { int count; char *toprint; } SENDBUFF; .................. { SENDBUFF *sbuf = malloc (sizeof(SENDBUF); sbuf->count = 999; sbuf->toprint = malloc(strlen("This is Cool Stuff!")); strcpy (sbuf->toprint, "This is cool Stuff!");
// Now { char *databuf; int bufsize = 0, buflen = 0; bufsize = sizeof(int) + strlen(sbuf->toprint) + 1; databuf = malloc (bufsize); memset (databuf, 0, bufsize); memcpy(databuf, &(sbuf->count), sizeof(int)); buflen = sizeof(int); memcpy(databuf + buflen, sbuf->toprint, strlen(sbuf->toprint) + 1); //buflen += strlen(sbuf->toprint) + 1;
// Now do the sending to the client socket // send (client_sock, databuf, bufsize, 0); ....................... }
Done on the server:
My objective is to pack this buffer across the n/w and recv it on the client. count is a simple counter which i am usign to keep track of the lost/unordered packets is any. My recv system call on the client is as follows. mybuff on the client has been declared of type struct sendbuff.
Declare mybuff as char *.
char *mybuff;
while(1) //Beggining of While { while(bytesrcvd<sizeof(mybuff)) {
if ((numbytes=recv(sockfd, (char *)mybuff, sizeof(mybuff), 0)) == -1) { perror("recv"); exit(1); } bytesrcvd=numbytes+bytesrcvd; }
Now over here we retrieve the struct members from mybuff.
{ SENDBUFF *recvdstuff; recvdstuff->count = *((int *)mybuff; recvdstuff->toprint = mybuff + sizeof(int);
printf("recvdstuff->count is: %d\n", recvdstuff->count); printf("recvdstuff->toprint is: %s\n", recvdstuff->toprint); }
This should work, with a little tweaking to suit your needs. You could change recvdstuff not to be a pointer.
Could somebody point me to handling a case as follows:
typedef struct test_struct { int s_id; char *doc_id; } TEST_STRUCT;
sendbuff in my case is **ts (where ts is TEST_STRUCT **ts).
Thanks in Advance, Animesh
Thanks dear. Did get the code to work earlier in the day.
Trevor
|------|____________________________________|------| ( >- / Scaling FreeSoftware & OpenSource \ -< ) /~\ / In the Enterprise \ /~\ | ) \ | www.fsf.org | www.opensource.org | / (/ | |_|_ ____________________________________/ _|_|
______________________________________________________ Click here to donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/