使用sock_stream时连续调用write是否安全?

时间:2015-03-04 17:22:51

标签: c linux sockets tcp

我需要在Linux上用C编写一个小型客户端/服务器应用程序。

我已经构建了一个简短的例子,以便进一步探索,因为我是网络编程的新手。

我基本上是在尝试发送由客户端动态分配的double数组。

我找到了以下方法(客户端):

write(sd,&datas.size,sizeof(int)); /* send size */
write(sd,datas.yi,datas.size*sizeof(double));/* send array */

并在服务器端:

read(sd_cli,&datas.size,sizeof(int)); /* receive size */
datas.yi=(double *)malloc(datas.size*sizeof(double));
read(sd_cli,datas.yi,datas.size*sizeof(double)); /* receiving datas */

乍一看,我的代码似乎工作正常。

但由于write调用是非阻塞的,我问自己read序列是否可以接收,例如double之前的数组?

有没有保证不会发生这种情况?

感谢。

1 个答案:

答案 0 :(得分:1)

SOCK_STREAM类型的套接字提供可靠的有序数据传输,但细节取决于底层传输的性质。读者将按照写入的顺序逐字节地接收成功写入的所有数据(如果它实际上选择全部读取),但不一定是相同大小的块。

阻止 vs 。非阻塞与它无关,虽然我实际上并没有看到是什么让你说你的写入是非阻塞的。也许你正在评论这样一个事实:write()read()都没有承诺传输任何给定调用所请求的全部字节数。这本身并不保证不会阻塞,但你绝对需要正确地考虑它,特别是对于套接字,尤其是如果你真的已经将套接字的一端或两端置于非阻塞模式。您问题的原始版本似乎声称您确实对其进行了说明。

在任何情况下,除非存在某种内核错误,否则您的客户端将永远不会在数组的任何部分之后读取数组大小,也不会以不同的相对顺序接收字节。

然而,非常清楚,这里是通过流套接字读取和写入可变大小double数组的合理实现。他们假设发送者和接收者具有类型double的相同表示,对于UNIX域套接字肯定是这种情况。虽然包含大约一半代码的辅助函数适合重用,但它们并非完全无关紧要:

/******
 * helper functions
 */

/*
 * Returns the number of bytes written, which may be zero, or a number
 * less than zero on failure.
 */
ssize_t write_fully(int fd, const void *buf, size_t count) {
    const unsigned char *next = buf;
    size_t remaining = count;

    while (remaining) {
        ssize_t n_written = write(fd, next, remaining);

        if (n_written < 0) {
            /* error */
            return n_written;
        } else {
            assert(n_written <= remaining);
            next += n_written;
            remaining -= n_written;
        }
    }

    /* all bytes successfully written */
    return count;
}

/* 
 * Returns the number of bytes read on success, or a number less
 * than zero on error.  It is accounted "success" if the end of the stream
 * is reached before the requested number of bytes is read; that case
 * can be distinguished by the return value, but no recovery is
 * possible.
 */
ssize_t read_fully(int fd, void *buf, size_t count) {
    unsigned char *next = buf;
    size_t remaining = count;

    while (remaining) {
        ssize_t n_read = read(fd, next, remaining);

        if (n_read < 0) {
            /* error */
            return n_read;
        } else if (n_read) {
            assert(n_read <= remaining);
            next += n_read;
            remaining -= n_read;
        } else {
            /* premature end of file */
            return count - remaining;
        }
    }

    /* all bytes successfully read */
    return count;
}

/******
 * Array-transfer functions
 */

/* returns 0 on success, else nonzero */
int write_double_array(int fd, unsigned n, double d[n]) {
    ssize_t bytes_written;

    bytes_written = write_fully(fd, &n, sizeof(n));
    if (bytes_written < 0) return bytes_written;

    bytes_written = write_fully(fd, d, n * sizeof(double));
    return (bytes_written < 0) ? bytes_written : 0;
}

/*
 * returns 0 on success, else nonzero.
 * On success, the caller takes responsibility for freeing the
 * dynamically-allocated result array.
 */
int read_double_array(int fd, unsigned *n, double **d) {
    unsigned temp_n;
    ssize_t bytes_read = read_fully(fd, &temp_n, sizeof(temp_n));

    if (bytes_read < 0) {
        return -1;
    } else if (bytes_read != sizeof(temp_n)) {
        return 1;
    } else if (temp_n) {
        size_t n_bytes = temp_n * sizeof(double);
        double *temp = malloc(n_bytes);

        if (!temp) return -1;  /* allocation failure */
        if (read_fully(fd, temp, n_bytes) < n_bytes) {
            free(temp);
            return -1;
        }

        /* success */
        *d = temp;
    }

    *n = temp_n;
    return 0;
}

您可以以不同方式实现数组传输协议,但该方法以您声称要执行的相同形式发送数据。你不能安全地做到这一点。

相关问题