Object built-in object

Object Socket

Network socket object

Socket belongs to the net module, how to create

1
var s = new net.Socket();

Inheritance

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, the specified address set, the default is AF_INET, ipv4
  • type : Integer, specifies the protocol family, the default is SOCK_STREAM, tcp

Member attribute

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 currently connected partner

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 currently connected local port

1
readonly Integer Socket.localPort;

timeout

Integer, query and set timeout unit in milliseconds

1
Integer Socket.timeout;

Member function

connect

Establish a tcp connection

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

Call parameters:

  • host : String, specify the other party's address or host name
  • port : Integer, specify the other party's port

bind

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

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

Call parameters:

  • port : Integer, specify the bound port
  • allowIPv4 : Boolean, specifies 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 at 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, specifies 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, the excess request will be rejected, the default is 120

accept

Wait and accept a connection

1
Socket Socket.accept() async;

Return result:

  • Socket , returns the connection object received

recv

Read the 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, the default is to read data of any size

Return result:

  • Buffer , returns 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 is to read data of any size

Return result:

  • NObject , return the packet read from the connection

The result returned by recvfrom contains the following:

  • data : 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 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 result:

  • Buffer , returns the data read from the stream, or null if no data is readable, 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 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 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, 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 reimplemented according to its own characteristics

1
String Socket.toString();

Return result:

  • String , returns a 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