State machine that locates the first line breaks, in C

Asked

Viewed 183 times

5

How to make a state machine in C, using decision commands (if, else) to go through a buffer received from the internet, byte by byte, in order to locate the first line breaks (\n\n), and only then write the content? The socket part is already ok, only the state machine is missing

  • 3

    Basically with switch case(estado)

  • 4

    What have you tried? What difficulty are you having?

  • 1

    as @Earendul says: the most basic way is with switch: http://stackoverflow.com/a/1648098/25324 In this question you have many other ways of making state machines (see other answers).

  • The connection is made through a socket, and implemented in the HTTP 1.0 protocol. The program is command-line operated and compiled over Linux. It takes as parameter a complete URI and a file name, and then connects to the server, retrieves the page and saves to the informed file.

  • Yes. The socket is ok. Missing only the part of the machine, to find and count these characters " r n r n". So I identify the beginning of the content, discharging all http header.

  • 2

    It’s still unclear why you think you need a state machine. Why can’t you use something like strtok or even a simple tie for? Have you tried with any of these approaches?

  • 1

    The "HTTP Header" consists of a few lines and an empty line break at the end. Each line has a string with its respective r n. And to be able to count these end-of-line characters + the r n r n of the empty line, you need a state machine, whatever way it is built. Thanks! Thanks for commenting.

  • 2

    I had mentioned the strtok, but what will serve you is the strstr. See this example in the SOEN: http://stackoverflow.com/questions/18726077/split-string-through-strtok-in-c-language

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.