1

My question arises because I am developing a small server using C and sockets with select in a Linux system. The server receives one character at the time.

Ex: if a client types "hi" and then presses enter, the server will receive h, then i, and finally '\n'.

I heard of '\r' but I can't find any relevant information about it more than what it is but not when it is sent.

The question is: If I wanna execute an action based on the value of the string, should I buffer the data up to the '\n' and then check what it contains? Or should I be looking for a '\r' everytime a character is received?

3
  • Depends on the protocol. For example, HTTP uses \r\n in between lines of the request and response headers
    – Ben Voigt
    Commented Apr 4, 2015 at 3:16
  • So in the 'hi' example, would it be 'h', then 'i', then '\r\n'? @BenVoigt
    – OHHH
    Commented Apr 4, 2015 at 3:22
  • Yes four characters, h, i, \r, \n
    – Ben Voigt
    Commented Apr 4, 2015 at 3:24

1 Answer 1

3

Depends on what is being sent over the socket. The sockets don't care. What protocol is the server using? That's the question. Did it read lines with gets()? From files? Your client has to know something about the agreement (protocol) being used.

You can use a packet sniffer like wireshark some other way to 'sniff the wire' to see what is being sent from the client. Or just use read() as shown below and read a fixed amount and display the ASCII numeric value of what you receive until you determine the line terminator or delimiter you want to divide content up with.

'\r' = carriage return (ASCII 0x0d)
'\n' = linefeed (ASCII 0x0a)

No big mystery to socket programs.

Here's an example of communicating over sockets in a single program using a pair of sockets retrieved from a socketpair() system call.

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>

#define DATA1 "In Xanadu, did Kublai Khan . . ."
#define DATA2 "A stately pleasure dome decree . . ."

/*
 * This program creates a pair of connected sockets, then forks and
 * communicates over them.  This is very similar to communication with pipes;
 * however, socketpairs are two-way communications objects. Therefore,
 * this program can send messages in both directions.
 */
main()
{
    int sockets[2], child;
    char buf[1024];

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
        perror("opening stream socket pair");
        exit(1);
    }
    if ((child = fork()) == -1)
        perror("fork");
    else if (child) {   /* This is the parent. */
        close(sockets[0]);
        if (read(sockets[1], buf, 1024, 0) < 0)
            perror("reading stream message");
        printf("-->%s\n", buf);
        if (write(sockets[1], DATA2, sizeof(DATA2)) < 0)
            perror("writing stream message");
        close(sockets[1]);
    } else {        /* This is the child. */
        close(sockets[1]);
        if (write(sockets[0], DATA1, sizeof(DATA1)) < 0)
            perror("writing stream message");
        if (read(sockets[0], buf, 1024, 0) < 0)
            perror("reading stream message");
        printf("-->%s\n", buf);
        close(sockets[0]);
    }
}
1
  • It's good to note the read() count could be used. It's obviously not a complete practical example. Just to illustrate some basics and that delimiters don't need to be explicitly checked for.
    – clearlight
    Commented Apr 4, 2015 at 3:35

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