Object built-in object

Object Socket

Network socket object

Socket belongs to net Module, creation method

1
var s = new net.Socket();

Inheritance relationship

Constructor

Socket

Socket constructor, create a new Socket object

1 2
new Socket(Integer family = net.AF_INET, Integer type = net.SOCK_STREAM);

Call parameters:

  • family: Integer, specify the address set, the default is AF_INET, ipv4
  • type: Integer, specify the protocol family, the default is SOCK_STREAM, tcp

Member attributes

family

Integer, query the address set of the current Socket object

1
readonly Integer Socket.family;

type

Integer, query the protocol family of the current Socket object

1
readonly Integer Socket.type;

remoteAddress

String, query the address of the other party currently connected

1
readonly String Socket.remoteAddress;

remotePort

Integer, query the port of the other party currently connected

1
readonly Integer Socket.remotePort;

localAddress

String, query the local address of the current connection

1
readonly String Socket.localAddress;

localPort

Integer, query the currently connected local port

1
readonly Integer Socket.localPort;

timeout

Integer, query and set the timeout time in milliseconds

1
Integer Socket.timeout;

fd

Integer, query Stream The corresponding file descriptor value, implemented by the subclass

1
readonly Integer Socket.fd;

Member function

connect

Establish a tcp connection

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

Call parameters:

  • host: String, specify the address or host name of the other party
  • port: Integer, specify the opposite port

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 bound port
  • allowIPv4: Boolean, specify whether to accept ipv4 connection, 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, Boolean allowIPv4 = true);

Call parameters:

  • addr: String, specify the bound address
  • port: Integer, specify the bound port
  • allowIPv4: Boolean, specify whether to accept ipv4 connection, 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, specify the length of the request queue, requests that exceed it will be rejected, the default is 120

accept

Wait and accept a connection

1
Socket Socket.accept() async;

Return result:

  • Socket, Return the received connection object

recv

Read the specified size of data 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, the default reads data of any size

Return result:

  • Buffer, Return the data read from the connection

recvfrom

Read a UDP packet

1
NObject Socket.recvfrom(Integer bytes = -1) async;

Call parameters:

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

Return result:

  • NObject, Return the data packet read from the connection

The results returned by recvfrom include the following:

  • data: The received binary data block
  • address: Sender's address
  • port: Sender's port

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

sendto

Send a UDP packet to the given ip:port

1 2 3
Socket.sendto(Buffer data, String host, Integer port) async;

Call parameters:

  • data: Buffer, Given the data to be written
  • host: String, specify the target ip or host name
  • port: Integer, specify the target port

read

Read data of the 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, Return the data read from the stream, if there is no data to read, or the connection is interrupted, then return null

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 the contents of the file buffer to the physical device

1
Socket.flush() async;

close

Close the current stream object

1
Socket.close() async;

copyTo

Copy the stream data to the target stream

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

Call parameters:

  • stm: Stream, The target stream object
  • bytes: Long, the 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 re-implemented 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 a collection of readable attributes defined by the object

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

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable