TCP, UDP
TCP, UDP, HTTP, HTTPS
Node.js中的TCP
實作
var net = require('net');
var HOST = 'localhost';
var PORT = 8000;
var client = new net.Socket(); //建立一個新的socket實例
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
client.write('hello,this is from client!');//發送給server數據
const repl = require('repl');
var test = repl.start('請輸入: ').context;
test.hello = function() {
client.write('client說了hello!');
}
//之後啟動client後輸入hello()
});
client.on('data', function(data) {
console.log('DATA: ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});Node.js中的UDP
實作
UDP廣播 (需要兩台以上電腦在同一個區網內才可測試)
但IPv6 不支援廣播,只支援群播(multicasting),所以可將程式碼改為如下
Last updated