-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathsimpleSocketServer.js
More file actions
31 lines (26 loc) · 973 Bytes
/
simpleSocketServer.js
File metadata and controls
31 lines (26 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
A simple TCP chat server in node.js.
Minimal code for listening for clients.
Prints out what the clients say.
Echoes back to each client.
created 22 Oct 2018
by Tom Igoe
*/
// make an instance of the net library:
const net = require('net');
// create the server:
var server = net.createServer(listenForClients);
// start the server listening:
server.listen(8080);
// This function is called every time a new client connects:
function listenForClients(client) {
console.log('client connected at ' + new Date());
console.log('client: ' + client.remoteAddress);
// client.setEncoding('utf8'); // encode everything sent by the client as a string
client.write('hello'); // send the client a hello message
function readIncoming(data) {
console.log(client.remoteAddress + ': ' + data); // print what the client said
}
// client event handler. runs if there's incoming socket data:
client.on('data', readIncoming);
}