Object built-in object

ObjectSocket

network socket object

Socket belongs tonetmodule, create method

1
var s = new net.Socket();

inheritance relationship

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<class>Stream|fd|read();write();flush();close();copyTo()] [<this>Socket|new Socket()|family;remoteAddress;remotePort;localAddress;localPort;timeout|connect();bind();listen();accept();recv();send()] [object] <:- [Stream] [Stream] <:- [Socket] Socket new Socket() family remoteAddress remotePort localAddress localPort timeout connect() bind() listen() accept() recv() send() Stream fd read() write() flush() close() copyTo() object toString() toJSON()

Constructor

Socket

Socket constructor, which creates a new Socket object

1
new Socket(Integer family = net.AF_INET);

Call parameters:

  • family: Integer, specify the address set, the default is AF_INET, ipv4

Member properties

family

Integer, query the address set of the current Socket object

1
readonly Integer Socket.family;

remoteAddress

String, query the address of the currently connected peer

1
readonly String Socket.remoteAddress;

remotePort

Integer, query the currently connected peer port

1
readonly Integer Socket.remotePort;

localAddress

String, query the local address of the current connection

1
readonly String Socket.localAddress;

localPort

Integer, query the local port of the current connection

1
readonly Integer Socket.localPort;

timeout

Integer, query and set timeout in milliseconds

1
Integer Socket.timeout;

fd

Integer, queryStreamThe corresponding file descriptor value, implemented by subclasses

1
readonly Integer Socket.fd;

member function

connect

establish a tcp connection

1 2
Socket.connect(String host, Integer port = 0) async;

Call parameters:

  • host: String, specify the address or host name of the other party, and can also point to the path of unix socket and Windows pipe
  • port: Integer, specify the opposite port, this parameter is ignored when connecting unix socket and Windows pipe

bind

Bind the current Socket to the specified port of all local addresses

1 2
Socket.bind(Integer port, Boolean allowIPv4 = true);

Call parameters:

  • port: Integer, specify the port to bind to
  • allowIPv4: Boolean, specifies whether to accept ipv4 connections, the default is true. This parameter is valid in ipv6 and depends on the operating system

Bind the current Socket to the specified port of the specified address

1 2 3
Socket.bind(String addr, Integer port = 0, Boolean allowIPv4 = true);

Call parameters:

  • addr: String, specifies the binding address, can also point to unix socket and Windows pipe path
  • port: Integer, specify the port to bind, this parameter is ignored when binding unix socket and Windows pipe
  • allowIPv4: Boolean, specifies whether to accept ipv4 connections, the default is true. This parameter is valid in ipv6 and depends on the operating system

listen

Start listening for connection requests

1
Socket.listen(Integer backlog = 120);

Call parameters:

  • backlog: Integer, specifies the length of the request queue, requests that exceed it will be rejected, the default is 120

accept

wait for and accept a connection

1
Socket Socket.accept() async;

Return result:

  • Socket, returns the received connection object

recv

Read data of the specified size from the connection. Unlike the read method, recv does not guarantee that the required data is read, but returns immediately after reading the data.

1
Buffer Socket.recv(Integer bytes = -1) async;

Call parameters:

  • bytes: Integer, specify the amount of data to be read, default to read data of any size

Return result:

  • Buffer, returns the data read from the connection

send

Writes the given data to the connection, this method is equivalent to the write method

1
Socket.send(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to write

read

Read data of specified size from the stream

1
Buffer Socket.read(Integer bytes = -1) async;

Call parameters:

  • bytes: Integer, specify the amount of data to be read, the default is to read a random size data block, the size of the data read depends on the device

Return result:

  • Buffer, returns the data read from the stream, or null if there is no data to read or the connection is interrupted

write

Write the given data to the stream

1
Socket.write(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to write

flush

Write file buffer contents to physical device

1
Socket.flush() async;

close

closes the current stream object

1
Socket.close() async;

copyTo

Copy stream data to target stream

1 2
Long Socket.copyTo(Stream stm, Long bytes = -1) async;

Call parameters:

  • stm:Stream, the target stream object
  • bytes: Long, number of bytes copied

Return result:

  • Long, returns the number of bytes copied

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be reimplemented according to its own characteristics

1
String Socket.toString();

Return result:

  • String, returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns the set of readable properties defined by the object

1
Value Socket.toJSON(String key = "");

Call parameters:

  • key: String, unused

Return result:

  • Value, returns a value containing JSON serializable