Skip to content

Commit 07e0b96

Browse files
committed
new udp
1 parent 9a3beb7 commit 07e0b96

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

19-udp/190-UDP-Server.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
const dgram = require('dgram')
22
const server = dgram.createSocket('udp4');
3-
3+
44
server.on('message', (msg, rinfo) => {
55
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
6-
//we can send something back.
6+
//we can send something back to the destination
7+
//we write a udp datagram and set the destination ip/ port
8+
//notice it is called send and not write, because it is a whole message
9+
server.send("hello client!", rinfo.port, rinfo.address);
10+
711
});
812

913
server.on('listening', () => {
1014
const address = server.address();
1115
console.log(`server listening ${address.address}:${address.port}`);
1216
});
1317

14-
server.bind(3333);
18+
19+
server.bind(3333);
20+
//use this to send a sample udp message
21+
echo "Hello UDP" | nc -u 127.0.0.1 3333
22+
1523
// Prints: server listening 0.0.0.0:3333

19-udp/191-UDP-Client.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
const dgram = require('dgram')
1+
const dgram = require('dgram');
2+
const { isTypedArray } = require('util/types');
23
const client = dgram.createSocket('udp4');
34

45
client.on('message', (msg, rinfo) => {
56
console.log(`client got: ${msg} from ${rinfo.address}:${rinfo.port}`);
67
});
8+
//this automatically binds to a random port, (can't send anything otherwise)
9+
client.send("hello server", 3333, "127.0.0.1")
710

8-
//optional but we can bind to a random port of our choice
9-
client.bind(55555);
10-
11-
client.connect( 3333,"127.0.0.1", () => {
12-
console.log ("server exists .. send something.. ")
13-
//notice that its called send and not write
14-
//because it is not a stream of bytes ..but an actual unit (message) we send
15-
client.send("a whole message");
16-
});
11+
//optionally bind to a source port of your choosing
12+
//if you don't do that, kernel will pick one for you
13+
//client.bind(55555)
1714

15+
//technically if you know the clients bind source port anyone can send stuff to it !
16+
//try it
17+
////echo "Hello UDP" | nc -u 127.0.0.1 randomport
18+
client.on("error", (err) => console.log(err) )
19+
//a bind was successful!
1820
client.on('listening', () => {
1921
const address = client.address();
2022
//this will keep reading

19-udp/192-UDP-Client-Persisted.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const dgram = require('dgram')
2+
const client = dgram.createSocket('udp4');
3+
4+
client.on('message', (msg, rinfo) => {
5+
console.log(`client got: ${msg} from ${rinfo.address}:${rinfo.port}`);
6+
});
7+
8+
//optional but we can bind to a random port of our choice
9+
//client.bind(55555);
10+
11+
client.connect( 3333,"127.0.0.1", () => {
12+
console.log ("server exists .. send something.. ")
13+
//notice that its called send and not write
14+
//because it is not a stream of bytes ..but an actual unit (message) we send
15+
client.send("a whole message");
16+
});
17+
18+
client.on("error", (err) => console.log(err) )
19+
client.on('listening', () => {
20+
const address = client.address();
21+
//this will keep reading
22+
console.log(`client listening ${address.address}:${address.port}`);
23+
});
24+

0 commit comments

Comments
 (0)