1

I am writing a portable application in C which is supposed to work on mac and windows, The socket code I am writing is using POSIX sockets Winsock api on windows has similar features.

The server is written in java and this is a client applicaiton which connects to server and then communicates,

I need to write a readline function Just as we have in C# or Java which reads a line terminated by newline character.

I am currently using following function but I do not think it is very efficient way to do it because I am reading character by character

    //return number of bytes read or -1 on error
int readline(SOCKET s, char* pResponse)
{
    char c = '0';
    int status = 0;
    int i = 0;

    while(true)
    {
        status = recv(s,&c, 1,0);

        if(status == SOCKET_ERROR)
        {
            //socket error
            return -1;
        }
        else if(status == 0)
        {
            //closed ?
            return -1;
        }
        else if(status > 0)
        {
            pResponse[i] = c;

            i++;

            if(c == '\n' || c == '\r\n')
            {
                break;
            }
        }
    }
    return i;
}

The sender/server will only send this line and wait for a response to come from client, so we can assume that it's okay to read entire socket data

So can I write this one better than this ? is reading char by char a performance issue ? any help would be appreciated.

3
  • 2
    c == '\r\n' is buggy my friend
    – loreb
    Jan 5, 2013 at 17:33
  • @loreb an explanation would help me more, thanks for pointing out
    – Ahmed
    Jan 5, 2013 at 17:34
  • '\r\n' is not a constant of type char, it takes two bytes -- you should get some compiler warnings about that; '\r\n' is what you get if you read the '\r' character followed by the '\n' character, ie you should check two consecutive characters.
    – loreb
    Jan 5, 2013 at 17:41

2 Answers 2

3

Yes, you can do better. Readline should read larger amounts of data from the socket and if there is any data after the first '\n' it should leave that data in a buffer somewhere for later. This is called "buffering" and will require a solid knowledge of pointers and arrays.

Your socket library might already be buffering for you, in which case you don't need to do this, but probably not.

3
  • 1
    Fwiw, I ended up writing char by char by mistake when I was learning sockets, and the result was that performance was still ok on linux but terrible on windows XP, as in ~100kb/s on the loop interface.
    – loreb
    Jan 5, 2013 at 17:35
  • After first new line there is no data, we can assume that
    – Ahmed
    Jan 5, 2013 at 17:36
  • OK, that makes it easier for you then. Just make sure you do non-blocking reads. Jan 5, 2013 at 19:45
0

You should break, when the last character in your buffer is '\r' and the character before it is '\n'.

If you are going to treat the contents of the buffer as a c string, then replace the '\r' by a '\0' before you return.

If the file you are reading is a dos text file, and the \r\n suggests that it is, consider the case, that the final line in the file may, or may not have a \r\n. That is a case you are likely to run into.

4
  • Thanks, for your comments, I was using \0 outside function which is I guess wrong, it should be in the function as you said. the other side is a server in java it does ps.println(json.toString()); ps is PrintStream. So should I just check eof as \n and previous char \r . Would that be enough ?
    – Ahmed
    Jan 5, 2013 at 18:40
  • I would recommend setting yourself up a unit test program to verify that it is working correctly. an empty one, one text line, two text lines, three text lines, and one with 2 text lines, with a third line which does not have carriage control. what about the case where you have just carriage control and no characters. Setting yourself up a unit test program will make it easy to verify in the future, and give you some confidence that your function works.
    – EvilTeach
    Jan 5, 2013 at 23:09
  • lol, and don't do like i did and forget the error conditions. does it fail correctly in the 2 scenerios that it should? what happens if the socket is a bad value? what happens if the response pointer is NULL? What happens if more data comes in than will fit in the buffer? Setting up unit tests to verify correct behavior will help keep you sane.
    – EvilTeach
    Jan 5, 2013 at 23:12
  • One other thing to note, is that the old mac carriage control was '\r' whereas the new macs use '\n'. Dos boxes use '\r\n'. Is that going to be a problem for you? Do you need the code to run on both platforms? It may be that some additional logic to try to infer what the correct carriage control is for your stream would be a good idea.
    – EvilTeach
    Jan 5, 2013 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.