2010-03-09 07:06:29 +08:00
|
|
|
require("../common");
|
2010-03-10 08:27:49 +08:00
|
|
|
net = require('net');
|
2009-06-09 01:10:23 +08:00
|
|
|
var N = 50;
|
2009-05-20 03:53:26 +08:00
|
|
|
|
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-03-10 08:27:49 +08:00
|
|
|
var server = net.createServer(function (socket) {
|
2009-08-27 00:22:00 +08:00
|
|
|
socket.addListener("connect", function () {
|
2010-02-17 05:15:30 +08:00
|
|
|
socket.write("hello\r\n");
|
2009-06-28 02:40:43 +08:00
|
|
|
});
|
2009-05-20 03:53:26 +08:00
|
|
|
|
2010-02-12 16:25:15 +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
|
|
|
|
2009-08-27 00:22:00 +08:00
|
|
|
socket.addListener("close", function (had_error) {
|
|
|
|
//puts("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-02-27 04:06:32 +08:00
|
|
|
server.listen(PORT);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
server.addListener('listening', function () {
|
|
|
|
puts('listening');
|
|
|
|
var client = net.createConnection(PORT);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
client.setEncoding("UTF8");
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
client.addListener("connect", function () {
|
|
|
|
puts("client connected.");
|
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-03-10 08:27:49 +08:00
|
|
|
client.addListener("data", function (chunk) {
|
|
|
|
client_recv_count += 1;
|
|
|
|
puts("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-03-10 08:27:49 +08:00
|
|
|
client.addListener("close", function (had_error) {
|
|
|
|
puts("disconnect");
|
|
|
|
assert.equal(false, had_error);
|
|
|
|
if (disconnect_count++ < N)
|
|
|
|
client.connect(PORT); // reconnect
|
|
|
|
else
|
|
|
|
server.close();
|
|
|
|
});
|
2009-08-27 00:22:00 +08:00
|
|
|
});
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2009-08-27 00:51:04 +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
|
|
|
|