|
| 1 | +--- |
| 2 | +title: Net模块和DNS模块 |
| 3 | +layout: page |
| 4 | +category: nodejs |
| 5 | +date: 2015-03-17 |
| 6 | +modifiedOn: 2015-03-17 |
| 7 | +--- |
| 8 | + |
| 9 | +net模块用于底层的网络通信。 |
| 10 | + |
| 11 | +## 服务器端Socket接口 |
| 12 | + |
| 13 | +下面代码打开一个服务器端Socket接口,用来接受客户端的数据。 |
| 14 | + |
| 15 | +```javascript |
| 16 | + |
| 17 | +var serverPort = 9099; |
| 18 | +var net = require('net'); |
| 19 | +var server = net.createServer(function(client) { |
| 20 | + console.log('client connected'); |
| 21 | + console.log('client IP Address: ' + client.remoteAddress); |
| 22 | + console.log('is IPv6: ' + net.isIPv6(client.remoteAddress)); |
| 23 | + console.log('total server connections: ' + server.connections); |
| 24 | + |
| 25 | + // Waiting for data from the client. |
| 26 | + client.on('data', function(data) { |
| 27 | + console.log('received data: ' + data.toString()); |
| 28 | + |
| 29 | + // Write data to the client socket. |
| 30 | + client.write('hello from server'); |
| 31 | + }); |
| 32 | + |
| 33 | + // Closed socket event from the client. |
| 34 | + client.on('end', function() { |
| 35 | + console.log('client disconnected'); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +server.on('error',function(err){ |
| 40 | + console.log(err); |
| 41 | + server.close(); |
| 42 | +}); |
| 43 | + |
| 44 | +server.listen(serverPort, function() { |
| 45 | + console.log('server started on port ' + serverPort); |
| 46 | +}); |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +上面代码中,createServer方法建立了一个服务端,一旦收到客户端发送的数据,就发出回应,同时还监听客户端是否中断通信。最后,listen方法打开服务端。 |
| 51 | + |
| 52 | +## 客户端Socket接口 |
| 53 | + |
| 54 | + |
| 55 | +客户端Socket接口用来向服务器发送数据。 |
| 56 | + |
| 57 | +```javascript |
| 58 | + |
| 59 | +var serverPort = 9099; |
| 60 | +var server = 'localhost'; |
| 61 | +var net = require('net'); |
| 62 | + |
| 63 | +console.log('connecting to server...'); |
| 64 | +var client = net.connect({server:server,port:serverPort},function(){ |
| 65 | + console.log('client connected'); |
| 66 | + |
| 67 | + // send data |
| 68 | + console.log('send data to server'); |
| 69 | + client.write('greeting from client socket'); |
| 70 | +}); |
| 71 | + |
| 72 | +client.on('data', function(data) { |
| 73 | + console.log('received data: ' + data.toString()); |
| 74 | + client.end(); |
| 75 | +}); |
| 76 | + |
| 77 | +client.on('error',function(err){ |
| 78 | + console.log(err); |
| 79 | +}); |
| 80 | +client.on('end', function() { |
| 81 | + console.log('client disconnected'); |
| 82 | +}); |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +上面代码连接服务器之后,就向服务器发送数据,然后监听服务器返回的数据。 |
| 87 | + |
| 88 | +## DNS模块 |
| 89 | + |
| 90 | +DNS模块用于解析域名。resolve4方法用于IPv4环境,resolve6方法用于IPv6环境,lookup方法在以上两种环境都可以使用,返回IP地址(address)和当前环境(IPv4或IPv6)。 |
| 91 | + |
| 92 | +```javascript |
| 93 | + |
| 94 | +var dns = require('dns'); |
| 95 | + |
| 96 | +dns.resolve4('www.pecollege.net', function (err, addresses) { |
| 97 | + if (err) |
| 98 | + console.log(err); |
| 99 | + |
| 100 | + console.log('addresses: ' + JSON.stringify(addresses)); |
| 101 | +}); |
| 102 | + |
| 103 | +dns.lookup('www.pecollege.net', function (err, address, family) { |
| 104 | + if (err) |
| 105 | + console.log(err); |
| 106 | + |
| 107 | + console.log('addresses: ' + JSON.stringify(address)); |
| 108 | + console.log('family: ' + JSON.stringify(family)); |
| 109 | +}); |
| 110 | + |
| 111 | +``` |
0 commit comments