2010-12-04 15:20:34 -08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var net = require('net');
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-04 16:11:57 -08:00
|
|
|
var binaryString = "";
|
2009-09-21 12:27:22 +02:00
|
|
|
for (var i = 255; i >= 0; i--) {
|
|
|
|
var s = "'\\" + i.toString(8) + "'";
|
2010-12-04 16:11:57 -08:00
|
|
|
var S = eval(s);
|
2010-07-15 11:47:25 -07:00
|
|
|
common.error( s
|
2009-09-28 12:06:30 +02:00
|
|
|
+ " "
|
|
|
|
+ JSON.stringify(S)
|
|
|
|
+ " "
|
2010-06-29 23:12:46 -07:00
|
|
|
+ JSON.stringify(String.fromCharCode(i))
|
2009-09-28 12:06:30 +02:00
|
|
|
+ " "
|
|
|
|
+ S.charCodeAt(0)
|
|
|
|
);
|
2009-10-29 23:34:10 +01:00
|
|
|
process.assert(S.charCodeAt(0) == i);
|
|
|
|
process.assert(S == String.fromCharCode(i));
|
2009-09-21 12:27:22 +02:00
|
|
|
binaryString += S;
|
|
|
|
}
|
|
|
|
|
2010-08-27 15:38:46 -07:00
|
|
|
// safe constructor
|
|
|
|
var echoServer = net.Server(function (connection) {
|
2009-09-21 12:27:22 +02:00
|
|
|
connection.setEncoding("binary");
|
2010-02-12 00:25:15 -08:00
|
|
|
connection.addListener("data", function (chunk) {
|
2010-07-15 11:47:25 -07:00
|
|
|
common.error("recved: " + JSON.stringify(chunk));
|
2010-02-16 13:15:30 -08:00
|
|
|
connection.write(chunk, "binary");
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
2010-02-12 00:25:15 -08:00
|
|
|
connection.addListener("end", function () {
|
2010-04-08 10:44:22 -07:00
|
|
|
connection.end();
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
|
|
|
});
|
2010-07-15 11:47:25 -07:00
|
|
|
echoServer.listen(common.PORT);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
|
|
|
var recv = "";
|
|
|
|
|
2010-08-12 01:38:42 +02:00
|
|
|
echoServer.addListener("listening", function() {
|
|
|
|
var j = 0;
|
2010-08-27 15:38:46 -07:00
|
|
|
var c = net.createConnection(common.PORT);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-08-12 01:38:42 +02:00
|
|
|
c.setEncoding("binary");
|
|
|
|
c.addListener("data", function (chunk) {
|
|
|
|
if (j < 256) {
|
|
|
|
common.error("write " + j);
|
|
|
|
c.write(String.fromCharCode(j), "binary");
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
c.end();
|
|
|
|
}
|
|
|
|
recv += chunk;
|
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-08-12 01:38:42 +02:00
|
|
|
c.addListener("connect", function () {
|
|
|
|
c.write(binaryString, "binary");
|
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-08-12 01:38:42 +02:00
|
|
|
c.addListener("close", function () {
|
2010-10-12 10:09:02 +11:00
|
|
|
console.dir(recv);
|
2010-08-12 01:38:42 +02:00
|
|
|
echoServer.close();
|
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
process.addListener("exit", function () {
|
2010-06-23 17:40:51 -07:00
|
|
|
console.log("recv: " + JSON.stringify(recv));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(2*256, recv.length);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
|
|
|
var a = recv.split("");
|
|
|
|
|
|
|
|
var first = a.slice(0,256).reverse().join("");
|
2010-06-23 17:40:51 -07:00
|
|
|
console.log("first: " + JSON.stringify(first));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
|
|
|
var second = a.slice(256,2*256).join("");
|
2010-06-23 17:40:51 -07:00
|
|
|
console.log("second: " + JSON.stringify(second));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(first, second);
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|