100%(2)2 out of 2 people found this document helpful
This preview shows page 76 - 79 out of 142 pages.
What can be the datagram maximum size?It really depends on the network it travels through. The UDP header allows up to 65535 bytes ofdata, but if you are sending a packet across an Ethernet network, for instance, the Ethernet MTUis 1500 bytes, limiting the maximum datagram size. Also, some routers will attempt to fragment alarge UDP packet into 512 byte chunks.UDP ExercisesExercise 1Create a UDP server that echoes the messages it receives back into the origin socket.
Child processesOn Node you can spawn child processes, which can be another Node process or any process youcan launch from the command line. For that you will have to provide the command and argumentsto execute it. You can either spawn and live along side the process (spawn), or you can wait until itexits (exec).Executing commandsYou can then launch another process and wait for it to finish like this:1varexec=require('child_process').exec;23exec('cat *.js wc -l',function(err, stdout, stderr) {4if(err) {5console.log('child process exited with error code '+err.code);6return;7}8console.log(stdout);9});Here on line 3 we are passing in “cat *.js wc -l” as the command, the first argument to theexecinvokation. We are then passing as the second argument a callback function that will be invokedonce the exec has finished.If the child process returned an error code, the first argument of the callback will contain an instanceof Error, with the code property set to the child exit code.If not, the output of stdout and stderr will be collected and be offered to us as strings.You can also pass an optionaloptionsargument between the command and the callback functionlike this:1varoptions={timeout: 10000};2exec('cat *.js wc -l', options,function(err, stdout, stderr) {3//...4});The available options are:
Child processes67•encoding: the expected encoding for the child output. Defaults to ‘utf8’;•timeout: the timeout in milliseconds for the execution of the command. Defaults to 0, whichdoes not timeout;•maxBuffer: specifies the maximum size of the output allowed on stdout or stderr. If exceeded,the child is killed. Defaults to 200 * 1024;•killSignal: the signal to be sent to the child if it times out or exceeds the output buffers.Identified as a string;•cwd: current working directory;•env: environment variables to be passed into the child process. Defaults to null.On thekillSignaloption you can pass a string identifying the name of the signal you wish to sendto the target process. Signals are identified in node as strings. For a complete list of strings type onyour shell:1$man signalScroll down, and you will see a list of constants representing the signals. Those are the strings usedin Node.Spawning processesYou can spawn a new child process based on thechild_process.spawnfunction like this:1varspawn=require('child_process').spawn;23varchild=spawn('tail',['-f','/var/log/system.log']);4child.stdout.on('data',function(data) {5console.log('stdout: '+data);6});Here we are spawning a child process to run the “tail” command, passing in as arguments “-f” and“/var/log/system.log”. This “tail” command will monitor the file “/var/log/system.log” - if it exists -