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