2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-12-05 06:45:52 +08:00
|
|
|
|
2010-12-05 07:20:34 +08:00
|
|
|
var net = require('net');
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
var N = 50;
|
2009-08-14 18:51:46 +08:00
|
|
|
var c = 0;
|
2009-06-09 01:10:23 +08:00
|
|
|
var client_recv_count = 0;
|
2009-08-14 18:51:46 +08:00
|
|
|
var disconnect_count = 0;
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
var server = net.createServer(function(socket) {
|
|
|
|
socket.addListener('connect', function() {
|
|
|
|
socket.write('hello\r\n');
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
socket.addListener('end', function() {
|
2010-04-09 01:44:22 +08:00
|
|
|
socket.end();
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
socket.addListener('close', function(had_error) {
|
|
|
|
//console.log('server had_error: ' + JSON.stringify(had_error));
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, had_error);
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
});
|
2010-03-10 08:27:49 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
server.listen(common.PORT, function() {
|
2010-06-24 08:40:51 +08:00
|
|
|
console.log('listening');
|
2010-07-16 02:47:25 +08:00
|
|
|
var client = net.createConnection(common.PORT);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
client.setEncoding('UTF8');
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
client.addListener('connect', function() {
|
|
|
|
console.log('client connected.');
|
2010-03-10 08:27:49 +08:00
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
client.addListener('data', function(chunk) {
|
2010-03-10 08:27:49 +08:00
|
|
|
client_recv_count += 1;
|
2010-12-05 06:45:52 +08:00
|
|
|
console.log('client_recv_count ' + client_recv_count);
|
|
|
|
assert.equal('hello\r\n', chunk);
|
2010-04-09 01:44:22 +08:00
|
|
|
client.end();
|
2010-03-10 08:27:49 +08:00
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
client.addListener('close', function(had_error) {
|
|
|
|
console.log('disconnect');
|
2010-03-10 08:27:49 +08:00
|
|
|
assert.equal(false, had_error);
|
|
|
|
if (disconnect_count++ < N)
|
2010-07-16 02:47:25 +08:00
|
|
|
client.connect(common.PORT); // reconnect
|
2010-03-10 08:27:49 +08:00
|
|
|
else
|
|
|
|
server.close();
|
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
});
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
process.addListener('exit', function() {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(N+1, disconnect_count);
|
|
|
|
assert.equal(N+1, client_recv_count);
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|
2010-03-10 08:27:49 +08:00
|
|
|
|