Explainer
Why a C TCP socket cannot open a Node-RED WebSocket session
Short answer
A TCP connect to Node-RED on port 1880 proves the socket layer only. The WebSocket session opens after the C client sends an HTTP opening handshake that requests the endpoint path with the Upgrade headers, and payloads must then ride in masked protocol frames. Configure a C WebSocket library with host, port and path; appending the path to the address passed to inet_addr cannot work.

On this page
- What the connect call proves and what it does not
- Why the path cannot travel in the address
- The opening handshake your library sends
- What the test payload must look like on the wire
- Configure the listener and prove the path
- Replies, closure and the raw TCP alternative
- The decision for each case
- Questions asked
The connect call in the C program opens a raw TCP stream to Node-RED on port 1880, and that is all it does. A WebSocket session on the same port exists only after the client sends an HTTP opening handshake that requests the endpoint path — here /pathofsocket — with the Upgrade headers the WebSocket standard requires, and every payload must then travel inside a masked protocol frame instead of as bare bytes. That is why the working address ws://myipaddr:1880/pathofsocket cannot be reproduced by appending /pathofsocket to the string handed to inet_addr: the socket address structure holds an IPv4 address and a port and nothing else. The fix is a C WebSocket client library configured with the host, the port and the path, sending SensorDataTest as a text message once the library reports the session open.
What the connect call proves and what it does not #
The listing performs none of the WebSocket work. Every line of it is the operating system's plain socket interface: socket creates a TCP socket, connect completes the three-way TCP handshake, and send and recv move raw bytes in both directions.
"Connected" therefore confirms exactly one fact: a process accepted a TCP connection on that port. Node-RED's runtime serves the editor and any WebSocket listener a flow registers on the same port, which is 1880 in this setup and is the runtime's default, so something was listening — but the connection record, the sockaddr_in structure, carries only sin_family, sin_addr and sin_port. Family, address, port. There is no field for a path, a protocol switch or a subprotocol, so the structure cannot express a WebSocket session at all.
Then send writes the test string straight onto the stream. On a WebSocket endpoint the first bytes on the connection must form an HTTP request line, and a bare string is not one, so the listener reads the connection as a malformed request and the session never comes into existence.
The same shape — TCP healthy, application session absent — is common on the plant floor. The article MQTT connect fails when TCP works: diagnose the handshake works through the MQTT version of it, and the two-layer rule is not specific to Node-RED: an MQTT publisher must also address a broker endpoint rather than a bare socket, which is why MQTT communication requires a broker endpoint.
Why the path cannot travel in the address #
inet_addr parses one thing: a dotted-quad IPv4 string such as 192.168.1.50. Append a slash and a path and the function rejects the whole string and returns INADDR_NONE, whose bit pattern 0xFFFFFFFF is the address 255.255.255.255 — a broadcast address, not the controller. The program would then reach nothing at all.
The working address packs three layers into one string, and the C socket carries only two of them:
| Piece of the address | Layer | Where the C program carries it |
|---|---|---|
| the host | IP routing | sin_addr, set from inet_addr |
| the port | TCP service selection | sin_port, set with htons |
| the path | HTTP resource requested after connect | nowhere — it belongs in the handshake request line |
The kernel routes packets by address and port alone. The path exists only inside the first HTTP request written to the established stream, and that request is what a WebSocket library builds from its host, port and path settings. It is never a property of the socket address.
The opening handshake your library sends #
The WebSocket protocol, IETF standard RFC 6455, defines the session as an HTTP/1.1 upgrade. The client sends a GET for the endpoint path with an Upgrade header naming websocket, a Connection header carrying Upgrade, a Sec-WebSocket-Key of 16 random bytes base64-encoded into 24 characters, and Sec-WebSocket-Version set to 13. With the values from this setup the request reads as follows; the key shown is the standard's own published example value:
GET /pathofsocket HTTP/1.1
Host: myipaddr:1880
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
The server answers with HTTP status 101 Switching Protocols and a Sec-WebSocket-Accept header: the base64 encoding of the SHA-1 digest of the client key concatenated with the standard's fixed GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11. For the example key above, RFC 6455 documents the accept value s3pPLMBiTxaQ9kYGzzhZRbK+xOo=. From the 101 onward the same TCP stream carries WebSocket frames instead of HTTP.
A C client library such as libwebsockets runs this exchange for you. The hand-rolled send of a plain string performs none of it, and that is the whole gap between "Connected" and an open session.
What the test payload must look like on the wire #
RFC 6455 fixes the frame format too. A client-to-server frame carries a first byte holding the final-fragment bit and an opcode (text is 0x1), a second byte holding the mask bit and the payload length, a 32-bit masking key, and the payload XORed byte by byte with that key. Clients must mask: the standard demands it of every client frame.
Worked example, for the payload SensorDataTest, which is 14 bytes:
- byte 0: final text frame, so 0x80 + 0x01 = 0x81
- byte 1: mask bit set, length 14 fits the 7-bit length field, so 0x80 + 0x0E = 0x8E
- bytes 2 to 5: the 4-byte masking key
- bytes 6 to 19: the payload bytes, each XORed with the key byte at index position modulo 4
Total on the wire: 1 + 1 + 4 + 14 = 20 bytes. The 7-bit length field covers payloads up to 125 bytes; a length field of 126 introduces a 2-byte extended length for payloads up to 65,535 bytes, and 127 introduces an 8-byte extended length beyond that. This payload fits the simple form.
The raw send call writes the payload alone — no opcode byte, no mask bit, no key — so the listener cannot parse it as a frame. The library adds the header and the mask. A successful library send can also mean the bytes are queued inside the library rather than delivered, so keep servicing its event loop and follow its documented send semantics.
Configure the listener and prove the path #
On the Node-RED side, open the websocket-in node and confirm it listens rather than connects: a node set to the connect-to role is itself a client and will never accept your C program. Check that its path is exactly /pathofsocket, wire it to a debug node, deploy, and look for listener errors in the flow.
One safety note: for first tests, keep that listener wired to debug output only — a flow that forwards an untested client's messages into device writes can actuate plant equipment from your bench.
Prove the path with the JavaScript client first, sending a recognizable message to the same address; the debug sidebar must show it. Only then connect the C client: configure the library with host myipaddr, port 1880 and path /pathofsocket, run its event loop until the connection-established callback fires, send SensorDataTest through the library's text-message API, and keep servicing events until the write completes. The proof is the exact string in the debug sidebar, not the "Connected" line. Libraries take the endpoint in different forms: some accept one complete URL, others separate host, port, path and security fields — with separate fields, include the leading slash and match the deployed path byte for byte.
Replies, closure and the raw TCP alternative #
If the debug sidebar shows the payload but the C program still hangs, the fault is on the reply path: a websocket-in node wired only to a debug node consumes the message and writes nothing back, so the immediate recv blocks even though delivery worked. To receive an answer, add a websocket-out node configured to reply on the same session and route the answer into it.
A socket that stays connected while no application data moves is the signature of a session layer that never formed. The article why a gateway shows a TCP connection but sends no payload documents exactly that symptom on a Node-RED-driven gateway.
When bytes do come back, respect the count the library reports and terminate text before printing it, and treat connection closure as closure rather than as a short reply.
Raw TCP is a legitimate design, but it is a different endpoint: Node-RED's TCP nodes listen on their own configured port, speak no HTTP, and leave message boundaries to your application protocol. Such a listener will never serve the WebSocket address the JavaScript client uses. For the existing path-based endpoint, keep WebSocket and replace the raw socket exchange with library calls.
The decision for each case #
If the endpoint already serves the JavaScript client at its WebSocket address, implement the C side with a WebSocket client library — host, port 1880, path /pathofsocket — wait for its session-open event, and send SensorDataTest as a text frame. Treat that event, not the return of connect, as the success signal, and never put the path in the address string.
If you control both ends and raw bytes are the actual requirement, deploy a TCP listener node on its own port, define message boundaries in the application protocol, and drop the WebSocket address entirely.
If the C program must receive answers, the flow needs a websocket-out node replying on the session; without one, remove the recv and treat the client as one-way.
Questions asked
What status does the server return when the WebSocket handshake succeeds?
Node-RED answers with HTTP status 101 Switching Protocols plus a Sec-WebSocket-Accept header derived from the key your client sent. A 400 Bad Request points at a missing or malformed upgrade header, and a 404 Not Found points at a path that does not match the deployed listener. Frames flow only after the 101 arrives on that connection.
Does the masking rule apply to replies from Node-RED as well?
No. The standard requires a 32-bit masking key on every client-to-server frame and forbids masks on server-to-client frames. A server that receives an unmasked client frame must close the connection, which looks exactly like a session that dies right after a successful handshake. A maintained library handles both directions automatically.
Can the C program send plain TCP to Node-RED instead of WebSocket?
Yes, but not to this listener. Node-RED's TCP nodes accept raw streams on their own configured port, and TCP itself has no message boundaries, so the two ends must agree on a framing rule such as a length prefix or a delimiter byte. The WebSocket URL and its path stop applying, and the two listeners can coexist in one flow.
How do you tell a successful TCP connect from an open WebSocket session?
The connect call returns as soon as the operating system completes the TCP handshake, before any HTTP is exchanged. A WebSocket library reports the session open only after its upgrade request has been answered with the 101 response. Treat the library's connection-established callback as the send-ready signal and treat the return of connect as network reachability only.

