End line of text based network protocol

Discussion in 'C++' started by Mike Mimic, May 7, 2004.

  1. Mike Mimic

    Mike Mimic Guest

    Hi!

    I am programming small program which would use its own
    text based network protocol. I come to the problem with
    ending lines. I was thinking about ending them with \r\n
    combination. But the problem is that I would like that
    program would be tolerant to other endings (only \r or \n).

    So the question is if there is some efficient way to
    implement this? Is there some library which would do this?
    It should be platform independent.


    Mike
    Mike Mimic, May 7, 2004
    #1
    1. Advertising

  2. "Mike Mimic" <> wrote in message
    news:c7ehnj$c0n$...
    > Hi!
    >
    > I am programming small program which would use its own
    > text based network protocol. I come to the problem with
    > ending lines. I was thinking about ending them with \r\n
    > combination. But the problem is that I would like that
    > program would be tolerant to other endings (only \r or \n).
    >
    > So the question is if there is some efficient way to
    > implement this? Is there some library which would do this?
    > It should be platform independent.



    Well a check for '\r' detectes both '\r' and '\n' so you could do:

    if(c=='\r' || c=='\n')
    // ...

    The first check detects both '\r' and '\r' '\n' sequence and if none of them
    exist checks for '\n' alone.






    Regards,

    Ioannis Vranos
    Ioannis Vranos, May 7, 2004
    #2
    1. Advertising

  3. Mike Mimic

    Mike Mimic Guest

    Hi!

    Ioannis Vranos wrote:
    Well a check for '\r' detectes both '\r' and '\n' so you could do:
    >
    > if(c=='\r' || c=='\n')
    > // ...
    >
    > The first check detects both '\r' and '\r' '\n' sequence and if none of them
    > exist checks for '\n' alone.


    But the problem with this is if end line is \r\n I will get after that
    one emtpy line. (Because of \n.)


    Mike
    Mike Mimic, May 7, 2004
    #3
  4. "Mike Mimic" <> wrote in message
    news:c7ehnj$c0n$...
    > Hi!
    >
    > I am programming small program which would use its own
    > text based network protocol. I come to the problem with
    > ending lines. I was thinking about ending them with \r\n
    > combination. But the problem is that I would like that
    > program would be tolerant to other endings (only \r or \n).
    >
    > So the question is if there is some efficient way to
    > implement this? Is there some library which would do this?
    > It should be platform independent.
    >


    I don't see this as anything other than a parsing issue. Presumably you are
    parsing the content of lines in your program as well, so why not use
    whatever techniques you are already using?

    Unless the parsing task was really simple, this is an area where I would get
    some help. One well established parsing tool is the flex/bison combination,
    but there are loads more. Just google for parser generator.

    John
    John Harrison, May 7, 2004
    #4
  5. Mike Mimic

    Mike Mimic Guest

    Hi!

    John Harrison wrote:
    > I don't see this as anything other than a parsing issue. Presumably you are
    > parsing the content of lines in your program as well, so why not use
    > whatever techniques you are already using?


    I would like to have line by line so that I can than know where is the
    end of line and so on. I think that it will be easier for me to have
    a line to parse than to have multiple lines.

    > Unless the parsing task was really simple, this is an area where I would get
    > some help. One well established parsing tool is the flex/bison combination,
    > but there are loads more. Just google for parser generator.


    Anyone any other suggestions?


    Mike
    Mike Mimic, May 7, 2004
    #5
  6. "Mike Mimic" <> wrote in message
    news:c7g3s0$l5b$...
    > Hi!
    >
    > John Harrison wrote:
    > > I don't see this as anything other than a parsing issue. Presumably you

    are
    > > parsing the content of lines in your program as well, so why not use
    > > whatever techniques you are already using?

    >
    > I would like to have line by line so that I can than know where is the
    > end of line and so on. I think that it will be easier for me to have
    > a line to parse than to have multiple lines.
    >
    > > Unless the parsing task was really simple, this is an area where I would

    get
    > > some help. One well established parsing tool is the flex/bison

    combination,
    > > but there are loads more. Just google for parser generator.

    >
    > Anyone any other suggestions?
    >


    Well then you are going to have to write your own line reading function

    string read_line(istream& in)
    {
    string line;
    char ch;
    while (in.get(ch))
    {
    if (ch == '\n')
    break;
    if (ch == '\r')
    {
    if (in.get(ch) && ch != '\n')
    in.putback(ch);
    break;
    }
    line += ch;
    }
    return line;
    }

    Untested code.

    john
    John Harrison, May 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    OO network protocol

    VisionSet, Nov 13, 2005, in forum: Java
    Replies:
    6
    Views:
    439
    VisionSet
    Nov 14, 2005
  2. DaveC

    Network Protocol?

    DaveC, Jul 9, 2004, in forum: C++
    Replies:
    14
    Views:
    696
    Phlip
    Jul 10, 2004
  3. kaushikshome
    Replies:
    4
    Views:
    759
    kaushikshome
    Sep 10, 2006
  4. walterbyrd
    Replies:
    7
    Views:
    307
    Asun Friere
    May 17, 2007
  5. scad
    Replies:
    23
    Views:
    1,157
    Alf P. Steinbach
    May 17, 2009
Loading...

Share This Page