模块 net
该模块为使用 TCP 和 UDP socket的网络提供支持。 socket表示客户端和服务器程序之间通过网络的连接。 底层的本地绑定是由 java.net 包提供的。
Example
// A simple TCP server
var io = require('io');
var net = require('net');
var server = new net.ServerSocket();
server.bind('127.0.0.1', 6789);
var socket = server.accept();
var stream = new io.TextStream(socket.getStream(), {
  'charset': 'US-ASCII'
});
var line;
do {
  // Read one line from the client
  line = stream.readLine();
  console.log(line);
  // Write back to the client
  stream.writeLine("Received: " + line);
} while (line.indexOf("END") < 0);
stream.close();
socket.close();
server.close();
Class DatagramSocket
Instance Methods
- bind(host, port)
 - close()
 - connect(host, port)
 - disconnect()
 - getTimeout()
 - isBound()
 - isClosed()
 - isConnected()
 - localAddress()
 - receive(length, buffer)
 - receiveFrom(length, buffer)
 - remoteAddress()
 - send(data)
 - sendTo(host, port, data)
 - setTimeout(timeout)
 
Class ServerSocket
Instance Methods
- accept()
 - bind(host, port)
 - close()
 - getTimeout()
 - isBound()
 - isClosed()
 - localAddress()
 - setTimeout(timeout)
 
Class Socket
Instance Methods
- bind(host, port)
 - close()
 - connect(host, port, [timeout])
 - getStream()
 - getTimeout()
 - isBound()
 - isClosed()
 - isConnected()
 - localAddress()
 - remoteAddress()
 - setTimeout(timeout)
 
DatagramSocket ()
DatagramSocket类用于创建一个UDP socket。
DatagramSocket.prototype. bind (host, port)
将socket绑定到本地地址和端口。 如果地址或端口被省略,系统将选择本地地址和端口来绑定socket.
Parameters
| String | host | address (interface) to which the socket will be bound.  | 
                
| Number | port | port number to bind the socket to.  | 
                
DatagramSocket.prototype. close ()
立即关闭socket
DatagramSocket.prototype. connect (host, port)
将socket连接到远程地址。 如果DatagramSocket已连接,则它只能将数据发送到指定地址并从指定地址接收数据。 默认情况下DatagramSockets没有连接。
Parameters
| String | host | IP address or hostname  | 
                
| Number | port | port number or service name  | 
                
DatagramSocket.prototype. disconnect ()
断开socket。
DatagramSocket.prototype. getTimeout ()
返回此 DatagramSocket 的当前超时。值为零意味着禁用了超时,即 receive() 永远不会超时。
Returns
| Number | the current timeout  | 
                
DatagramSocket.prototype. isBound ()
返回这个socket是否绑定到一个地址。
Returns
| Boolean | true if the socket has been bound to an address  | 
                
DatagramSocket.prototype. isClosed ()
返回socket是否关闭。
Returns
| Boolean | true if the socket has been closed  | 
                
DatagramSocket.prototype. isConnected ()
返回socket是否连接。
Returns
| Boolean | true if the socket has been connected to a remote address  | 
                
DatagramSocket.prototype. localAddress ()
获取此socket绑定的本地地址。这返回具有包含作为字符串的 IP 地址的属性地址的对象以及包含端口号的属性端口,例如
{address: '127.0.0.1', port: 8080}。
Returns
| Object | an address descriptor  | 
                
DatagramSocket.prototype. receive (length, buffer)
从此socket接收数据报数据包。此方法不会返回发件人的 IP 地址,所以它应该与 connect()一起使用。
Parameters
| Number | length | the maximum number of bytes to receive  | 
                
| ByteArray | buffer | optional buffer to store bytes in  | 
                
Returns
| ByteArray | the received data  | 
                
DatagramSocket.prototype. receiveFrom (length, buffer)
从此socket接收数据报数据包。此方法返回具有以下属性的对象:
- address: 发件人的IP地址作为字符串
 - port: 发件人的端口号
 - data: 收到的数据
 
Parameters
| Number | length | the maximum number of bytes to receive  | 
                
| ByteArray | buffer | optional buffer to store bytes in  | 
                
Returns
| Object | the received packet  | 
                
DatagramSocket.prototype. remoteAddress ()
获取此socket连接的远程地址。这返回具有包含作为字符串的 IP 地址的属性地址的对象以及包含端口号的属性端口,例如
{address: '127.0.0.1', port: 8080}.
Returns
| Object | an address descriptor  | 
                
DatagramSocket.prototype. send (data)
从这个socket发送一个数据报包。此方法不允许指定收件人的IP地址,所以它应该与 connect() 一起使用。
Parameters
| Binary | data | the data to send  | 
                
DatagramSocket.prototype. sendTo (host, port, data)
从这个socket发送一个数据报包到指定的地址。
Parameters
| String | host | the IP address of the recipient  | 
                
| Number | port | the port number  | 
                
| Binary | data | the data to send  | 
                
DatagramSocket.prototype. setTimeout (timeout)
使用指定的超时启用/禁用超时,以毫秒为单位。通过将此选项设置为非零超时,对此 DatagramSocket 的 receive() 调用将仅阻塞这段时间。
Parameters
| Number | timeout | timeout in milliseconds  | 
                
ServerSocket ()
这个类实现一个服务器socket。服务器socket等待通过网络进入的请求。
ServerSocket.prototype. accept ()
侦听对此socket的连接并返回一个新的 socket对象。该方法会阻塞,直到建立连接。
Returns
| Socket | a newly connected socket object  | 
                
ServerSocket.prototype. bind (host, port)
将socket绑定到本地地址和端口。如果地址或端口被省略,系统将选择本地地址和端口来绑定socket。
Parameters
| String | host | address (interface) to which the socket will be bound.  | 
                
| Number | port | port number to bind the socket to.  | 
                
ServerSocket.prototype. close ()
立即关闭socket
ServerSocket.prototype. getTimeout ()
返回此 ServerSocket 的当前超时。值为零意味着超时被禁用,即 accept() 永远不会超时
Returns
| Number | the current timeout  | 
                
ServerSocket.prototype. isBound ()
返回这个socket是否绑定到一个地址。
Returns
| Boolean | true if the socket has been bound to an address  | 
                
ServerSocket.prototype. isClosed ()
返回socket是否关闭。
Returns
| Boolean | true if the socket has been closed  | 
                
ServerSocket.prototype. localAddress ()
获取该socket绑定的本地地址。这返回一个具有包含作为字符串的 IP 地址的属性地址的对象和包含端口号的属性端口,例如
{address: '127.0.0.1', port: 8080}
Returns
| Object | an address descriptor  | 
                
ServerSocket.prototype. setTimeout (timeout)
使用指定的超时启用/禁用超时,以毫秒为单位。如果将此选项设置为非零超时,则对此 ServerSocket 的 accept() 调用将仅阻塞这段时间。
Parameters
| Number | timeout | timeout in milliseconds  | 
                
Socket ()
Socket 类用于创建一个 TCP socket。在能够发送和接收数据之前,新创建的socket必须连接到远程地址。
Socket.prototype. bind (host, port)
将socket绑定到本地地址和端口。如果地址或端口被省略,系统将选择本地地址和端口来绑定socket。
Parameters
| String | host | address (interface) to which the socket will be bound.  | 
                
| Number | port | port number to bind the socket to.  | 
                
Socket.prototype. close ()
立即关闭socket
Socket.prototype. connect (host, port, [timeout])
在socket上启动连接。连接超时连接到指定主机上的远程端口。发生故障时引发异常。
Parameters
| String | host | IP address or hostname  | 
                
| Number | port | port number or service name  | 
                
| Number | [timeout] | optional timeout value in milliseconds  | 
                
Socket.prototype. getTimeout ()
返回此 Socket 的当前超时值。值为零意味着禁用超时,即 read() 永远不会超时。
Returns
| Number | the current timeout  | 
                
Socket.prototype. isBound ()
返回此socket是否绑定到地址。
Returns
true if the socket has been bound to an address  | 
                
Socket.prototype. isConnected ()
返回socket是否连接。
Returns
true if the socket has been connected to a remote address  | 
                
Socket.prototype. localAddress ()
获取此socket绑定的本地地址。这返回一个具有包含作为字符串的 IP 地址的属性地址的对象和包含端口号的属性端口,例如
{address: '127.0.0.1', port: 8080}。
Returns
| Object | an address descriptor  | 
                
Socket.prototype. remoteAddress ()
获取此socket连接的远程地址。这将返回一个包含 IP 地址属性地址作为字符串的属性对象和一个包含端口号的属性端口,例如
{address: '127.0.0.1', port: 8080}。
Returns
| Object | an address descriptor  | 
                
Socket.prototype. setTimeout (timeout)
使用指定的超时启用/禁用超时,以毫秒为单位。如果将此选项设置为非零超时,则此socket流上的 read() 将仅阻塞此时间量。
Parameters
| Number | timeout | timeout in milliseconds  |