tcp.listen(0, function() { const socket = net.Stream({ highWaterMark: 0 }); console.log('Connecting to socket '); socket.connect(this.address().port, function() { console.log('socket connected'); connectHappened = true; }); console.log(`connecting = ${socket.connecting}`); assert.strictEqual('opening', socket.readyState); // Make sure that anything besides a buffer or a string throws. [null, true, false, undefined, 1, 1.0, 1 / 0, +Infinity, -Infinity, [], {} ].forEach(function(v) { function f() { console.error('write', v); socket.write(v); } assert.throws(f, TypeError); }); // Write a string that contains a multi-byte character sequence to test that // `bytesWritten` is incremented with the # of bytes, not # of characters. const a = "L'État, c'est "; const b = 'moi'; // We're still connecting at this point so the datagram is first pushed onto // the connect queue. Make sure that it's not added to `bytesWritten` again // when the actual write happens. const r = socket.write(a, function(er) { console.error('write cb'); dataWritten = true; assert.ok(connectHappened); console.error('socket.bytesWritten', socket.bytesWritten); //assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length); console.error('data written'); }); console.error('socket.bytesWritten', socket.bytesWritten); console.error('write returned', r); assert.strictEqual(socket.bytesWritten, Buffer.from(a).length); assert.strictEqual(false, r); socket.end(b); assert.strictEqual('opening', socket.readyState); });
server = net.createServer(function (c) { var vnc = net.Stream(); c.pipe(vnc, {end: false}); vnc.pipe(c); vnc.on('close', function (had_error) { // XXX we need to be able to restart the vnc if this happens, // but the only case should be when the VM is shutoff, so // we wouldn't be able to reconnect anyway. VM.log('INFO', 'vnc closed for ' + vmobj.uuid); clearVNC(vmobj.uuid); }); vnc.on('end', function (had_error) { // XXX we need to be able to restart the vnc if this happens, // but the only case should be when the VM is shutoff, so // we wouldn't be able to reconnect anyway. VM.log('INFO', 'vnc ended for ' + vmobj.uuid); clearVNC(vmobj.uuid); }); vnc.on('error', function () { VM.log('WARN', 'Warning: VNC socket error: ' + JSON.stringify(arguments)); clearVNC(vmobj.uuid); }); vnc.connect(zonepath + '/root/tmp/vm.vnc'); });
var web = http.Server(function(req, res) { web.close(); console.log(req.headers); var socket = net.Stream(); socket.connect(tcpPort); socket.on('connect', function() { console.log('socket connected'); }); req.pipe(socket); req.on('end', function() { res.writeHead(200); res.write('thanks'); res.end(); console.log('response with \'thanks\''); }); req.connection.on('error', function(e) { console.log('http server-side error: ' + e.message); process.exit(1); }); });
getexchange(domain, function (err, exchange) { if (err) return cb(err); var client = new events.EventEmitter(); client.stream = net.Stream(); client.stream.host = domain; client.stream.post = 25; client.stream.on('connect', function () { client.kick(); }) client.waiting = false; client.eof = '\r\n' client.stream.on('data', function (chunk) { }) client.queue = [['HELO '+exchange, function (err) {if (err) cb(err)}]]; client.currentCallback = function (code, line) { if (code > 299) return cb(new Error({code:code, message:line})); client.kick(); } client.kick = function () { var c = client.queue.shift(); client.stream.write(c[0]) client.currentCallback = function (code, line) { if (c[1]) c[1](null, code, line); client.kick(); } } })
tcp.listen(common.PORT, function() { var socket = net.Stream(); console.log('Connecting to socket '); socket.connect(tcpPort, function() { console.log('socket connected'); connectHappened = true; }); console.log('_connecting = ' + socket._connecting); assert.equal('opening', socket.readyState); // Make sure that anything besides a buffer or a string throws. [ null, true, false, undefined, 1, 1.0, 1 / 0, +Infinity -Infinity, [], {} ].forEach(function(v) { function f() { socket.write(v); } assert.throws(f, TypeError); }); // Write a string that contains a multi-byte character sequence to test that // `bytesWritten` is incremented with the # of bytes, not # of characters. var a = "L'État, c'est "; var b = "moi"; // We're still connecting at this point so the datagram is first pushed onto // the connect queue. Make sure that it's not added to `bytesWritten` again // when the actual write happens. var r = socket.write(a, function() { dataWritten = true; assert.ok(connectHappened); assert.equal(socket.bytesWritten, Buffer(a + b).length); console.error('data written'); }); assert.equal(socket.bytesWritten, Buffer(a).length); assert.equal(false, r); socket.end(b); assert.equal('opening', socket.readyState); });
server = net.createServer(function (c) { var vnc = net.Stream(); c.pipe(vnc, {end: false}); vnc.pipe(c); vnc.on('close', function (had_error) { // XXX we need to be able to restart the vnc if this happens, // but the only case should be when the VM is shutoff, so // we wouldn't be able to reconnect anyway. VM.log('INFO', 'vnc closed for ' + vmobj.uuid); clearVNC(vmobj.uuid); }); vnc.on('end', function (had_error) { // XXX we need to be able to restart the vnc if this happens, // but the only case should be when the VM is shutoff, so // we wouldn't be able to reconnect anyway. VM.log('INFO', 'vnc ended for ' + vmobj.uuid); clearVNC(vmobj.uuid); if (vmobj.hasOwnProperty('vnc_password') && vmobj.vnc_password.length > 0) { // if we are using VNC passwords then the connection ends for an // incorrect password, so we do need to respawn here, otherwise // we'll be unable to reconnect. We reload first so we skip // respawn if VM is not running. VM.load(vmobj.uuid, function (e, obj) { if (e) { VM.log('ERROR', 'Unable to reload VM ' + vmobj.uuid, e); } else { spawnVNC(obj); VM.log('INFO', 'respawned VNC for VM ' + obj.uuid); } }); } }); vnc.on('error', function () { VM.log('WARN', 'Warning: VNC socket error: ' + JSON.stringify(arguments)); clearVNC(vmobj.uuid); }); vnc.connect(zonepath + '/root/tmp/vm.vnc'); });
function startClient () { var socket = net.Stream(); console.log("Connecting to socket"); socket.connect(tcpPort); socket.on('connect', function () { console.log('socket connected'); }); assert.equal("opening", socket.readyState); assert.equal(false, socket.write("foo")); socket.end("bar"); assert.equal("opening", socket.readyState); }
process.nextTick(function() { var s = net.Stream(); var gotConnected = false; s.connect(9000); s.on('connect', function() { gotConnected = true; connections++; connect(); }); s.on('close', function() { if (gotConnected) connections--; }); s.on('error', function() { errors++; }); });
const web = http.Server(common.mustCall((req, res) => { web.close(); const socket = net.Stream(); socket.connect(tcpPort); socket.on('connect', common.mustCall(() => {})); req.pipe(socket); req.on('end', common.mustCall(() => { res.writeHead(200); res.write('thanks'); res.end(); })); req.connection.on('error', (e) => { assert.ifError(e); }); }));
Clients.prototype.tcpClient = function(options) { var self = this; var socket; options = mixin({ socket : function() { self.emit('socket', socket) }, close : function() { self.emit('close', socket); self.ifaces[socket.id] = socket; }, open : function() { self.ifaces[socket.id] = socket; } }, options); socket = net.Stream().on('connect', options.socket).on('close', options.close); socket.connect(options.port, options.host, options.open) socket = new Socket({ socket : socket }) }
tcp.listen(common.PORT, function () { var socket = net.Stream(); console.log('Connecting to socket'); socket.connect(tcpPort, function() { console.log('socket connected'); connectHappened = true; }); assert.equal('opening', socket.readyState); var r = socket.write('foo', function () { fooWritten = true; assert.ok(connectHappened); console.error("foo written"); }); assert.equal(false, r); socket.end('bar'); assert.equal('opening', socket.readyState); });
server = net.createServer(function (c) { var dpy = net.Stream(); var remote_address = ''; c.pipe(dpy); dpy.pipe(c); remote_address = '[' + c.remoteAddress + ']:' + c.remotePort; c.on('close', function (had_error) { log.info(protocol + ' connection ended from ' + remote_address); }); dpy.on('error', function () { log.warn('Warning: ' + protocol + ' socket error: ' + JSON.stringify(arguments)); }); c.on('error', function () { log.warn('Warning: ' + protocol + ' net socket error: ' + JSON.stringify(arguments)); }); dpy.connect(path.join(zonepath, sockpath)); });