Ask a Question related to UNIX Programming, Design and Development.
-
David Schwartz #1
Re: Attempting to write a gets like function for tcp
"James Blackwell" <jblack@comet.linuxguru.net> wrote in message
news:slrnbgnak2.sc5.jblack@comet.linuxguru.net...
I'll stick to the most obvious stuff:> Is there something I'm doing obviously wrong here?
Ack! What if 'i' is '-1' or zero?> struct connection {
> int conn_no;
> int bufflen;
> char *buffer;
> char *dataend;
> };
>
> #define BUFFLEN 16384
> char *tc_getline(struct connection *s) {
> char *retval;
> char *enter;
> int i;
> int bufferleft;
> int len;
>
>
> while ( s->buffer == s->dataend || !(enter=strchr(s->buffer,'\n') {
>
> len = s->dataend - s->buffer;
> bufferleft = s->bufflen - len;
>
> i = recv(
> s->conn_no,
> s->dataend,
> bufferleft,
> 0);
>
> s->dataend[i] = '\0';
If s->buffer==s->dataend, 'enter' may not contain anything meaningful!> (s->dataend) += i;
> }
> len = enter - s->buffer+1;
Umm, 'enter' points to the '\n', so your buffer now starts with a '\n'!> retval = strndup(s->buffer, len);
>
> len = s->dataend-enter;
> memmove(s->buffer, enter, s->dataend-enter);
That's certainly not what you want.
That's the most serious stuff I could find.> s->dataend -= len;
>
> return retval;
> }
DS
David Schwartz Guest
-
Error attempting to write location redirection
in the webserver.log, we are getting "Error attempting to write location redirection to web server (Windows NT error number -2147467259 occurred)"... -
write to database without giving write permission to IIS
Hi there, I have some ASP code that writes to access database. For security reason I do not want IUSER to give write permission to database... -
I need to write a function to compare IP addresses
"Phil Powell" <soazine@erols.com> wrote in message news:cd__a.739$3M4.380@lakeread04... nodes trying compare <snip> The other post while... -
[PHP] Write RegExp-Vars in a function
Hello Niels Use preg_replace_callback http://de.php.net/preg_replace_callback since it is faster than using the /e switch. Regards... -
Write RegExp-Vars in a function
Hi, Although this seemed to be an easy prob I couldn't solve it in all the time. I have a function and a string i want to be replaced. I use... -
Derk Gwen #2
Re: Attempting to write a gets like function for tcp
James Blackwell <jblack@comet.linuxguru.net> wrote:
#
# Hello and greetings all!
#
# I have been trying to get this code snippet to work for hours and I'm
# just plain stuck. This function is supposed to basically act like a
# gets for tcp, returning a line of data each time its called (and
# buffering remaining data until asked for) but fails miserably.
#
# The algorithm that I came up with was along these lines:
#
# while ( tcp buffer is empty | no \n in buffer) {
# grab data from the socket
# add new data to the buffer
# }
In my naivetÂŽ, I would be tempted to do that as
FILE *socket = fdopen(socketnumber,"r+");
...
fgets(line,sizeof(line),socket);
...
--
Derk Gwen [url]http://derkgwen.250free.com/html/index.html[/url]
We found a loophole; they can't keep us out anymore.
Derk Gwen Guest
-
James Blackwell #3
Re: Attempting to write a gets like function for tcp
In article <vgns664f0nhi48@corp.supernews.com>, Derk Gwen wrote:
Grin. I'm the naive one, not you. Shortly before you probably wrote> James Blackwell <jblack@comet.linuxguru.net> wrote:
> #
> # Hello and greetings all!
> #
> # I have been trying to get this code snippet to work for hours and I'm
> # just plain stuck. This function is supposed to basically act like a
> # gets for tcp, returning a line of data each time its called (and
> # buffering remaining data until asked for) but fails miserably.
> #
> # The algorithm that I came up with was along these lines:
> #
> # while ( tcp buffer is empty | no \n in buffer) {
> # grab data from the socket
> # add new data to the buffer
> # }
>
> In my naivetÂŽ, I would be tempted to do that as
> FILE *socket = fdopen(socketnumber,"r+");
> ...
> fgets(line,sizeof(line),socket);
> ...
this a swell aussie gentleman by the name of Chris McDonald pointed
out fdopen to me. fdopen/fgets was exactly what I was looking for.
fdopen was exactly the trick for me. Now I can stop trying to implement
a tcp version of fgets as one already exists!
One thing that surprises me, though. I don't think many of the socket
examples in the wild never actually get around to mentioning that fdopen
exists.
I do appreciate the advice though. As Chris said to me in private email,
"Network coding is now so simple it's almost boring."
Again, thank you
James Blackwell
James Blackwell Guest



Reply With Quote

