class System::Network::Socket

An easy to use class for opening and using TCP/IP sockets.

Constructors

Socket ()Constructs a new socket.

Methods

Socket Accept ()Accepts an incoming connection and returns a new socket representing that connection.
int Bind (const int)Binds the socket to the specified port on the local host.
Close ()Closes the connection to the socket.
int Connect (const string, const int)Connects this socket to the specified host address and port.
int Listen (const int)Starts listening for incoming connections on this socket.
string Receive ()Receives data from the connected socket.
string Receiveln ()Receives a text line from the connected socket.
Send (const string)Sends data to the connected socket.
Sendln (const string)Sends a text line to the connected socket.

Properties

int Blocking ()Returns true if the socket is currently in blocking mode.
Blocking (const int)Sets the socket to either blocking or non-blocking mode.
int Connected ()Returns true if this socket is still connected.
int Error ()Returns the most recent error code, or 0 if no error has occurred.
int InputPending ()Returns the number of bytes currently pending in the input queue of the socket.
int Valid ()Returns true if this is a valid socket.

Type Declaration

native class System::Network::Socket
{
    ["An easy to use class for opening and using TCP/IP sockets."]
    method Socket (); ["Constructs a new socket."]
    method int Bind (const int port); ["Binds the socket to the specified port on the local host. This will make this instance a server socket."]
    method int Listen (const int maxConn); ["Starts listening for incoming connections on this socket. 'maxConn' specifies the maximum number of connections allowed to this socket."]
    method Socket Accept (); ["Accepts an incoming connection and returns a new socket representing that connection. If there are currently no pending connections in the socket's connection queue, this function will wait for an incoming connection, unless the socket is in non-blocking mode."]
    method int Connect (const string host, const int port); ["Connects this socket to the specified host address and port. This will make the socket a client socket. If no server is currently listening for incoming connections, this will return an error."]
    method Send (const string); ["Sends data to the connected socket. The data will be prefixed with a 4-byte length. If the connection has been closed, or any other error occurs, the Connected property will return false and the Error property will return an error code."]
    method string Receive (); ["Receives data from the connected socket. The data must be prefixed with a 4-byte length. If the connection has been closed, or any other error occurs, this will return an empty string. The Connected property will return false and the Error property will return an error code."]
    method Sendln (const string); ["Sends a text line to the connected socket. A carriage return character will be appended. If the connection has been closed, or any other error occurs, the Connected property will return false and the Error property will return an error code."]
    method string Receiveln (); ["Receives a text line from the connected socket. The function will continue to read characters from the socket until a carriage return is received. If the connection has been closed, or any other error occurs, this will return an empty string. The Connected property will return false and the Error property will return an error code."]
    method Close (); ["Closes the connection to the socket."]
    accessor int Blocking (); ["Returns true if the socket is currently in blocking mode. This is the default mode."]
    accessor Blocking (const int); ["Sets the socket to either blocking or non-blocking mode. Only Accept(), Receive() and Send() can be used in non-blocking mode."]
    accessor int InputPending (); ["Returns the number of bytes currently pending in the input queue of the socket. This can be used to check for input before calling Receive() to prevent blocking."]
    accessor int Valid (); ["Returns true if this is a valid socket. If the system has run out of resources for sockets, this may return false."]
    accessor int Connected (); ["Returns true if this socket is still connected. This will not actually check if the socket is still connected. It merely reflects wether the most recent Send() or Receive() operation was successful."]
    accessor int Error (); ["Returns the most recent error code, or 0 if no error has occurred."]
}

Reference

method Socket ()

Constructs a new socket.


method Socket Accept ()

Accepts an incoming connection and returns a new socket representing that connection.

If there are currently no pending connections in the socket's connection queue, this function will wait for an incoming connection, unless the socket is in non-blocking mode.


method int Bind (const int port)

Binds the socket to the specified port on the local host.

This will make this instance a server socket.


method Close ()

Closes the connection to the socket.


method int Connect (const string host, const int port)

Connects this socket to the specified host address and port.

This will make the socket a client socket. If no server is currently listening for incoming connections, this will return an error.


method int Listen (const int maxConn)

Starts listening for incoming connections on this socket.

'maxConn' specifies the maximum number of connections allowed to this socket.


method string Receive ()

Receives data from the connected socket.

The data must be prefixed with a 4-byte length. If the connection has been closed, or any other error occurs, this will return an empty string. The Connected property will return false and the Error property will return an error code.


method string Receiveln ()

Receives a text line from the connected socket.

The function will continue to read characters from the socket until a carriage return is received. If the connection has been closed, or any other error occurs, this will return an empty string. The Connected property will return false and the Error property will return an error code.


method Send (const string)

Sends data to the connected socket.

The data will be prefixed with a 4-byte length. If the connection has been closed, or any other error occurs, the Connected property will return false and the Error property will return an error code.


method Sendln (const string)

Sends a text line to the connected socket.

A carriage return character will be appended. If the connection has been closed, or any other error occurs, the Connected property will return false and the Error property will return an error code.


accessor int Blocking ()

Returns true if the socket is currently in blocking mode.

This is the default mode.


accessor Blocking (const int)

Sets the socket to either blocking or non-blocking mode.

Only Accept(), Receive() and Send() can be used in non-blocking mode.


accessor int Connected ()

Returns true if this socket is still connected.

This will not actually check if the socket is still connected. It merely reflects wether the most recent Send() or Receive() operation was successful.


accessor int Error ()

Returns the most recent error code, or 0 if no error has occurred.


accessor int InputPending ()

Returns the number of bytes currently pending in the input queue of the socket.

This can be used to check for input before calling Receive() to prevent blocking.


accessor int Valid ()

Returns true if this is a valid socket.

If the system has run out of resources for sockets, this may return false.