6

For homework, I have to create a client/server program to store data. The client reads a file and sends it to the server via socket, the server receives it and stores the data. The final goal is to implement sharding between several servers. The program works well but the read function always read less data than I asked for when the client and server are not in the same machine

Here is my code for reading:

static inline size_t reliableRead(const int sourceSocket, void* data, const size_t nBytes)
{
    size_t dataRead = 0;
    unsigned int lossInARaw = 0;

    do
    {   const size_t readStatus = read(sourceSocket,data+dataRead,nBytes - dataRead);

        if (readStatus == (unsigned)-1)
        {
            LOG_ERRNO_ERROR("error on read");
            break;
        }

        dataRead += readStatus;

        if (readStatus != nBytes)
            LOG_WARNING("data loss(%u): read %zu/%zuo",++lossInARaw,dataRead,nBytes);
    } while (dataRead < nBytes);

    return dataRead;
}

Here is the result when I try to read blocks of 1024o then 8192o. First client and server are two differrents hosts, then on the same

server-side when receiving data sliced in blocks of 1024o from the client on another machine enter image description here

server-side when receiving data sliced in blocks of 8192o from the client on another machine enter image description here

server-side when receiving data when the client is also on the machine enter image description here

From the image, read is clearly capable of reading more than 1024o. I have never socket programming before, so maybe I am missing something, how can I make the read function reads mor often the amount of data I asked it to read ? Because even without displaying the warning, this clearly increases the sending time (don't mind the timing messag, it is clearly off, I will work on that)

6
  • 4
    Unrelated to your problem, but note that read returns a value of type ssize_t, which is a signed size type. Also note that read will return 0 when the other end of a connection have closed its socket or pipe, or you have reached end of file. Something I recommend that you handle gracefully, before it turns into an error. Oct 17, 2020 at 17:21
  • 3
    As for your problem, remember that both pipes and TCP sockets (if you're using any of them) are streaming ways of communicating, without message boundaries, starts or ends. It's all just a stream of bytes. Therefore what you call "data loss" isn't actually that, it's just that you might need more than one call to read to receive all data sent by the peer. And actually, a single call to read might actually give you more than a single "message", and that there can be a partial "message" at the end of the data you just received. Oct 17, 2020 at 17:26
  • @Someprogrammerdude I noticed the ssize_t this is why I explicitly used (unsigned)-1 in the if. I will handle the 0 returns but I wanted to check that it returns 0 if EOF is the only value reads, or if even if it is the 460th (for exemple) value.
    – Adrien
    Oct 17, 2020 at 17:42
  • @Someprogrammerdude I know that, but I didn't know how to call it. My server and client program are compiled with the same header file that defines the chunk to use. Before the call to reliableRead, I have another call to 'read()' that reads 4 octets to know the size of the incoming "Message"
    – Adrien
    Oct 17, 2020 at 17:45
  • 2
    @Adrien I'd keep it ssize_t and maybe cast to size_t iff there was no error (i.e., if the value was nonnegative). Using your approach, you need to compare with (size_t)-1 not (unsigned)-1. (unsigned)-1 is usually 0xffffffff (32 bits on 64 bit platforms) but (size_t)-1 would be 0xffffffffffffffff on a 64 bit platform. Oct 17, 2020 at 18:19

1 Answer 1

5

For recv, rather than read, POSIX specifies the MSG_WAITALL flag, which you should be able to use to prevent needless context switches to your thread when all it would do is just re-request the remainder of a would-be-partial read.

MSG_WAITALL On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.

3
  • 1
    I should have read the man of recv more carefully. I was so sure of missing something about networking I didn't saw that. I feel so dumb right now ^ ^ ' Thank you
    – Adrien
    Oct 17, 2020 at 17:36
  • MSG_WAITALL is a flag for recv(). read() has nothing to do with it, and it is equivalent to specifying zero flags in recv().
    – user207421
    Oct 18, 2020 at 4:58
  • @MarquisofLorne Yup. I had it reversed. "For recv, rather than read", not for "read, rather than recv". Thanks. Of course, I meant recv. read doesn't have a flags argument. :) Oct 18, 2020 at 7:20

Your Answer

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

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