class Socket extends Object with StObject
This class is an abstraction of a TCP socket or a streaming IPC
endpoint
(uses named pipes on Windows, and Unix domain sockets otherwise). It is also
an EventEmitter
.
A net.Socket
can be created by the user and used directly to interact with
a server. For example, it is returned by createConnection
,
so the user can use it to talk to the server.
It can also be created by Node.js and passed to the user when a connection
is received. For example, it is passed to the listeners of a 'connection'
event emitted on a Server
, so the user can use
it to interact with the client.
- Annotations
- @JSType() @JSImport("net", "Socket") @native()
- Since
v0.3.4
- Alphabetic
- By Inheritance
- Socket
- StObject
- Object
- Any
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Socket(options: SocketConstructorOpts)
- new Socket()
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def addListener(event: String, listener: Function1[Any, Unit]): Socket.this.type
events.EventEmitter
events.EventEmitter
- close 2. connect 3. data 4. drain 5. end 6. error 7. lookup 8. timeout
- def addListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_end(event: end, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def addListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("addListener")
- def address(): |[AddressInfo, Object]
Returns the bound
address
, the addressfamily
name andport
of the socket as reported by the operating system:{ port: 12346, family: 'IPv4', address: '127.0.0.1' }
Returns the bound
address
, the addressfamily
name andport
of the socket as reported by the operating system:{ port: 12346, family: 'IPv4', address: '127.0.0.1' }
- Since
v0.1.90
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- val bytesRead: Double
The amount of received bytes.
The amount of received bytes.
- Since
v0.5.3
- val bytesWritten: Double
The amount of bytes sent.
The amount of bytes sent.
- Since
v0.5.3
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def connect(port: Double, host: String, connectionListener: Function0[Unit]): Socket.this.type
- def connect(port: Double, host: String): Socket.this.type
- def connect(port: Double, connectionListener: Function0[Unit]): Socket.this.type
- def connect(port: Double): Socket.this.type
- def connect(path: String, connectionListener: Function0[Unit]): Socket.this.type
- def connect(path: String): Socket.this.type
- def connect(options: SocketConnectOpts, connectionListener: Function0[Unit]): Socket.this.type
- def connect(options: SocketConnectOpts): Socket.this.type
Initiate a connection on a given socket.
Initiate a connection on a given socket.
Possible signatures:
*
socket.connect(options[, connectListener])
*socket.connect(path[, connectListener])
forIPC
connections. *socket.connect(port[, host][, connectListener])
for TCP connections. * Returns:net.Socket
The socket itself.This function is asynchronous. When the connection is established, the
'connect'
event will be emitted. If there is a problem connecting, instead of a'connect'
event, an'error'
event will be emitted with the error passed to the'error'
listener. The last parameterconnectListener
, if supplied, will be added as a listener for the'connect'
event **once**.This function should only be used for reconnecting a socket after
'close'
has been emitted or otherwise it may lead to undefined behavior. - val connecting: Boolean
If
true
,socket.connect(options[, connectListener])
was called and has not yet finished.If
true
,socket.connect(options[, connectListener])
was called and has not yet finished. It will staytrue
until the socket becomes connected, then it is set tofalse
and the'connect'
event is emitted. Note that thesocket.connect(options[, connectListener])
callback is a listener for the'connect'
event.- Since
v6.1.0
- val destroyed: Boolean
See
writable.destroyed
for further details. - def emit(event: Symbol, args: Any*): Boolean
- def emit(event: String, args: Any*): Boolean
- def emit_close(event: close, hadError: Boolean): Boolean
- Annotations
- @JSName("emit")
- def emit_connect(event: connect): Boolean
- Annotations
- @JSName("emit")
- def emit_data(event: data, data: Buffer): Boolean
- Annotations
- @JSName("emit")
- def emit_drain(event: drain): Boolean
- Annotations
- @JSName("emit")
- def emit_end(event: end): Boolean
- Annotations
- @JSName("emit")
- def emit_error(event: error, err: Error): Boolean
- Annotations
- @JSName("emit")
- def emit_lookup(event: lookup, err: Error, address: String, family: Double, host: String): Boolean
- Annotations
- @JSName("emit")
- def emit_lookup(event: lookup, err: Error, address: String, family: String, host: String): Boolean
- Annotations
- @JSName("emit")
- def emit_ready(event: ready): Boolean
- Annotations
- @JSName("emit")
- def emit_timeout(event: timeout): Boolean
- Annotations
- @JSName("emit")
- def end(str: Uint8Array, encoding: Unit, callback: Function0[Unit]): Unit
- def end(str: Uint8Array, encoding: BufferEncoding, callback: Function0[Unit]): Unit
- def end(str: Uint8Array, encoding: BufferEncoding): Unit
- def end(str: String, encoding: Unit, callback: Function0[Unit]): Unit
- def end(str: String, encoding: BufferEncoding, callback: Function0[Unit]): Unit
- def end(str: String, encoding: BufferEncoding): Unit
- def end(callback: Function0[Unit]): Unit
- def end(buffer: Uint8Array, callback: Function0[Unit]): Unit
- def end(buffer: Uint8Array): Unit
- def end(buffer: String, callback: Function0[Unit]): Unit
- def end(buffer: String): Unit
- def end(): Unit
Half-closes the socket.
Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
See
writable.end()
for further details.- returns
The socket itself.
- Since
v0.1.90
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hasOwnProperty(v: String): Boolean
- Definition Classes
- Object
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isPrototypeOf(v: Object): Boolean
- Definition Classes
- Object
- val localAddress: UndefOr[String]
The string representation of the local IP address the remote client is connecting on.
The string representation of the local IP address the remote client is connecting on. For example, in a server listening on
'0.0.0.0'
, if a client connects on'192.168.1.1'
, the value ofsocket.localAddress
would be'192.168.1.1'
.- Since
v0.9.6
- val localPort: UndefOr[Double]
The numeric representation of the local port.
The numeric representation of the local port. For example,
80
or21
.- Since
v0.9.6
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def on(event: String, listener: Function1[Any, Unit]): Socket.this.type
- def on_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_connect(event: connect, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_drain(event: drain, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_end(event: end, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_ready(event: ready, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def on_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("on")
- def once(event: String, listener: Function1[Any, Unit]): Socket.this.type
- def once_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_connect(event: connect, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_drain(event: drain, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_end(event: end, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_ready(event: ready, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def once_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("once")
- def pause(): Socket.this.type
Pauses the reading of data.
Pauses the reading of data. That is,
'data'
events will not be emitted. Useful to throttle back an upload.- returns
The socket itself.
- def prependListener(event: String, listener: Function1[Any, Unit]): Socket.this.type
- def prependListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_end(event: end, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependListener")
- def prependOnceListener(event: String, listener: Function1[Any, Unit]): Socket.this.type
- def prependOnceListener_close(event: close, listener: Function1[Boolean, Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_connect(event: connect, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_data(event: data, listener: Function1[Buffer, Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_drain(event: drain, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_end(event: end, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_error(event: error, listener: Function1[Error, Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_lookup(event: lookup, listener: Function4[Error, String, |[String, Double], String, Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_ready(event: ready, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def prependOnceListener_timeout(event: timeout, listener: Function0[Unit]): Socket.this.type
- Annotations
- @JSName("prependOnceListener")
- def propertyIsEnumerable(v: String): Boolean
- Definition Classes
- Object
- def ref(): Socket.this.type
Opposite of
unref()
, callingref()
on a previouslyunref
ed socket will_not_ let the program exit if it's the only socket left (the default behavior).Opposite of
unref()
, callingref()
on a previouslyunref
ed socket will_not_ let the program exit if it's the only socket left (the default behavior). If the socket isref
ed callingref
again will have no effect.- returns
The socket itself.
- Since
v0.9.1
- val remoteAddress: UndefOr[String]
The string representation of the remote IP address.
The string representation of the remote IP address. For example,
'74.125.127.100'
or'2001:4860:a005::68'
. Value may beundefined
if the socket is destroyed (for example, if the client disconnected).- Since
v0.5.10
- val remoteFamily: UndefOr[String]
The string representation of the remote IP family.
The string representation of the remote IP family.
'IPv4'
or'IPv6'
.- Since
v0.11.14
- val remotePort: UndefOr[Double]
The numeric representation of the remote port.
The numeric representation of the remote port. For example,
80
or21
.- Since
v0.5.10
- def resume(): Socket.this.type
Resumes reading after a call to
socket.pause()
.Resumes reading after a call to
socket.pause()
.- returns
The socket itself.
- def setEncoding(encoding: BufferEncoding): Socket.this.type
- def setEncoding(): Socket.this.type
Set the encoding for the socket as a
Readable Stream
.Set the encoding for the socket as a
Readable Stream
. Seereadable.setEncoding()
for more information.- returns
The socket itself.
- Since
v0.1.90
- def setKeepAlive(enable: Unit, initialDelay: Double): Socket.this.type
- def setKeepAlive(enable: Boolean, initialDelay: Double): Socket.this.type
- def setKeepAlive(enable: Boolean): Socket.this.type
- def setKeepAlive(): Socket.this.type
Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
Set
initialDelay
(in milliseconds) to set the delay between the last data packet received and the first keepalive probe. Setting0
forinitialDelay
will leave the value unchanged from the default (or previous) setting.Enabling the keep-alive functionality will set the following socket options:
*
SO_KEEPALIVE=1
*TCP_KEEPIDLE=initialDelay
*TCP_KEEPCNT=10
*TCP_KEEPINTVL=1
- returns
The socket itself.
- Since
v0.1.92
- def setNoDelay(noDelay: Boolean): Socket.this.type
- def setNoDelay(): Socket.this.type
Enable/disable the use of Nagle's algorithm.
Enable/disable the use of Nagle's algorithm.
When a TCP connection is created, it will have Nagle's algorithm enabled.
Nagle's algorithm delays data before it is sent via the network. It attempts to optimize throughput at the expense of latency.
Passing
true
fornoDelay
or not passing an argument will disable Nagle's algorithm for the socket. Passingfalse
fornoDelay
will enable Nagle's algorithm.- returns
The socket itself.
- Since
v0.1.90
- def setTimeout(timeout: Double, callback: Function0[Unit]): Socket.this.type
- def setTimeout(timeout: Double): Socket.this.type
Sets the socket to timeout after
timeout
milliseconds of inactivity on the socket.Sets the socket to timeout after
timeout
milliseconds of inactivity on the socket. By defaultnet.Socket
do not have a timeout.When an idle timeout is triggered the socket will receive a
'timeout'
event but the connection will not be severed. The user must manually callsocket.end()
orsocket.destroy()
to end the connection.js socket.setTimeout(3000); socket.on('timeout', () => { console.log('socket timeout'); socket.end(); });
If
timeout
is 0, then the existing idle timeout is disabled.The optional
callback
parameter will be added as a one-time listener for the'timeout'
event.- returns
The socket itself.
- Since
v0.1.90
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toLocaleString(): String
- Definition Classes
- Object
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unref(): Socket.this.type
Calling
unref()
on a socket will allow the program to exit if this is the only active socket in the event system.Calling
unref()
on a socket will allow the program to exit if this is the only active socket in the event system. If the socket is alreadyunref
ed callingunref()
again will have no effect.- returns
The socket itself.
- Since
v0.9.1
- def valueOf(): Any
- Definition Classes
- Object
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- def write(str: Uint8Array, encoding: Unit, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(str: Uint8Array, encoding: BufferEncoding, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(str: Uint8Array, encoding: BufferEncoding): Boolean
- def write(str: String, encoding: Unit, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(str: String, encoding: BufferEncoding, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(str: String, encoding: BufferEncoding): Boolean
- def write(buffer: Uint8Array, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(buffer: Uint8Array): Boolean
Sends data on the socket.
Sends data on the socket. The second parameter specifies the encoding in the case of a string. It defaults to UTF8 encoding.
Returns
true
if the entire data was flushed successfully to the kernel buffer. Returnsfalse
if all or part of the data was queued in user memory.'drain'
will be emitted when the buffer is again free.The optional
callback
parameter will be executed when the data is finally written out, which may not be immediately.See
Writable
streamwrite()
method for more information.- Since
v0.1.90
- def write(buffer: String, cb: Function1[UndefOr[Error], Unit]): Boolean
- def write(buffer: String): Boolean
Deprecated Value Members
- val bufferSize: Double
This property shows the number of characters buffered for writing.
This property shows the number of characters buffered for writing. The buffer may contain strings whose length after encoding is not yet known. So this number is only an approximation of the number of bytes in the buffer.
net.Socket
has the property thatsocket.write()
always works. This is to help users get up and running quickly. The computer cannot always keep up with the amount of data that is written to a socket. The network connection simply might be too slow. Node.js will internally queue up the data written to a socket and send it out over the wire when it is possible.The consequence of this internal buffering is that memory may grow. Users who experience large or growing
bufferSize
should attempt to "throttle" the data flows in their program withsocket.pause()
andsocket.resume()
.- Deprecated
Since v14.6.0 - Use
writableLength
instead.- Since
v0.3.8
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated