From: barncat on 12 May 2010 13:41 hi I am reading data from a popen call (server) and sending the data to a client. It works fine except funny chars are prepended to the string sent and displayed at the client. here is the relevant code: -- FILE *FP; char string[32]; char buf[128]; FP = popen("lparstat -i | head -5 | tail -2","r"); while(fgets(buf,128,FP) != NULL) { strcat(string,buf); squeeze(string); //function to remove spaces } pclose(FP); if (send(socket, string, strlen(string), 0) == -1) perror("Writing String to client"); -- /* function to remove spaces */ int squeeze(char *s) { char *t = s; for(;*s;(*s != ' ') ? *t++ = *s++ : *s++) continue; *t = '\0'; } -- Output from client: -bash-2.05b$ ./program received answer to query from server: /ò,¸/ò,ÀType:Dedicated Mode:Capped -- Thanks, Jim
From: Eric Sosman on 12 May 2010 14:00 On 5/12/2010 1:41 PM, barncat wrote: > hi > I am reading data from a popen call (server) and sending the data to a > client. It works fine except funny chars are prepended to the string > sent and displayed at the client. here is the relevant code: > -- > FILE *FP; > char string[32]; > char buf[128]; > FP = popen("lparstat -i | head -5 | tail -2","r"); > while(fgets(buf,128,FP) != NULL) { > strcat(string,buf); Two things: First, since the contents of buf[] could be up to four times more than will fit in string[], this is a buffer overflow waiting to happen. Second (and this is probably the proximate cause of your problem), you have not initialized string[] in any way, so it "contains garbage." When you append buf[] to garbage, you get garbage followed by the contents of buf[]. (You could, in fact, get something much worse -- it depends on what kind of garbage happens to be in string[] to begin with.) Third ("There is no third thing!" "Yes, there is!") I cannot see any reason for string[] to exist at all, if all you want to do is transmit the de-spaced contents of buf[]. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: John Gordon on 12 May 2010 14:14 In <e9c77949-3250-4167-85d7-fe4bb39e248a(a)o8g2000yqo.googlegroups.com> barncat <thebarncat(a)gmail.com> writes: > I am reading data from a popen call (server) and sending the data to a > client. It works fine except funny chars are prepended to the string > sent and displayed at the client. here is the relevant code: Since you haven't given us both parts of the code (client and server), it's impossible to tell where the error is. (Whenever you are posting for help on this newsgroup, it's always best to post complete and compilable code, rather than incomplete snippets.) However, I'm going to guess that it's because your client program is doing something like this: char buf[128]; recv(socket, buf, sizeof(buf), 0); printf("%s\n", buf); You can't do that. Socket data is just bytes; it is not a string. If you want to treat socket data as a string, you have to keep track of the number of bytes that the socket actually received and insert a null character in the buffer at the correct spot, like this: char buf[128]; int len; len = recv(socket, buf, sizeof(buf), 0); buf[len] = '\0'; printf("%s\n", buf); This doesn't explain why the garbage characters are showing up at the beginning of your string rather than the end, but I'm sure the answer is in your code somewhere. -- John Gordon A is for Amy, who fell down the stairs gordon(a)panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"
From: barncat on 12 May 2010 14:41 > > Two things: First, since the contents of buf[] could be up > to four times more than will fit in string[], this is a buffer > overflow waiting to happen. Second (and this is probably the > proximate cause of your problem), you have not initialized string[] > in any way, so it "contains garbage." When you append buf[] to > garbage, you get garbage followed by the contents of buf[]. (You > could, in fact, get something much worse -- it depends on what > kind of garbage happens to be in string[] to begin with.) > > Third ("There is no third thing!" "Yes, there is!") I cannot > see any reason for string[] to exist at all, if all you want to > do is transmit the de-spaced contents of buf[]. > > -- > Eric Sosman > esos...(a)ieee-dot-org.invalid yes, i agree about the buffer overflow possibility. sorry, being sloppy. also do not do much C programming so i do struggle with it. i was using string to hold each line read from peopen file handle (buf concatenated to string) . buf wa supposed to be 32 bytes and string 128, sorry again thanks for you suggestions, i will give them a try
From: barncat on 12 May 2010 15:30 On May 12, 2:14 pm, John Gordon <gor...(a)panix.com> wrote: > In <e9c77949-3250-4167-85d7-fe4bb39e2...(a)o8g2000yqo.googlegroups.com> barncat <thebarn...(a)gmail.com> writes: > > > I am reading data from a popen call (server) and sending the data to a > > client. It works fine except funny chars are prepended to the string > > sent and displayed at the client. here is the relevant code: > > Since you haven't given us both parts of the code (client and server), > it's impossible to tell where the error is. > > (Whenever you are posting for help on this newsgroup, it's always best > to post complete and compilable code, rather than incomplete snippets.) > > However, I'm going to guess that it's because your client program is > doing something like this: > > char buf[128]; > recv(socket, buf, sizeof(buf), 0); > printf("%s\n", buf); > > You can't do that. Socket data is just bytes; it is not a string. If > you want to treat socket data as a string, you have to keep track of the > number of bytes that the socket actually received and insert a null > character in the buffer at the correct spot, like this: > > char buf[128]; > int len; > len = recv(socket, buf, sizeof(buf), 0); > buf[len] = '\0'; > printf("%s\n", buf); > > This doesn't explain why the garbage characters are showing up at the > beginning of your string rather than the end, but I'm sure the answer > is in your code somewhere. > > -- > John Gordon A is for Amy, who fell down the stairs > gor...(a)panix.com B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" Thanks John. I was not sure how much code to post. I did not want to post too much. i am trying to figure this out with your and Eric's suggestions. i will post all code if I can't get it working.
|
Next
|
Last
Pages: 1 2 3 Prev: Difference between service and process?? Next: New functional language - Fling |