javascript - Node.js file transfer using UDP - Stack Overflow

admin2025-04-20  0

I'm trying to create a simple Node.js server that receives files from clients via UDP. The problem I'm having is that every time I try to transmit a large file (anything over 100kb), the server doesn't seem to respond. So far I've been successful at transmitting files up to 50kb.

Is there any way to resolve this issue?

Client Code:

var PORT = 33333;
var HOST = 'localhost';
var dgram = require('dgram');
var log = require('sys').log;
var client = dgram.createSocket('udp4');
var fs = require("fs");

fs.readFile('C:\\test.pdf', function (err,data) {
  if (err) {
    return console.log(err);
  }
  client.send(data, 0, data.length, PORT, HOST, function(err, bytes) {
    if (err) 
        throw err;
    log('UDP file sent to ' + HOST +':'+ PORT);
    log('File sise: ' + data.length);
  });
});

Server Code:

var PORT = 33333;
var HOST = 'localhost';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
var fs = require("fs");
var log = require('sys').log;
var wstream = fs.createWriteStream('test.pdf');

wstream.on('finish', function () {
  console.log('file has been written');
});

server.on('message', function (message, remote) {
    wstream.write(message);
    wstream.end();
});

server.bind(PORT, HOST);

I'm trying to create a simple Node.js server that receives files from clients via UDP. The problem I'm having is that every time I try to transmit a large file (anything over 100kb), the server doesn't seem to respond. So far I've been successful at transmitting files up to 50kb.

Is there any way to resolve this issue?

Client Code:

var PORT = 33333;
var HOST = 'localhost';
var dgram = require('dgram');
var log = require('sys').log;
var client = dgram.createSocket('udp4');
var fs = require("fs");

fs.readFile('C:\\test.pdf', function (err,data) {
  if (err) {
    return console.log(err);
  }
  client.send(data, 0, data.length, PORT, HOST, function(err, bytes) {
    if (err) 
        throw err;
    log('UDP file sent to ' + HOST +':'+ PORT);
    log('File sise: ' + data.length);
  });
});

Server Code:

var PORT = 33333;
var HOST = 'localhost';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
var fs = require("fs");
var log = require('sys').log;
var wstream = fs.createWriteStream('test.pdf');

wstream.on('finish', function () {
  console.log('file has been written');
});

server.on('message', function (message, remote) {
    wstream.write(message);
    wstream.end();
});

server.bind(PORT, HOST);
Share Improve this question asked Jan 8, 2015 at 1:35 Tito RahmanTito Rahman 1072 silver badges9 bronze badges 3
  • 2 Transferring a file like that over UDP is a bad idea IMHO. You can miss packets with UDP, meaning you could end up with an inplete file. Why not use TCP? – mscdex Commented Jan 8, 2015 at 1:47
  • Definitely don't use UDP for this if you can help it. In addition to dropped packets, it's very mon for packets to arrive out-of-order. TCP solves all of this for you. – Brad Commented Jan 8, 2015 at 2:53
  • I know that TCP is better and using something else other than UDP is the way to go. I was just curious as to what caused the problem and how to solve it. – Tito Rahman Commented Jan 8, 2015 at 21:03
Add a ment  | 

1 Answer 1

Reset to default 7

From the dgram docs:

The Payload Length field is 16 bits wide, which means that a normal payload cannot be larger than 64K octets including internet header and data (65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header); this is generally true for loopback interfaces, but such long datagrams are impractical for most hosts and networks.

You can't send datagrams of more than 65,507 bytes. That's a hard limit on UDP. It sounds like you should be using a higher-level protocol for these files.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149672a287544.html

最新回复(0)