The ServerSocket class allows code to act as a server for Transport Control Protocol (TCP) Connections.

You can test for support at run time using the ServerSocket.isSupported property.

OpenFL target support: This feature is supported on all desktop operating systems, on iOS, and on Android. This feature is not supported on the html5 target or other non-sys targets.

Adobe AIR profile support: This feature is supported on all desktop operating systems, on iOS (starting with AIR 3.8), and on Android (starting with AIR 3.8). This feature is not supported on AIR for TV devices. See AIR Profile Support for more information regarding API support across multiple profiles.

A TCP server listens for incoming connections from remote clients. When a client attempts to connect, the ServerSocket dispatches a connect event. The ServerSocketConnectEvent object dispatched for the event provides a Socket object representing the TCP connection between the server and the client. Use this Socket object for subsequent communication with the connected client. You can get the client address and port from the Socket object, if needed.

Note: Your application is responsible for maintaining a reference to the client Socket object. If you don't, the object is eligible for garbage collection and may be destroyed by the runtime without warning.

To put a ServerSocket object into the listening state, call the listen() method. In the listening state, the server socket object dispatches connect events whenever a client using the TCP protocol attempts to connect to the bound address and port. The ServerSocket object continues to listen for additional connections until you call the close() method.

TCP connections are persistent — they exist until one side of the connection closes it (or a serious network failure occurs). Any data sent over the connection is broken into transmittable packets and reassembled on the other end. All packets are guaranteed to arrive (within reason) — any lost packets are retransmitted. In general, the TCP protocol manages the available network bandwidth better than the UDP protocol. Most OpenFL applications that require socket communications should use the ServerSocket and Socket classes rather than the DatagramSocket class.

The ServerSocket class can only be used in targets that support TCP.

Events:

close

Dispatched when the operating system closes this socket.

connect

Dispatched when a remote socket seeks to connect to this server socket.

Static variables

@:value(true)staticread onlyisSupported:Bool = true

Indicates whether or not ServerSocket features are supported in the run-time environment.

Constructor

new()

Creates a ServerSocket object.

Throws:

SecurityError

This error occurs if the calling content is running outside the AIR application security sandbox.

Variables

read onlybound:Bool

Indicates whether the socket is bound to a local address and port.

read onlylistening:Bool

Indicates whether the server socket is listening for incoming connections.

read onlylocalAddress:String

The IP address on which the socket is listening.

read onlylocalPort:Int

The port on which the socket is listening.

Methods

@:value({ localAddress : "0.0.0.0", localPort : 0 })bind(localPort:Int = 0, localAddress:String = "0.0.0.0"):Void

Binds this socket to the specified local address and port.

Parameters:

localPort

(default = 0) The number of the port to bind to on the local computer. If localPort, is set to 0 (the default), the next available system port is bound. Permission to connect to a port number below 1024 is subject to the system security policy. On Mac and Linux systems, for example, the application must be running with root privileges to connect to ports below 1024.

localAddress

(default = "0.0.0.0") The IP address on the local machine to bind to. This address can be an IPv4 or IPv6 address. If localAddress is set to 0.0.0.0 (the default), the socket listens on all available IPv4 addresses. To listen on all available IPv6 addresses, you must specify "::" as the localAddress argument. To use an IPv6 address, the computer and network must both be configured to support IPv6. Furthermore, a socket bound to an IPv4 address cannot connect to a socket with an IPv6 address. Likewise, a socket bound to an IPv6 address cannot connect to a socket with an IPv4 address. The type of address must match.

Throws:

RangeError

This error occurs when localPort is less than 0 or greater than 65535.

ArgumentError

This error occurs when localAddress is not a syntactically well-formed IP address.

IOError

When the socket cannot be bound, such as when: - the underlying network socket (IP and port) is already in bound by another object or process. - the application is running under a user account that does not have the privileges necessary to bind to the port. Privilege issues typically occur when attempting to bind to well known ports (localPort < 1024) - this ServerSocket object is already bound. (Call close() before binding to a different socket.) - when localAddress is not a valid local address.

close():Void

Closes the socket and stops listening for connections.

Closed sockets cannot be reopened. Create a new ServerSocket instance instead.

Throws:

Error

This error occurs if the socket could not be closed, or the socket was not open.

@:value({ backlog : 0 })listen(backlog:Int = 0):Void

Initiates listening for TCP connections on the bound IP address and port.

The listen() method returns immediately. Once you call listen(), the ServerSocket object dispatches a connect event whenever a connection attempt is made. The socket property of the ServerSocketConnectEvent event object references a Socket object representing the server-client connection.

The backlog parameter specifies how many pending connections are queued while the connect events are processed by your application. If the queue is full, additional connections are denied without a connect event being dispatched. If the default value of zero is specified, then the system-maximum queue length is used. This length varies by platform and can be configured per computer. If the specified value exceeds the system-maximum length, then the system-maximum length is used instead. No means for discovering the actual backlog value is provided. (The system-maximum value is determined by the SOMAXCONN setting of the TCP network subsystem on the host computer.)

Throws:

RangeError

There is insufficient data available to read.

IOError

This error occurs if the socket is not open or bound. This error also occurs if the call to listen() fails for any other reason.

Inherited Variables

Inherited Methods

Defined by EventDispatcher

dispatchEvent(event:Event):Bool

Dispatches an event into the event flow. The event target is the EventDispatcher object upon which the dispatchEvent() method is called.

Parameters:

event

The Event object that is dispatched into the event flow. If the event is being redispatched, a clone of the event is created automatically. After an event is dispatched, its target property cannot be changed, so you must create a new copy of the event for redispatching to work.

Returns:

A value of true if the event was successfully dispatched. A value of false indicates failure or that preventDefault() was called on the event.

Throws:

Error

The event dispatch recursion limit has been reached.

hasEventListener(type:String):Bool

Checks whether the EventDispatcher object has any listeners registered for a specific type of event. This allows you to determine where an EventDispatcher object has altered handling of an event type in the event flow hierarchy. To determine whether a specific event type actually triggers an event listener, use willTrigger().

The difference between hasEventListener() and willTrigger() is that hasEventListener() examines only the object to which it belongs, whereas willTrigger() examines the entire event flow for the event specified by the type parameter.

When hasEventListener() is called from a LoaderInfo object, only the listeners that the caller can access are considered.

Parameters:

type

The type of event.

Returns:

A value of true if a listener of the specified type is registered; false otherwise.

toString():String

willTrigger(type:String):Bool

Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. This method returns true if an event listener is triggered during any phase of the event flow when an event of the specified type is dispatched to this EventDispatcher object or any of its descendants.

The difference between the hasEventListener() and the willTrigger() methods is that hasEventListener() examines only the object to which it belongs, whereas the willTrigger() method examines the entire event flow for the event specified by the type parameter.

When willTrigger() is called from a LoaderInfo object, only the listeners that the caller can access are considered.

Parameters:

type

The type of event.

Returns:

A value of true if a listener of the specified type will be triggered; false otherwise.