Question

I want to compile some markdown posts thorough netcat. Here is Makefile.

# Makefile

all: $(POSTS)

$(POST_DEST_DIR)/%.html: $(POST_SRC_DIR)/%.md | $(POST_DEST_DIR)
  @nc localhost 3000 < $< > $@
  @echo 'compiled $@'

.DELETE_ON_ERROR: $(POSTS)

When TCP server exit with error, nc exit without error while Node.js nc wrapper exit with error. Here is a Node.js wrapper script.

// nc.js

var client = require('net').connect(3000);
process.stdin.pipe(client);
client.pipe(process.stdout);
client.on('error', function (err) {
  console.error(err.message);
  process.exit(1);
});

An then

# Makefile with nc.js

$(POST_DEST_DIR)/%.html: $(POST_SRC_DIR)/%.md | $(POST_DEST_DIR)
  @node nc.js < $< > $@
  @echo 'compiled $@'

The TCP server is also written in NodeJS. I want nc to exit with error when TCP server crashes in order to stop make process immediately.

Here is a TCP server for test.

// tcp server for error test

require('net').createServer(function(socket) {
  process.exit(1);
}).listen(3000);

I've read nc man page. But I found it's impossible to do what I want to do. Am I missing something?

Was it helpful?

Solution

TCP doesn't have any concept of the remote server "crashing". When a program with an open TCP socket ends (regardless of how), the OS will close the socket.

In order to do what you want, you would need to create a protocol where the server would acknowledge completion of whatever operation you want it to perform and then the client would exit with failure unless it got that application-layer acknowledgement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top