TFTP Client and Server

0.1.2 · abandoned · verified Sun Apr 19

This module provides a full-featured streaming TFTP (Trivial File Transfer Protocol) client and server for Node.js. It supports several TFTP RFCs (1350, 2347, 2348, 2349, 3617, 7440) and de facto extensions like rollover and windowsize, which address TFTP's inherent 32MB file size limit and slow lock-step transfer mechanism. The current stable version is 0.1.2. Developed for Node.js versions 0.10 and newer, it integrates with Node.js streams for easy file transfers. However, the README clearly states that TFTP is an obsolete legacy protocol, generally unsuitable for internet transfers due to UDP packet loss, and recommends FTP for most use cases. Its primary differentiation is providing a complete TFTP solution within Node.js, including performance-boosting extensions, despite the protocol's limitations. The package does not appear to have an active release cadence, with the last known update being in 2015.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a basic TFTP server to handle GET and PUT requests and a client to download a file from it. It showcases the streaming interface for file transfers and includes basic error handling for both server and client operations. To run this, create 'server_files' and 'client_downloads' directories, and place a 'remote_file.txt' in 'server_files'.

const tftp = require('tftp');
const fs = require('fs');

// --- TFTP Server Example ---
const server = tftp.createServer();
server.on('error', (err) => {
  console.error('Server error:', err.message);
});
server.on('get', (req, respond) => {
  console.log(`Server received GET request for: ${req.file} from ${req.remoteAddress}:${req.remotePort}`);
  const filePath = `./server_files/${req.file}`;
  fs.access(filePath, fs.constants.F_OK, (err) => {
    if (err) {
      console.error(`File not found: ${filePath}`);
      return req.error('File not found');
    }
    respond(fs.createReadStream(filePath));
  });
});
server.on('put', (req, readStream) => {
  console.log(`Server received PUT request for: ${req.file} from ${req.remoteAddress}:${req.remotePort}`);
  const filePath = `./server_files/${req.file}`;
  const writeStream = fs.createWriteStream(filePath);
  readStream.pipe(writeStream);
  readStream.on('end', () => {
    console.log(`File ${req.file} saved on server.`);
  });
  readStream.on('error', (err) => {
    console.error(`Error saving file ${req.file} on server:`, err.message);
  });
});
server.listen(69, () => {
  console.log('TFTP server listening on port 69...');
});

// --- TFTP Client Example (after server is running) ---
// Ensure 'test_file.txt' exists in a 'client_files' directory or similar for PUT
// Ensure 'server_files/remote_file.txt' exists for GET

// To run this example:
// 1. Create a directory named 'server_files'
// 2. Place a file named 'remote_file.txt' inside 'server_files'
// 3. Create a directory named 'client_downloads'

const client = tftp.createClient();
const remoteHost = '127.0.0.1';

// Client GET operation
const getRequest = client.get('remote_file.txt', { remoteAddress: remoteHost, remotePort: 69 });
const downloadPath = './client_downloads/downloaded_file.txt';
const writeStream = fs.createWriteStream(downloadPath);

getRequest.on('error', (err) => {
  console.error('Client GET error:', err.message);
  process.exit(1);
});
getRequest.on('timeout', () => {
  console.error('Client GET timed out.');
  process.exit(1);
});
getRequest.on('end', () => {
  console.log(`File downloaded to ${downloadPath}`);
});
getRequest.pipe(writeStream);

// // Client PUT operation (uncomment to test PUT)
// const uploadPath = './client_files/upload_me.txt'; // Make sure this file exists
// const putRequest = client.put('uploaded_file.txt', { remoteAddress: remoteHost, remotePort: 69 });
// const readStream = fs.createReadStream(uploadPath);

// putRequest.on('error', (err) => {
//   console.error('Client PUT error:', err.message);
//   process.exit(1);
// });
// putRequest.on('timeout', () => {
//   console.error('Client PUT timed out.');
//   process.exit(1);
// });
// putRequest.on('end', () => {
//   console.log(`File uploaded from ${uploadPath}`);
// });
// readStream.pipe(putRequest);

view raw JSON →