-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (45 loc) · 1.39 KB
/
server.js
File metadata and controls
58 lines (45 loc) · 1.39 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var dgram = require('dgram');
var Server = function () {
// create socket
this.socket = dgram.createSocket('udp4');
// config
this.port = 3000;
this.host = '127.0.0.1';
var that = this;
// start listening
this.socket.on('listening', function() {
var address = that.socket.address();
console.log('UDP Server listening on ' + address.address + ':' + address.port);
});
// wait for message
this.socket.on('message', function(msg) {
var operator = msg[0],
operand1 = msg[1],
operand2 = msg[2];
console.log();
console.log("ERGEBNIS:");
// convert operator and calculate result
switch(operator) {
case 1:
console.log(operand1 + operand2);
break;
case 2:
console.log(operand1 - operand2);
break;
case 3:
console.log(operand1 / operand2)
break;
case 4:
console.log(operand1 * operand2);
break;
default:
console.log("ERROR: undefined operation");
}
// close socket after calculation
that.socket.close();
})
// bind socket on given port and host
this.socket.bind(this.port, this.host);
}
// export server as module
module.exports = Server;