class Socket
package sys.net
extended by SslSocket, Socket, SslSocket, UdpSocket, Socket
Available on neko, python, hl, macro, cs, php, java, cpp, lua
A TCP socket class : allow you to both connect to a given server and exchange messages or start your own server and wait for connections.
Static methods
staticselect(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>, ?timeout:Float):{write:Array<Socket>, read:Array<Socket>, others:Array<Socket>}
Wait until one of the sockets group is ready for the given operation:
read
contains sockets on which we want to wait for available data to be read,write
contains sockets on which we want to wait until we are allowed to write some data to their output buffers,others
contains sockets on which we want to wait for exceptional conditions.-
select
will block until one of the condition is met, in which case it will return the sockets for which the condition was true.In case a `timeout` (in seconds) is specified, select might wait at worst until the timeout expires.
Constructor
Variables
custom:Dynamic
Available on neko, python, hl, macro, cs, php, java, cpp
A custom value that can be associated with the socket. Can be used to retrieve your custom infos after a select
.
Methods
accept():Socket
Accept a new connected client. This will return a connected socket on which you can read/write some data.
bind(host:Host, port:Int):Void
Bind the socket to the given host/port so it can afterwards listen for connections there.
close():Void
Closes the socket : make sure to properly close all your sockets or you will crash when you run out of file descriptors.
connect(host:Host, port:Int):Void
Connect to the given server host/port. Throw an exception in case we couldn't successfully connect.
listen(connections:Int):Void
Allow the socket to listen for incoming questions. The parameter tells how many pending connections we can have until they get refused. Use accept()
to accept incoming connections.
read():String
Read the whole data available on the socket.
Note: this is not meant to be used together with setBlocking(false)
,
as it will always throw haxe.io.Error.Blocked
. input
methods should be used directly instead.
setBlocking(b:Bool):Void
Change the blocking mode of the socket. A blocking socket is the default behavior. A non-blocking socket will abort blocking operations immediately by throwing a haxe.io.Error.Blocked value.
setFastSend(b:Bool):Void
Allows the socket to immediately send the data when written to its output : this will cause less ping but might increase the number of packets / data size, especially when doing a lot of small writes.
setTimeout(timeout:Float):Void
Gives a timeout (in seconds) after which blocking socket operations (such as reading and writing) will abort and throw an exception.
write(content:String):Void
Write the whole data to the socket output.
Note: this is not meant to be used together with setBlocking(false)
, as
haxe.io.Error.Blocked
may be thrown mid-write with no indication of how many bytes have been written.
output.writeBytes()
should be used instead as it returns this information.