0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test-net-server.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

process.Buffer.prototype.toString = function () {
return this.utf8Slice(0, this.length);
};
var sys = require("sys");
var net = require("./lib/net");
var server = new net.Server(function (socket) {
sys.puts("connection (" + socket.fd + "): "
+ socket.remoteAddress
+ " port "
+ socket.remotePort
);
sys.puts("server fd: " + server.fd);
socket.addListener("data", function (b) {
socket.send("pong ascii\r\n", "ascii");
socket.send(b);
socket.send("pong utf8\r\n", "utf8");
});
2009-12-17 09:31:10 +01:00
socket.addListener("eof", function () {
sys.puts("server peer eof");
socket.close();
2009-12-18 14:52:02 +01:00
});
socket.addListener('drain', function () {
sys.puts("server-side socket drain");
2009-12-17 09:31:10 +01:00
});
});
2009-12-30 09:57:55 +01:00
server.listen(8000);
sys.puts("server fd: " + server.fd);
2009-12-17 09:31:10 +01:00
var c = net.createConnection(8000, "localhost");
2009-12-18 14:52:02 +01:00
c.addListener('connect', function () {
2009-12-17 09:31:10 +01:00
sys.puts("!!!client connected");
2009-12-18 14:52:02 +01:00
c.send("hello\n");
2009-12-17 09:31:10 +01:00
});
2009-12-18 14:52:02 +01:00
c.addListener('drain', function () {
sys.puts("!!!client drain");
2009-12-17 09:31:10 +01:00
});
c.addListener('data', function (d) {
2009-12-18 14:52:02 +01:00
sys.puts("!!!client got: " + JSON.stringify(d.toString()));
c.close();
2009-12-18 14:52:02 +01:00
});
2009-12-17 09:31:10 +01:00
c.addListener('dataEnd', function (d) {
sys.puts("!!!client eof");
});