Object built-in object

ObjectSocket

network socket object

Socket belongs tonetmodule, creation method

1
var s = new net.Socket();

inheritance relationship

Constructor

Socket

Socket constructor, 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 other party's address of the current connection

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 unit 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 3
Socket.connect(String host, Integer port, Integer timeout = 0) async;

Call parameters:

  • host: String, specifies the other party's address or host name, or can point to unix socket and Windows pipe paths
  • port: Integer, specifies the other party's port. When connecting unix socket and Windows pipe, this parameter needs to be 0
  • timeout: Integer, specifies the timeout, the unit is milliseconds, the default is 0

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, specifies the bound port
  • allowIPv4: Boolean, specifies whether to accept ipv4 connections, the default is true. This parameter is valid for ipv6 and depends on the operating system

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

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

Call parameters:

  • addr: String, specifies the binding address, and can also point to unix socket and Windows pipe paths
  • port: Integer, specifies the bound port. 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 for 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 request queue length. Requests exceeding the limit will be rejected. The default is 120.

accept

Wait for and accept a connection

1
Socket Socket.accept() async;

Return results:

  • Socket, returns the received connection object

recv

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

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

Call parameters:

  • bytes: Integer, specifies the amount of data to be read. By default, data of any size is read.

Return results:

  • Buffer, returns the data read from the connection

send

Write 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 be written

read

Read data of specified size from the stream

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

Call parameters:

  • bytes: Integer, specifies the amount of data to be read. The default is to read data blocks of random size. The size of the data read depends on the device.

Return results:

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

write

Write the given data to the stream

1
Socket.write(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to be written

flush

Write file buffer contents to physical device

1
Socket.flush() async;

close

Close 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, target stream object
  • bytes: Long, number of bytes copied

Return results:

  • Long, returns the number of bytes copied

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Socket.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

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

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable