The socket interface allows you to create a server or a client using either the TCP/IP or UDP protocol. Using sockets you can transfer data between two devices either on a local network or via the internet. You can use this approach to send or download file, create a game server, provide audio or text chat for your application and a lot more.
Static Variables
Name | Description |
ip | IP address, only available upon connection. |
Declaration
|
recv_buffer_size | Current size of the receive buffer. |
Declaration unsigned int recv_buffer_size
|
protocol | Represent the current protocol use by the Socket. |
Declaration
|
Variables
Name | Description |
sck_id | Unique socket id given by the system. |
Declaration
|
Functions
Name | Description |
Create | Create a new non-blocking socket. |
Declaration Socket Create( const bool tcp ) Parameters
tcp : Determine whether or the Socket should use TCP/IP or the UPD protocol.
Return Value
The new Socket freshly created.
|
Destroy | Destroy a previously initialized Socket. |
Declaration
|
GenId | Generate a new Socket id ready for transmission. |
Declaration Return Value
Return the active sck_id used by the Socket.
|
Bind | Bind the socket start listening (TCP only) on the port specified. |
Declaration int Bind( unsigned short port ) Parameters
port : The port number to bind/open for connections.
Range 0 to 65535 Return Value
On success the function will return 0 ; -1 if the port is not available and -2 if the maximum amount of connections have been reached.
|
Accept | Accep a new Socket connection. |
Declaration Return Value
On success, return a nonnegative number that represent the new sck_id for the accepted socket. If an error occur -1 is returned.
|
Connect | Function to establish a connection with a local or remove port. |
Declaration int Connect( const char *host, unsigned short port ) Parameters
host : The IP address of the host.
port : The port number to connect.
Return Value
On success return 0 else return -1 if the IP address provided is invalid or if an communication error occur.
|
Close | Close the active socket connection. |
Declaration Return Value
true if the Socket is closed successfully else return false .
|
SetOption | Set a specific SocketOption value. |
Declaration int SetOption( const SocketOption option, int flag ) Parameters
Return Value
0 upon successf; else return a negative value that represent the error code associed with the option for the active platform.
|
Recv | Function to check if data have been received by the Socket. |
Declaration Return Value
A value greater than 0 represent the size in bytes of the data available to the Socket.
- Warning
- Calling this function will reset the data available in the receive buffer.
|
GetRecvBuffer | Set a specific SocketOption value. |
Declaration const char *GetRecvBuffer( void ) Return Value
The current byes available in the receive buffer.
|
Send | Send data over the Socket connection. |
Declaration int Send( const char *data ) Return Value
The number of bytes that were sent over the Socket. Take note that based on the type of Socket and the size of the data multiple Send calls are necessary. It is recommended to progressively transmit the data based on the number of bytes transferred for each send call.
|
SocketOption
Constant value to use an option which can be set via the SetOption
function of the Socket interface.
kNonBlock
: Enable or disable non-blocking socket.
kTcpNoDelay
: Control the availability of the "Nagle" algorithm.
kSendBufferSize
: Allows you to specify the size of the send buffer.
kRecvBufferSize
: Allows you to change the size of the receive buffer.
kReuseAddress
: This option allows multiple sockets to be open on the same port. If using UDP, it is used for broadcast.