Monkey Documentation

Class Socket


Constructors:
  • New ( protocol:String="stream" )
Properties:
Methods:
  • Accept : Socket ()
  • AcceptAsync : Void ( onComplete:IOnAcceptComplete )
  • Bind : Bool ( host:String, port:Int )
  • BindAsync : Void ( host:String, port:Int, onComplete:IOnBindComplete )
  • Connect : Bool ( host:String, port:Int )
  • ConnectAsync : Void ( host:String, port:Int, onComplete:IOnConnectComplete )
  • Receive : Int ( data:DataBuffer, offset:Int, count:Int )
  • ReceiveAll : Int ( data:DataBuffer, offset:Int, count:Int )
  • ReceiveAllAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnReceiveComplete )
  • ReceiveAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnReceiveComplete )
  • ReceiveFrom : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress )
  • ReceiveFromAsync : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress, onComplete:IOnReceiveFromComplete )
  • Send : Int ( data:DataBuffer, offset:Int, count:Int )
  • SendAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnSendComplete )
  • SendTo : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress )
  • SendToAsync : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress, onComplete:IOnSendToComplete )

Constructor Documentation

Method New ( protocol:String="stream" )

Creates a new socket.

The supported protocols are:

ProtocolDescription
streamTCP stream
serverTCP server
datagramUDP datagram


Property Documentation

Method IsBound : Bool () Property

Returns true if the socket is currently bound to a local address.

See also Bind

Method IsConnected : Bool () Property

Return true id the socket is currently connected to a remote host.

See also Connect

Method LocalAddress : SocketAddress () Property

Returns the local address of the socket.

Method Protocol : String () Property

Returns the socket protocol, either "stream", "server", or "datagram".

Method RemoteAddress : SocketAddress () Property

Returns the remote address of the socket.


Method Documentation

Method Accept : Socket ()

Accepts a new connection from a remote host.

The method will block until a new connection is accepted.

The socket must be a server socket. If the socket has not been bound, it will first be bound to a system generated host address and port number.

See also Bind

Method AcceptAsync : Void ( onComplete:IOnAcceptComplete )

Accepts a new connection from a remote host asynchronously.

The method returns immediately. Once a new connection is accepted, the OnAcceptComplete method of the onComplete object is called.

The socket must be a server socket.

In order to allow multiple incoming connections, your code should call AcceptAsync again somewhere inside OnAcceptComplete.

Method Bind : Bool ( host:String, port:Int )

Binds the socket to a local host and port.

If host is "", the socket will be bound to a system allocated host address, generally "localhost" or "127.0.0.1".

If port is 0, the socket will be bound to a system allocated port number.

Returns true if successful. This method will fail if the socket is already bound to a port, or the binding failed, for example if port is already in use.

In the case of server sockets, Bind will cause the server to start listening.

See also LocalAddress

Method BindAsync : Void ( host:String, port:Int, onComplete:IOnBindComplete )

Binds the socket to a local host and port asynchronously.

The method returns immediately. Once the bind operation is complete, the OnBindComplete method of the onComplete object is called.

Method Connect : Bool ( host:String, port:Int )

Connects the socket to a remote host.

The method will block until the connection completes.

Returns true if the connection was successful.

The socket must be a stream or datagram socket.

See also RemoteAddress

Method ConnectAsync : Void ( host:String, port:Int, onComplete:IOnConnectComplete )

Connects a socket to a remote host asynchronously.

The method returns immediately. Once the connection is complete, the OnConnectComplete method of the onComplete object is called.

The socket must be a stream or datagram socket.

Method Receive : Int ( data:DataBuffer, offset:Int, count:Int )

Receives data from a remote host and returns the number of bytes received.

The method blocks until either at least 1 byte of data has been received or the socket is closed.

The socket must be a connected stream or connected datagram socket.

See also Send

Method ReceiveAll : Int ( data:DataBuffer, offset:Int, count:Int )

Receives data from a remote host and returns the number of bytes received.

The method blocks until either count bytes of data have been received or the socket is closed.

The socket must be a connected stream or connected datagram socket.

See also Send

Method ReceiveAllAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnReceiveComplete )

Receives data from a remote host asynchronously.

The method returns immediately. Once either count bytes of data have been received or the socket is closed, the OnReceiveComplete method of the onComplete object is called.

The socket must be a connected stream or connected datagram socket.

Note: Be careful not to modify data or address while the operation is in progress.

Method ReceiveAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnReceiveComplete )

Receives data from a remote host asynchronously.

The method returns immediately. Once at least 1 byte of data has been received or the socket is closed, the OnReceiveComplete method of the onComplete object is called.

The socket must be a connected stream or connected datagram socket.

Note: Be careful not to modify data or address while the operation is in progress.

Method ReceiveFrom : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress )

Receives data from a remote host and returns the number of bytes received.

The method blocks until at least 1 byte of data is available or the socket is closed.

The address of the remote host that the data was sent from is written to address.

The socket must be an unconnected datagram socket.

Method ReceiveFromAsync : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress, onComplete:IOnReceiveFromComplete )

Receives data from a remote host asynchronously.

The method returns immediately. Once at least 1 byte of data has been received or the socket is closed, the OnReceiveFromComplete method of the onComplete object is called.

The address of the remote host that the data was sent from is written to address.

The socket must be an unconnected datagram socket.

Note: Be careful not to modify data or address while the operation is in progress.

Method Send : Int ( data:DataBuffer, offset:Int, count:Int )

Sends data to a remote host and returns the number of bytes sent.

The method blocks until either all bytes of data have been sent or the socket is closed.

The socket must be a connected stream or connected datagram socket.

See also Receive

Method SendAsync : Int ( data:DataBuffer, offset:Int, count:Int, onComplete:IOnSendComplete )

Sends data to a remote host asynchronously.

The method returns immediately. Once all bytes of data have bben been sent or the socket is closed, the OnSendComplete method of the onComplete object is called.

The socket must be a connected stream or connected datagram socket.

Note: Be careful not to modify data while the operation is in progress.

Method SendTo : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress )

Sends data to the remote host specified by address and returns the number of bytes sent.

The method blocks until either all bytes have been sent or the socket is closed.

The address parameter must contain a valid socket address.

The socket must be an unconnected datagram socket.

See also ReceiveFrom

Method SendToAsync : Int ( data:DataBuffer, offset:Int, count:Int, address:SocketAddress, onComplete:IOnSendToComplete )

Sends data to the remote host specified by address asynchronously.

The method returns immediately. Once all bytes of data have been sent or the socket is closed, the OnSendToComplete method of the onComplete object is called.

The address parameter must contain a valid socket address.

The socket must be an unconnected datagram socket.

Note: Be careful not to modify data or address while the operation is in progress.