4866664 [rkeene@sledge /home/rkeene/tmp]$ cat -n edonjs.js
 1 #! /usr/bin/env edonjs
 2 
 3 /*
 4  * JavaScript using the Tcl runtime:
 5  */
 6 server = runtime.socket('-server', function(sock, addr, port) {
 7     console.info("New connect from", addr, port, "as", sock);
 8     runtime.fileevent(sock, 'readable', function() {
 9         var line, e;
10 
11         try {
12             line = runtime.gets(sock);
13             if (runtime.eof(sock) && line === '') {
14                 console.info("EOF on sock", sock, "from", addr, port);
15 
16                 runtime.close(sock);
17                 return;
18             }
19         
20             console.debug("New data from", sock, ":", line);
21         } catch (e) {
22             console.error("Error processing sock", sock, ":", e);
23             console.info("Closing sock", sock, "from", addr, port);
24             runtime.close(sock);
25         }
26     });
27 }, 9313);
28 
29 
30 /*
31  * Pure Tcl version:
32  *
33  * set server [socket -server {apply {{sock addr port} {
34  *     puts "I> New connect from $addr $port as $sock"
35  *     fileevent $sock readable [list apply {{sock addr port} {
36  *         try {
37  *             set line [gets $sock]
38  *             if {[eof $sock] && $line eq ""} {
39  *                 puts "I> EOF on sock $sock from $addr $port"
40  *
41  *                 close $sock
42  *                 return
43  *             }
44  *
45  *             puts "D> New data from $sock : $line"
46  *         } on error e {
47  *             puts "E> Error processing sock $sock : $e"
48  *             puts "I> Closing sock $sock from $addr $port"
49  *             close $sock
50  *         }
51  *     }} $sock $addr $port]
52  * }}} 9313]
53  */
4866665 [rkeene@sledge /home/rkeene/tmp]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2019-06-14 01:32:40