Skip to content

SocketSend

Description

Blocking write to a connected socket. Does not partial-retry; the caller decides whether to loop on short writes.

Parameters

Name Direction Description
self in Socket to write to.
buf in Source buffer.
n in Number of bytes to send.

Success

returns number of bytes sent.

Failure

returns -1; logs the failing syscall.

Usage example (Cross-references)

Usage examples (Cross-references)
            return -1;
        }
        i64 sent = SocketSend(&sock, query, qlen);
        if (sent != (i64)qlen) {
            SocketClose(&sock);
    }
    
    i64 SocketSend(Socket *self, const void *buf, size n) {
        if (!self || !buf) {
            LOG_FATAL("SocketSend: NULL argument");
    i64 SocketSend(Socket *self, const void *buf, size n) {
        if (!self || !buf) {
            LOG_FATAL("SocketSend: NULL argument");
        }
        return plat_send(self->fd, buf, n);
    static void proxy_pump(Socket *a, Socket *b, Zstr first_chunk, size first_len) {
        if (first_chunk && first_len > 0) {
            if (SocketSend(b, first_chunk, first_len) < 0) {
                return;
            }
                    return;
                }
                if (SocketSend(b, buf, (size)n) < 0) {
                    return;
                }
                    return;
                }
                if (SocketSend(a, buf, (size)n) < 0) {
                    return;
                }
        Zstr payload = "hello from socket test";
        size n       = (size)ZstrLen(payload);
        if (SocketSend(&client, payload, n) != (i64)n) {
            SocketClose(&server);
            SocketClose(&client);
    
        Zstr payload = "X";
        bool sent    = SocketSend(&p.client, payload, 1) == 1;
    
        SocketPollItem item   = {0};
    
        // Server has data waiting, but we ask only about WRITE on the client.
        bool sent = SocketSend(&p.client, "Y", 1) == 1;
    
        SocketPollItem item   = {0};
        }
    
        bool sent = SocketSend(&p.client, "Z", 1) == 1;
    
        SocketPollItem item   = {0};
    
        // Make server0 and server2 readable; server1 stays idle.
        bool sent = SocketSend(&p0.client, "A", 1) == 1 && SocketSend(&p2.client, "C", 1) == 1;
    
        // Loopback delivery is asynchronous (notably on macOS): SocketSend
        }
    
        bool sent = SocketSend(&p1.client, "M", 1) == 1;
    
        SocketPollItem items[3]   = {0};
        if (ok) {
            // Make only the last descriptor readable.
            ok = SocketSend(&pairs[N - 1].client, "L", 1) == 1;
    
            SocketPollItem items[N] = {0};
        // Zero-byte send returns exactly 0 (kill lt_to_le on plat_send's
        // `ret < 0`: a `<= 0` would treat the legit 0 return as an error).
        ok = ok && SocketSend(&client, "", 0) == 0;
    
        // client -> server
        Zstr c2s = "client=>server:ABCDEFG";
        size n1  = (size)ZstrLen(c2s);
        ok       = ok && SocketSend(&client, c2s, n1) == (i64)n1;
        char buf1[64];
        i64  g1 = SocketRecv(&server, buf1, sizeof(buf1));
        Zstr s2c = "server=>client:0123456789";
        size n2  = (size)ZstrLen(s2c);
        ok       = ok && SocketSend(&server, s2c, n2) == (i64)n2;
        char buf2[64];
        i64  g2 = SocketRecv(&client, buf2, sizeof(buf2));
        Zstr payload = "poke";
        size n       = (size)ZstrLen(payload);
        ok           = ok && SocketSend(&client, payload, n) == (i64)n;
    
        SocketPollItem ready   = {0};
        Zstr payload = "recv-timeout-set";
        size n       = (size)ZstrLen(payload);
        ok           = ok && SocketSend(&client, payload, n) == (i64)n;
        char buf[32];
        i64  got = SocketRecv(&server, buf, sizeof(buf));
        Zstr payload = "after-send-timeout";
        size n       = (size)ZstrLen(payload);
        ok           = ok && SocketSend(&client, payload, n) == (i64)n;
        char buf[32];
        i64  got = SocketRecv(&server, buf, sizeof(buf));
        Zstr payload = "opts-ok";
        size n       = (size)ZstrLen(payload);
        ok           = ok && SocketSend(&client, payload, n) == (i64)n;
        char buf[16];
        i64  got = SocketRecv(&server, buf, sizeof(buf));
Last updated on