Attempting to write a gets like function for tcp

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default 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...
    > Is there something I'm doing obviously wrong here?
    I'll stick to the most obvious stuff:
    > 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';
    Ack! What if 'i' is '-1' or zero?
    > (s->dataend) += i;
    > }
    > len = enter - s->buffer+1;
    If s->buffer==s->dataend, 'enter' may not contain anything meaningful!
    > retval = strndup(s->buffer, len);
    >
    > len = s->dataend-enter;
    > memmove(s->buffer, enter, s->dataend-enter);
    Umm, 'enter' points to the '\n', so your buffer now starts with a '\n'!
    That's certainly not what you want.
    > s->dataend -= len;
    >
    > return retval;
    > }
    That's the most serious stuff I could find.

    DS


    David Schwartz Guest

  2. Similar Questions and Discussions

    1. 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)"...
    2. 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...
    3. 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...
    4. [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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: Attempting to write a gets like function for tcp

    In article <vgns664f0nhi48@corp.supernews.com>, Derk Gwen 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);
    > ...
    Grin. I'm the naive one, not you. Shortly before you probably wrote
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139