简体   繁体   中英

What does ENOENT mean when sending data over a socket?

I am working on some embedded C code that posts data to a website over the HTTP protocol. The first bytes go well, chunked in 1024 byte buffers. At the end, though, the send() call fails with errno=2 (ENOENT). I cannot find this error in the manpage , so I have no idea what goes wrong. My HTTP POST header says

Content-Length: 12517

which is the exact amount of bytes I am sending (it is a raw file post).

This is my code ( sockfd is an correctly opened socket):

void send_all_n(int sockfd, char *buf, int buf_len) {
    int bytes_sent = 0;
    char *buf_ptr = buf;
    int failed = 0;

    do {
        bytes_sent = send(sockfd, buf_ptr, buf_len-bytes_sent, 0);
        if (bytes_sent > 0) {
            buf_ptr += bytes_sent;
        } else if (bytes_sent == -1) {
            switch(errno) {
                case EWOULDBLOCK:
                    usleep(100000);
                    break;
                default:
                    failed = 1;
                    break;
            }
        }
    } while (bytes_sent < buf_len && failed == 0);
}

Many 1024-byte buffers go well, until somewhere at the end, my logging says this:

2014-04-09 18:13:31 +0800 [D]: [network] wrote 328 bytes to host, buf_len=1024, bytesleft=696
2014-04-09 18:13:31 +0800 [D]: [network] wrote -1 bytes to host, buf_len=1024, bytesleft=1025
2014-04-09 18:13:31 +0800 [S]: [network] could not send data: errno=2

Update : if I add 1 second sleeps between the sending of the buffers, it works fine.

Are you sure there is no call changing errno in between the send() that failed and the call that logs it?

At least for debugging I'd add perror("send() failed") to the switch() 's default case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM