Manages network connections via TCP/IP sockets.
The NetSocket class provides a simple way of managing TCP/IP socket communications. Connections from a single client to the server and from the server to multiple clients are supported. SSL functionality is also integrated.
The design of the NetSocket class caters to asynchronous (non-blocking) communication. This is achieved primarily through callback fields - connection alerts are managed by Feedback, incoming data is received through Incoming and readiness for outgoing data is supported by Outgoing.
After a connection has been established, data may be written using any of the following methods:
It is possible to write to a NetSocket object before the connection to a server is established. Doing so will buffer the data in the socket until the connection with the server has been initiated, at which point the data will be immediately sent.
To accept incoming client connections, create a NetSocket object with the SERVER
flag set and define the Port value on which to listen for new clients. If multiple connections from a single client IP address are allowed, set the MULTICONNECT
flag.
When a new connection is detected, the Feedback function will be called as Feedback(*NetSocket, *ClientSocket, LONG State)
The NetSocket parameter refers to the original NetSocket object, ClientSocket applies if a client connection is involved and the State value will be set to NTC::CONNECTED
. If a client disconnects, the Feedback function will be called in the same manner but with a State value of NTC::DISCONNECTED
.
Information on all active connections can be read from the Clients field. This contains a linked list of IP addresses and their connections to the server port.
To send data to a client, write it to the target ClientSocket.
All data that is received from client sockets will be passed to the Incoming feedback routine with a reference to a ClientSocket.
The NetSocket class consists of the following fields:
Access | Name | Type | Comment | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Address | STRING | An IP address or domain name to connect to. | |||||||||||||||||
If this field is set with an IP address or domain name prior to initialisation, an attempt to connect to that location will be made when the NetSocket is initialised. Post-initialisation this field cannot be set by the client, however calls to Connect() will result in it being updated so that it always reflects the named address of the current connection. | |||||||||||||||||||
Backlog | INT | The maximum number of connections that can be queued against the socket. | |||||||||||||||||
Incoming connections to NetSocket objects are queued until they are answered by the object. As there is a limit to the number of connections that can be queued, you can adjust the backlog by writing this field. The default setting is 10 connections. If the backlog is exceeded, subsequent connections to the socket will typically be met with a connection refused error. | |||||||||||||||||||
ClientData | APTR | A client-defined value that can be useful in action notify events. | |||||||||||||||||
This is a free-entry field value that can store client data for future reference. | |||||||||||||||||||
ClientLimit | INT | The maximum number of clients that can be connected to a server socket. | |||||||||||||||||
Clients | struct NetClient * | For server sockets, lists all clients connected to the server. | |||||||||||||||||
Error | ERR | Information about the last error that occurred during a NetSocket operation | |||||||||||||||||
This field describes the last error that occurred during a NetSocket operation: In the case where a NetSocket object enters the
| |||||||||||||||||||
Feedback | FUNCTION | A callback trigger for when the state of the NetSocket is changed. | |||||||||||||||||
Refer to a custom function in this field and it will be called whenever the State of the socket (such as connection or disconnection) changes. The function must be in the format The NetSocket parameter will refer to the NetSocket object to which the Feedback function is subscribed. The reflects the new value in the State field. | |||||||||||||||||||
Flags | NSF | Optional flags. | |||||||||||||||||
| |||||||||||||||||||
Incoming | FUNCTION | Callback that is triggered when the socket receives data. | |||||||||||||||||
The Incoming field can be set with a custom function that will be called whenever the socket receives data. The function must follow this definition: The NetSocket parameter refers to the NetSocket object. The Context refers to the object that set the Incoming field. Retrieve data from the socket with the Read() action. Reading at least some of the data from the socket is compulsory - if the function does not do this then the data will be cleared from the socket when the function returns. If the callback function returns | |||||||||||||||||||
MsgLimit | INT | Limits the size of incoming and outgoing messages. | |||||||||||||||||
This field limits the size of incoming and outgoing message queues (each socket connection receives two queues assigned to both incoming and outgoing messages). The size is defined in bytes. Sending or receiving messages that overflow the queue results in the connection being terminated with an error. The default setting is 1 megabyte. | |||||||||||||||||||
OutQueueSize | INT | The number of bytes on the socket's outgoing queue. | |||||||||||||||||
Outgoing | FUNCTION | Callback that is triggered when a socket is ready to send data. | |||||||||||||||||
The Outgoing field can be set with a custom function that will be called whenever the socket is ready to send data. The function must be in the format The NetSocket parameter refers to the NetSocket object. The Context refers to the object that set the Outgoing field. To send data to the NetSocket object, call the Write() action. If the callback function returns The Outgoing field is ineffective if the NetSocket is in server mode (target a connected client socket instead). | |||||||||||||||||||
Port | INT | The port number to use for initiating a connection. | |||||||||||||||||
SocketHandle | APTR | Platform specific reference to the network socket handle. | |||||||||||||||||
State | NTC | The current connection state of the netsocket object. | |||||||||||||||||
| |||||||||||||||||||
TotalClients | INT | Indicates the total number of clients currently connected to the socket (if in server mode). | |||||||||||||||||
In server mode, the netsocket will maintain a count of the total number of clients currently connected to the socket. You can read the total number of connections from this field. In client mode, this field is always set to zero. | |||||||||||||||||||
ValidCert | INT | Indicates certificate validity if the socket is encrypted with a certificate. | |||||||||||||||||
After an encrypted connection has been made to a server, the ValidCert field can be used to determine the validity of the server's certificate. If encrypted communication is not supported, |
The following actions are currently supported:
Name | Comment | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Disable | Disables sending and receiving on the socket. | |||||||||||||||||
ERR acDisable(*Object) This method will stop all sending and receiving of data over the socket. This is irreversible. Error Codes
| ||||||||||||||||||
Read | Read information from the socket. | |||||||||||||||||
ERR acRead(*Object, APTR Buffer, LONG Length, LONG *Result)
The Read() action will read incoming data from the socket and write it to the provided buffer. If the socket connection is safe, success will always be returned by this action regardless of whether or not data was available. Almost all other return codes indicate permanent failure and the socket connection will be closed when the action returns. Error Codes
| ||||||||||||||||||
Write | Writes data to the socket. | |||||||||||||||||
ERR acWrite(*Object, APTR Buffer, LONG Length, LONG Result)
Writing data to a socket will send raw data to the remote client or server. Write connections are buffered, so any data overflow generated in a call to this action will be buffered into a software queue. Resource limits placed on the software queue are governed by the MsgLimit field setting. Do not use this action if in server mode. Instead, write to the ClientSocket object that will receive the data. It is possible to write to a socket in advance of any connection being made. The netsocket will queue the data and automatically send it once the first connection has been made. |
The following methods are currently supported:
Name | Comment | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Connect | Connects a NetSocket to an address. | ||||||||||||||||||||||||
ERR ns::Connect(OBJECTPTR Object, CSTRING Address, LONG Port)
This method initiates the connection process with a target IP address. The address to connect to can be specified either as a domain name, in which case the domain name is first resolved to an IP address, or the address can be specified in standard IP notation. This method is non-blocking. It will return immediately and the connection will be resolved once the server responds to the connection request or an error occurs. Client code should subscribe to the State field to respond to changes to the connection state. Pre-Condition: Must be in a connection state of Post-Condition: If this method returns Error Codes
| |||||||||||||||||||||||||
DisconnectClient | Disconnects all sockets connected to a specific client IP. | ||||||||||||||||||||||||
ERR ns::DisconnectClient(OBJECTPTR Object, struct NetClient * Client)
For server sockets with client IP connections, this method will terminate all socket connections made to a specific client IP and free the resources allocated to it. If Feedback is defined, a If only one socket connection needs to be disconnected, please use DisconnectSocket(). Error Codes
| |||||||||||||||||||||||||
DisconnectSocket | Disconnects a single socket that is connected to a client IP address. | ||||||||||||||||||||||||
ERR ns::DisconnectSocket(OBJECTPTR Object, objClientSocket * Socket)
This method will disconnect a socket connection for a given client. If Feedback is defined, a Error Codes
| |||||||||||||||||||||||||
GetLocalIPAddress | Returns the IP address that the socket is locally bound to. | ||||||||||||||||||||||||
ERR ns::GetLocalIPAddress(OBJECTPTR Object, struct IPAddress * Address)
This method performs the POSIX equivalent of Error Codes
| |||||||||||||||||||||||||
ReadMsg | Read a message from the socket. | ||||||||||||||||||||||||
ERR ns::ReadMsg(OBJECTPTR Object, APTR * Message, LONG * Length, LONG * Progress, LONG * CRC)
This method reads messages that have been sent to the socket using Parasol Message Protocols. Any message sent with the WriteMsg() method will conform to this protocol, thus simplifying message transfers between programs based on the core platform at either point of the network link. This method never returns a successful error code unless an entire message has been received from the sender. Error Codes
| |||||||||||||||||||||||||
WriteMsg | Writes a message to the socket. | ||||||||||||||||||||||||
ERR ns::WriteMsg(OBJECTPTR Object, APTR Message, LONG Length)
Messages can be written to sockets with the WriteMsg method and read back by the receiver with ReadMsg(). The message data is sent through the Write() action, so the standard process will apply (the message will be queued and does not block if buffers are full). Error Codes
|
Name | Description |
---|---|
NSF::LOG_ALL | Print extra log messages. |
NSF::MULTI_CONNECT | Allow multiple connections from the same IP when in server mode. |
NSF::SERVER | Puts the socket into server mode. In this state the netsocket object will wait for incoming connections from clients. |
NSF::SSL | Use Secure Sockets Layer for all communication. |
NSF::SYNCHRONOUS | Use synchronous (blocking) network calls. |
NetSocket states
Name | Description |
---|---|
NTC::CONNECTED | There is an active connection at present. |
NTC::CONNECTING | A connection is being established. |
NTC::CONNECTING_SSL | An SSL connection is being established. |
NTC::DISCONNECTED | There is no connection. |
Field | Type | Description |
---|---|---|
Data | ULONG | 128-bit array for supporting both V4 and V6 IP addresses. |
Type | IPADDR | Identifies the address Data value as a V4 or V6 address type. |
Pad | LONG | Unused padding for 64-bit alignment |
Simple data storage class utilised by NetSocket to represent a client machine/IP.
Field | Type | Description |
---|---|---|
IP | char | IP address in 4/8-byte format |
Next | struct NetClient * | Next client in the chain |
Prev | struct NetClient * | Previous client in the chain |
NetSocket | objNetSocket * | Reference to the parent socket |
Sockets | objClientSocket * | Pointer to a list of sockets opened with this client. |
ClientData | APTR | Free for user data storage. |
TotalSockets | LONG | Count of all created sockets |
Class Info | |
---|---|
ID | ID_NETSOCKET |
Category | Network |
Include | modules/netsocket.h |
Version | 1 |