2010-02-25 14:36:17 +08:00
|
|
|
process.mixin(require("../common"));
|
2009-11-01 02:02:30 +08:00
|
|
|
tcp = require("tcp");
|
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
|
|
|
|
2009-09-28 22:08:09 +08:00
|
|
|
var server = tcp.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 () {
|
2009-08-27 00:22:00 +08:00
|
|
|
socket.close();
|
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-02-27 04:06:32 +08:00
|
|
|
server.listen(PORT);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
2010-02-27 04:06:32 +08:00
|
|
|
var client = tcp.createConnection(PORT);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
|
|
|
client.setEncoding("UTF8");
|
|
|
|
|
|
|
|
client.addListener("connect", function () {
|
|
|
|
puts("client connected.");
|
|
|
|
});
|
|
|
|
|
2010-02-12 16:25:15 +08:00
|
|
|
client.addListener("data", function (chunk) {
|
2009-08-27 00:22:00 +08:00
|
|
|
client_recv_count += 1;
|
|
|
|
puts("client_recv_count " + client_recv_count);
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal("hello\r\n", chunk);
|
2009-08-27 00:22:00 +08:00
|
|
|
client.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
client.addListener("close", function (had_error) {
|
|
|
|
puts("disconnect");
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(false, had_error);
|
2009-08-27 04:03:19 +08:00
|
|
|
if (disconnect_count++ < N)
|
2010-02-27 04:06:32 +08:00
|
|
|
client.connect(PORT); // reconnect
|
2009-08-27 00:22:00 +08:00
|
|
|
else
|
|
|
|
server.close();
|
|
|
|
});
|
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
|
|
|
});
|