Created
December 23, 2012 16:38
-
-
Save nkzawa/4364208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.HOST = process.argv[2] || '127.0.0.1'; | |
exports.PORT = process.argv[3] || 11111; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var environment = require('./environment'), | |
server = require('./tcp_echo_server'), | |
createClient = require('./tcp_echo_client').createClient, | |
HOST = environment.HOST, | |
PORT = environment.PORT; | |
server.on('connection', function (socket) { | |
server.close(); | |
socket.end(); | |
}); | |
server.listen(PORT, HOST, function () { | |
createClient(PORT, HOST); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var environment = require('./environment'), | |
server = require('./tcp_echo_server'), | |
createClient = require('./tcp_echo_client').createClient, | |
HOST = environment.HOST, | |
PORT = environment.PORT, | |
MAX_CONNECTIONS = 10, | |
count = 1000; | |
function _createClient() { | |
var client = createClient(PORT, HOST); | |
client.on('close', function (error) { | |
if (count) { | |
_createClient(); | |
} | |
}); | |
} | |
server.maxConnections = MAX_CONNECTIONS; | |
server.on('connection', function (socket) { | |
if (!--count) { | |
server.close(); | |
} | |
socket.end(); | |
}); | |
server.listen(PORT, HOST, function () { | |
for (var i = 0; i < MAX_CONNECTIONS; i++) { | |
_createClient(); | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'), | |
readline = require('readline'), | |
environment = require('./environment'), | |
clients = {}, | |
rl; | |
function createClient(port, host) { | |
var options = { | |
host: host || environment.HOST, | |
port: port || environment.PORT, | |
toString: function () { | |
return this.host + ':' + this.port; | |
} | |
}, | |
client = net.connect(options), | |
key; | |
client.on('error', function (e) { | |
console.error('Connection Failed - ' + options); | |
console.error(e.message); | |
}); | |
client.on('connect', function () { | |
var addr = client.address(); | |
console.log('Connected - ' + options); | |
key = addr.address + ':' + addr.port; | |
clients[key] = client; | |
}); | |
client.on('data', function (chunk) { | |
process.stdout.write(chunk.toString()); | |
}); | |
client.on('end', function (error) { | |
console.log('Connection End - ' + options); | |
delete clients[key]; | |
}); | |
client.on('close', function (error) { | |
console.log('Client Closed'); | |
}); | |
return client; | |
} | |
exports.createClient = createClient; | |
rl = readline.createInterface(process.stdin, process.stdout); | |
rl.on('SIGINT', function () { | |
for (key in clients) { | |
clients[key].end(); | |
} | |
rl.close(); | |
}); | |
function main() { | |
var client = createClient(), | |
i = 0; | |
client.setTimeout(1000); | |
client.on('timeout', function () { | |
var str = i + ': Hello World\n'; | |
process.stdout.write('[S] ' + str); | |
client.write(str); | |
i++; | |
}); | |
client.on('end', function (error) { | |
client.setTimeout(0); | |
}); | |
} | |
if (require.main === module) { | |
main(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'), | |
readline = require('readline'), | |
server = net.createServer(), | |
clients = {}, | |
rl; | |
module.exports = server; | |
server.maxConnections = 3; | |
function connectionStatus() { | |
return server.connections + '/' + server.maxConnections; | |
} | |
function Client(socket) { | |
this.socket = socket; | |
} | |
Client.key = function (socket) { | |
return socket.remoteAddress + ':' + socket.remotePort; | |
}; | |
Client.prototype.writeData = function (data) { | |
var socket = this.socket; | |
if (socket.writable) { | |
process.stdout.write('[' + this.key + '] - ' + data); | |
socket.write('[R] ' + data); | |
} | |
}; | |
Object.defineProperty(Client.prototype, 'key', { | |
get: function () { | |
return this.constructor.key(this.socket); | |
} | |
}); | |
server.on('listening', function () { | |
var addr = server.address(); | |
console.log('Listening Start on Server - ' + addr.address + ':' + addr.port); | |
}); | |
server.on('connection', function (socket) { | |
var client = new Client(socket), | |
key = client.key, | |
status = connectionStatus(); | |
console.log('Connection Start(' + status + ') - ' + key); | |
clients[key] = client; | |
}); | |
server.on('connection', function (socket) { | |
var key = Client.key(socket), | |
data = '', | |
newline = /\r\n|\n/; | |
socket.on('data', function (chunk) { | |
data += chunk.toString(); | |
if (newline.test(data)) { | |
clients[key].writeData(data); | |
data = ''; | |
} | |
}); | |
}); | |
server.on('connection', function (socket) { | |
var key = Client.key(socket); | |
socket.on('end', function () { | |
var status = connectionStatus(); | |
console.log('Connection End(' + status + ') - ' + key); | |
delete clients[key]; | |
}); | |
}); | |
server.on('close', function () { | |
console.log('Server Closed'); | |
rl.close(); | |
}); | |
rl = readline.createInterface(process.stdin, process.stdout); | |
rl.on('SIGINT', function () { | |
var socket, key; | |
// stop accepting new clients first | |
server.close(); | |
for (key in clients) { | |
socket = clients[key].socket; | |
socket.end(); | |
} | |
rl.close(); | |
}); | |
function main() { | |
var environment = require('./environment'); | |
server.listen(environment.PORT, environment.HOST); | |
} | |
if (require.main === module) { | |
main(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment