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.

113 lines
3.3 KiB

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
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);
});