mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.5 KiB
74 lines
1.5 KiB
15 years ago
|
require("../common");
|
||
15 years ago
|
tcp = require("tcp");
|
||
15 years ago
|
|
||
|
binaryString = "";
|
||
|
for (var i = 255; i >= 0; i--) {
|
||
|
var s = "'\\" + i.toString(8) + "'";
|
||
|
S = eval(s);
|
||
15 years ago
|
error( s
|
||
|
+ " "
|
||
|
+ JSON.stringify(S)
|
||
|
+ " "
|
||
|
+ JSON.stringify(String.fromCharCode(i))
|
||
|
+ " "
|
||
|
+ S.charCodeAt(0)
|
||
|
);
|
||
15 years ago
|
process.assert(S.charCodeAt(0) == i);
|
||
|
process.assert(S == String.fromCharCode(i));
|
||
15 years ago
|
binaryString += S;
|
||
|
}
|
||
|
|
||
15 years ago
|
var echoServer = tcp.createServer(function (connection) {
|
||
15 years ago
|
connection.setEncoding("binary");
|
||
15 years ago
|
connection.addListener("data", function (chunk) {
|
||
15 years ago
|
error("recved: " + JSON.stringify(chunk));
|
||
15 years ago
|
connection.write(chunk, "binary");
|
||
15 years ago
|
});
|
||
15 years ago
|
connection.addListener("end", function () {
|
||
15 years ago
|
connection.end();
|
||
15 years ago
|
});
|
||
|
});
|
||
|
echoServer.listen(PORT);
|
||
|
|
||
|
var recv = "";
|
||
|
var j = 0;
|
||
|
|
||
15 years ago
|
var c = tcp.createConnection(PORT);
|
||
15 years ago
|
|
||
|
c.setEncoding("binary");
|
||
15 years ago
|
c.addListener("data", function (chunk) {
|
||
15 years ago
|
if (j < 256) {
|
||
15 years ago
|
error("write " + j);
|
||
|
c.write(String.fromCharCode(j), "binary");
|
||
15 years ago
|
j++;
|
||
|
} else {
|
||
15 years ago
|
c.end();
|
||
15 years ago
|
}
|
||
|
recv += chunk;
|
||
|
});
|
||
|
|
||
|
c.addListener("connect", function () {
|
||
15 years ago
|
c.write(binaryString, "binary");
|
||
15 years ago
|
});
|
||
|
|
||
|
c.addListener("close", function () {
|
||
|
p(recv);
|
||
|
echoServer.close();
|
||
|
});
|
||
|
|
||
|
process.addListener("exit", function () {
|
||
|
puts("recv: " + JSON.stringify(recv));
|
||
|
|
||
15 years ago
|
assert.equal(2*256, recv.length);
|
||
15 years ago
|
|
||
|
var a = recv.split("");
|
||
|
|
||
|
var first = a.slice(0,256).reverse().join("");
|
||
|
puts("first: " + JSON.stringify(first));
|
||
|
|
||
|
var second = a.slice(256,2*256).join("");
|
||
|
puts("second: " + JSON.stringify(second));
|
||
|
|
||
15 years ago
|
assert.equal(first, second);
|
||
15 years ago
|
});
|