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.

93 lines
2.2 KiB

/* eslint-disable strict */
14 years ago
var common = require('../common');
var assert = require('assert');
var net = require('net');
14 years ago
var binaryString = '';
for (var i = 255; i >= 0; i--) {
14 years ago
var s = '\'\\' + i.toString(8) + '\'';
14 years ago
var S = eval(s);
14 years ago
common.error(s +
' ' +
JSON.stringify(S) +
' ' +
JSON.stringify(String.fromCharCode(i)) +
' ' +
S.charCodeAt(0));
assert.ok(S.charCodeAt(0) == i);
assert.ok(S == String.fromCharCode(i));
binaryString += S;
}
// safe constructor
14 years ago
var echoServer = net.Server(function(connection) {
console.error('SERVER got connection');
14 years ago
connection.setEncoding('binary');
connection.on('data', function(chunk) {
common.error('SERVER recved: ' + JSON.stringify(chunk));
14 years ago
connection.write(chunk, 'binary');
});
connection.on('end', function() {
console.error('SERVER ending');
connection.end();
});
});
echoServer.listen(common.PORT);
14 years ago
var recv = '';
echoServer.on('listening', function() {
console.error('SERVER listening');
var j = 0;
var c = net.createConnection({
port: common.PORT
});
14 years ago
c.setEncoding('binary');
c.on('data', function(chunk) {
console.error('CLIENT data %j', chunk);
var n = j + chunk.length;
while (j < n && j < 256) {
common.error('CLIENT write ' + j);
14 years ago
c.write(String.fromCharCode(j), 'binary');
j++;
}
if (j === 256) {
console.error('CLIENT ending');
c.end();
}
recv += chunk;
});
c.on('connect', function() {
console.error('CLIENT connected, writing');
14 years ago
c.write(binaryString, 'binary');
});
c.on('close', function() {
console.error('CLIENT closed');
console.dir(recv);
echoServer.close();
});
c.on('finish', function() {
console.error('CLIENT finished');
});
});
process.on('exit', function() {
14 years ago
console.log('recv: ' + JSON.stringify(recv));
14 years ago
assert.equal(2 * 256, recv.length);
14 years ago
var a = recv.split('');
14 years ago
var first = a.slice(0, 256).reverse().join('');
console.log('first: ' + JSON.stringify(first));
14 years ago
var second = a.slice(256, 2 * 256).join('');
console.log('second: ' + JSON.stringify(second));
assert.equal(first, second);
});