TCPLineStream

To use the tcp line stream library, you also need to follwing dependencies:

Basically, one opens the stream to a given server on a given port, and then just send and read lines. The library handles line end detection (and adds and removes as needed), and takes away all the asynchonous stuff (all calls are blocking). Here is an example code:

    TCPLineStream *stream=new TCPLineStream("google.com",80,"\r\n");

    if (!stream->open())
        return NULL;
    
    // we append a single \r\n here - the stream will add another one, which finishes the request to the server
    // one could append headers here, and send an empty line at the end
    stream->sendLine(string("GET http://google.com HTTP/1.0\r\n"));

    string firstLine=stream->readLine();
    // response must be for HTTP/1.0, and be 200
    if (0!=firstLine.compare("HTTP/1.0 200 OK")) {
        printf("call not sucessfull, response=%s\n",firstLine.c_str());
        return NULL;
    }
    // read headers
    while (true) {
        string line=stream->readLine();
        printf("header=[%s]\n",line.c_str());
        if (0==line.length())
            break;
    }
    while (true) {
	// give a initial length for the received line (will be expanded on demand, but this makes it somewhat faster)
        string line=stream->readLine(1500);
        printf("content=[%s]\n",line.c_str());
        if (0==line.length())
            break;
    }
    stream->close();


Please log in to post comments.