From: David Schwartz on 12 May 2010 21:06 On May 12, 4:31 pm, barncat <thebarn...(a)gmail.com> wrote: > if (send(socket, string, strlen(string) + 1, 0) == -1) > perror("Writing String to client"); Okay, so you send a "message" that consists of an arbitrary number of bytes of data and a terminating zero. The receiver can tell that it has the whole message when it receives a zero byte. > /* Read back response from server */ > if (read(sockfd, ServerInfo, 128), 0) > perror("Reading back server data"); > printf("%s\n",ServerInfo); Ack! You totally forgot what you were doing. You send a terminating zero, and you forgot to check whether or not you received one. So you *still* call 'printf' with a '%s' whether or not you received a complete, terminated string. This is why you *NEED* to draw up a specification. If the specification says "the sender shall send a zero byte at the end of each message and the recipient shall not process a message until it receives the terminating zero byte", then it would be clear that the reader is broken, and there would be no risk that while you were writing one side, you completely forget how the other side works. DS
From: barncat on 13 May 2010 08:49 On May 12, 8:42 pm, Barry Margolin <bar...(a)alum.mit.edu> wrote: > In article > <e9c77949-3250-4167-85d7-fe4bb39e2...(a)o8g2000yqo.googlegroups.com>, > > barncat <thebarn...(a)gmail.com> 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: > > I think it's because of strcat(string, buf). You never initialized > string[], so it contains garbage, and then you append buf to that. thanks. but after i got it "working", it did not matter if i initialized string[] or not. adding the null terminator and +1 char to the string length in the send call worked
From: barncat on 13 May 2010 09:11 On May 12, 9:06 pm, David Schwartz <dav...(a)webmaster.com> wrote: > On May 12, 4:31 pm, barncat <thebarn...(a)gmail.com> wrote: > > > if (send(socket, string, strlen(string) + 1, 0) == -1) > > perror("Writing String to client"); > > Okay, so you send a "message" that consists of an arbitrary number of > bytes of data and a terminating zero. The receiver can tell that it > has the whole message when it receives a zero byte. > > > /* Read back response from server */ > > if (read(sockfd, ServerInfo, 128), 0) > > perror("Reading back server data"); > > printf("%s\n",ServerInfo); > > Ack! You totally forgot what you were doing. You send a terminating > zero, and you forgot to check whether or not you received one. So you > *still* call 'printf' with a '%s' whether or not you received a > complete, terminated string. > > This is why you *NEED* to draw up a specification. If the > specification says "the sender shall send a zero byte at the end of > each message and the recipient shall not process a message until it > receives the terminating zero byte", then it would be clear that the > reader is broken, and there would be no risk that while you were > writing one side, you completely forget how the other side works. > > DS i agree with you! however, i am not a C programmer. I am an sys ad given this task and i don't have the time right now to tighten this thing up. so for now, it is quick and dirty (very dirty). However, i will make the time to incorporate a send/recv specification and give it a shot. I will post back in a few days with it. Thank you David
From: barncat on 13 May 2010 09:16 > Heck with it. Sludge tolerance reached. Going no further until > some evidence of cooperation appears, or until you start paying me. > > -- > Eric Sosman > esos...(a)ieee-dot-org.invalid Fair enough. Give me a few days to de sludge.. thanks
First
|
Prev
|
Pages: 1 2 3 Prev: Difference between service and process?? Next: New functional language - Fling |