3 - Share a file over TCP.

Let's go a bit more low level and share a file using just a tcp server. Write a tcp server that shares a file to any client that connects to it.

Also, write a client that connects to it and saves the server's response to a file.

Tips

To create TCP servers we need the net node core module.

You can create TCP servers like this.

var server = net.createServer(function (socket) {
  pump(someStream(), socket)
})

server.listen(3000)

To connect to a running TCP server you will need the net.connect function, which connects to the server and returns a stream.

var stream = net.connect(3000, 'localhost')

To create a writable stream to a file checkout the fs.createWriteStream function.

To save the file with a temporary name, you can define the filename like this:

var downloadedFilename = 'file-' + Date.now()

Testing

To test your solution first run the server.

node server.js

then the client.

node client.js

And check that a file was written with the content of the file you're sharing on the server.

When you are done click here to go to the next exercise