connection_status
connection_status(): int
Returns the connection status bitmask: CONNECTION_NORMAL(0)/CONNECTION_ABORTED(1)/CONNECTION_TIMEOUT(2), combinable bitwise (ABORTED|TIMEOUT=3). In Web scenarios it detects client disconnects; CLI has no HTTP client connection, so it is always CONNECTION_NORMAL - an honest environment-independent demo. Pairs with ignore_user_abort() as the connection-handling teaching closure.
C C23
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
// C has no "connection status" API: you detect a peer disconnect at recv()
// (recv returns 0 = orderly EOF when the peer closed the connection)
int main(void) {
int sv[2];
socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
const char *msg = "ping";
send(sv[0], msg, strlen(msg), 0);
char buf[16];
ssize_t n = recv(sv[1], buf, sizeof buf, 0);
printf("got %zd bytes\n", n);
close(sv[0]); /* peer goes away */
n = recv(sv[1], buf, sizeof buf, 0);
printf("peer disconnect detected: %s\n", n == 0 ? "true" : "false");
close(sv[1]);
return 0;
}
got 4 bytes peer disconnect detected: true
C++ C++26
#include <cstdio>
#include <cstring>
#include <sys/socket.h>
#include <unistd.h>
// C++ has no connection-status API either: recv() returning 0 is the disconnect signal
int main() {
int sv[2];
socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
const char *msg = "ping";
send(sv[0], msg, strlen(msg), 0);
char buf[16];
ssize_t n = recv(sv[1], buf, sizeof buf, 0);
std::printf("got %zd bytes\n", n);
close(sv[0]); // peer goes away
n = recv(sv[1], buf, sizeof buf, 0);
std::printf("peer disconnect detected: %s\n", n == 0 ? "true" : "false");
close(sv[1]);
return 0;
}
got 4 bytes peer disconnect detected: true
C# v11.0.100
// C#: no connection-status API in the language; ASP.NET exposes RequestAborted
// (a CancellationToken) - the demo models the same two states offline
using System.Threading;
var cts = new CancellationTokenSource(); // models a live request
Console.WriteLine(cts.IsCancellationRequested); // CONNECTION_NORMAL equivalent
cts.Cancel(); // models the client disconnecting
Console.WriteLine(cts.IsCancellationRequested); // CONNECTION_ABORTED equivalent
False True
Go v1.27.4
package main
import (
"fmt"
"net/http"
"net/http/httptest"
)
// Go: the per-request context carries the disconnect notification (r.Context().Done())
func main() {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "http://example.com/", nil)
handler := func(w http.ResponseWriter, r *http.Request) {
select {
case <-r.Context().Done():
fmt.Println("client disconnected")
default:
fmt.Println("connection normal")
}
}
handler(rec, req)
}
connection normal
Java v27.0.1
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class Main {
// Java has no direct connection-status API: a stream read returning -1
// (or the request's abort notification) is the disconnect signal
public static void main(String[] args) throws Exception {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
out.write("ping".getBytes());
byte[] buf = new byte[16];
int n = in.read(buf);
System.out.println("got " + n + " bytes");
out.close(); // peer goes away
n = in.read(buf);
System.out.println("peer disconnect detected: " + (n == -1));
}
}
got 4 bytes peer disconnect detected: true
JavaScript v26.13.1
// JS has no socket-level disconnect flag in the language core: servers surface it
// as the request's "close" event; the core primitive that models it is AbortSignal
const ac = new AbortController();
console.log(ac.signal.aborted); // live request: CONNECTION_NORMAL equivalent
ac.abort(); // models the client disconnecting
console.log(ac.signal.aborted); // CONNECTION_ABORTED equivalent
false true
PHP v8.6.0
// connection_status(): bitmask of CONNECTION_NORMAL(0)/ABORTED(1)/TIMEOUT(2)
// CLI has no HTTP client connection -> always CONNECTION_NORMAL (environment-independent)
var_dump(connection_status());
var_dump(connection_status() === CONNECTION_NORMAL);
var_dump(CONNECTION_ABORTED, CONNECTION_TIMEOUT);
int(0) bool(true) int(1) int(2)
Python v3.15.2
# Python stdlib: no connection-status API - recv() returning b"" is the disconnect signal
import socket
a, b = socket.socketpair()
a.sendall(b"ping")
print(f"got {len(b.recv(16))} bytes")
a.close() # peer goes away
print("peer disconnect detected:", b.recv(16) == b"")
b.close()
got 4 bytes peer disconnect detected: True
Rust v1.99.1
// Rust std has no sockets and no connection-status API:
// the disconnect signal exists only where the framework owns the socket
const CONNECTION_NORMAL: u8 = 0;
const CONNECTION_ABORTED: u8 = 1;
const CONNECTION_TIMEOUT: u8 = 2;
fn describe(status: u8) -> &'static str {
if status & CONNECTION_ABORTED != 0 {
"aborted"
} else if status & CONNECTION_TIMEOUT != 0 {
"timeout"
} else {
"normal"
}
}
fn main() {
let status: u8 = CONNECTION_NORMAL; // no client attached in a bare binary
println!("{}", describe(status));
println!("{}", describe(CONNECTION_ABORTED | CONNECTION_TIMEOUT));
}
normal aborted
TypeScript v26.13.1
// TS: same runtime as JS; the abort signal typed as AbortSignal
const ac: AbortController = new AbortController();
console.log(ac.signal.aborted); // live request: CONNECTION_NORMAL equivalent
ac.abort(); // models the client disconnecting
console.log(ac.signal.aborted); // CONNECTION_ABORTED equivalent
false true
Language differences
C: no connection-status API - a peer disconnect surfaces at the TCP layer as recv returning 0 (orderly EOF) or -1 (error); the demo shows it offline with socketpair; the same applies to C++.
C++: same as C, only socket primitives at the language layer.
C#: no language-layer API; ASP.NET's HttpContext.RequestAborted (a CancellationToken) is the framework-level analogue - a client disconnect fires the cancellation.
Go: the most direct analogue - r.Context().Done() closes when the client disconnects; servers should select on it to stop expensive work.
Java: no direct analogue - Servlet 3.0's AsyncContext or container-specific mechanisms report disconnects; at the stream layer a read returning -1 is the signal.
JavaScript: no socket-level flag in the language core; Node's HTTP server exposes disconnects as the request's "close" event, and AbortController is the language-level modeling primitive.
PHP: always CONNECTION_NORMAL(0) under CLI (no HTTP client connection - environment-independent fact); the bitmask ranges 0-3 with ABORTED|TIMEOUT=3; an important practical note from the manual's user notes: the status cannot be detected inside a loop that outputs nothing - you must first send output to the browser (flush); ignore_user_abort() (not deprecated) decides whether the script keeps running after a disconnect, connection_aborted() is its boolean shortcut, and together with the manual's "Connection handling" chapter they form the full teaching chain.
Python: no connection-status API - a recv returning b"" is peer EOF; WSGI frameworks have their own conventions.
Rust: no sockets in std - the disconnect signal only exists at the framework layer (e.g. tokio/axum's rx.closed()).
TypeScript: same as JavaScript.
- General conclusion: only the layer that OWNS the socket can answer "is the client still there"; PHP answers with a bitmask function, Go with a context, and the other languages answer at the framework layer.