Network Component
Version 6.3
MDK-Professional Middleware for IP Networking
|
TCP socket routines enable reliable IP communication using the transmission control protocol (TCP). More...
enum | tcpEvent { tcpEventConnect = 0, tcpEventEstablished, tcpEventClosed, tcpEventAbort, tcpEventACK, tcpEventData } |
TCP Callback Events. More... | |
enum | tcpState { tcpStateUNUSED = 0, tcpStateCLOSED, tcpStateLISTEN, tcpStateSYN_RECEIVED, tcpStateSYN_SENT, tcpStateFIN_WAIT_1, tcpStateFIN_WAIT_2, tcpStateCLOSING, tcpStateLAST_ACK, tcpStateTIME_WAIT, tcpStateESTABLISHED } |
TCP States. More... | |
typedef uint32_t(* | net_tcp_cb_t )(int32_t socket, tcpEvent event, const uint8_t *buf, uint32_t len) |
TCP Event callback function. | |
int32_t | tcp_get_socket (uint8_t type, uint8_t tos, uint32_t tout, net_tcp_cb_t cb_func) |
Allocate a free TCP socket. | |
netStatus | tcp_release_socket (int32_t socket) |
Release TCP socket and free resources. | |
netStatus | tcp_listen (int32_t socket, uint16_t port) |
Open TCP socket for incoming connection. | |
netStatus | tcp_connect (int32_t socket, const uint8_t *ip_addr, uint16_t port, uint16_t local_port) |
Initiate a TCP connection to a remote node. | |
uint8_t * | tcp_get_buf (uint32_t size) |
Allocate memory for TCP send buffer. | |
uint32_t | tcp_max_data_size (int32_t socket) |
Determine maximum number of data bytes that can be sent in TCP packet. | |
bool | tcp_check_send (int32_t socket) |
Check if TCP socket can send data. | |
tcpState | tcp_get_state (int32_t socket) |
Determine current state of a TCP socket. | |
netStatus | tcp_send (int32_t socket, uint8_t *buf, uint32_t len) |
Send a data packet to remote node. | |
netStatus | tcp_close (int32_t socket) |
Stop TCP communication and start closing procedure. | |
netStatus | tcp_abort (int32_t socket) |
Instantly stop TCP communication. | |
netStatus | tcp_reset_window (int32_t socket) |
Reset TCP window size to a default value from the configuration. | |
TCP socket routines enable reliable IP communication using the transmission control protocol (TCP).
The Transmission Control Protocol (TCP) runs on top of the Internet Protocol (IP). TCP is a connection-oriented and reliable full duplex protocol supporting a pair of byte streams, one for each direction. The two applications must establish a TCP connection before exchanging data. TCP retransmits data that do not reach the final destination due to errors or data corruption. Data are delivered in the sequence of their transmission.
TCP Event callback function.
[in] | socket | TCP socket handle of the local machine. |
[in] | event | Event type as shown in the table below (tcpEvent). |
[in] | buf | Pointer to a buffer containing the received data or to the IP address of the remote machine (see below). |
[in] | len | Number of data bytes or port number of the remote machine (see below). |
Is the type definition for the TCP callback function.
The event callback function of the TCP socket is called by the Network Core whenever a TCP event occurs.
The argument socket is the TCP socket handle of the local machine. The argument event specifies the type of event that occurred as shown in the table below or tcpEvent. The argument buf points to a buffer or to an IP address. If event is tcpEventData, then buf points to a buffer containing the received data. For all other events, buf points to the IP address of the remote machine. The argument len specifies the data length or port number of the remote machine. If event is tcpEventData, then len specifies the number of received data bytes. For all other events, len specifies the port number used by the remote machine.
The Network Core uses the return value of the callback function only when the event is tcpEventConnect. It uses the return value to decide whether to accept or reject an incoming connection when the TCP socket is listening. If the listener function returns 1, then it accepts the incoming connection. If the listener function returns 0, it rejects the incoming connection. Thus, you can define the listener function to selectively reject incoming connections from particular IP addresses.
Event Type | Description |
---|---|
tcpEventConnect | A Connect Request has been received from a remote client that wants to connect to the server |
tcpEventEstablished | The TCP socket has connected to the remote machine |
tcpEventClosed | The TCP connection has been properly closed |
tcpEventAbort | The TCP connection has been aborted |
tcpEventACK | Acknowledgement has been received from the remote host for the previously sent data |
tcpEventData | A TCP data packet has been received |
Parameter for:
enum tcpEvent |
TCP Callback Events.
Parameter for:
enum tcpState |
TCP States.
Returned by:
netStatus tcp_abort | ( | int32_t | socket | ) |
Instantly stop TCP communication.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_abort closes the TCP connection immediately by sending a TCP frame with the RESET flag set to the remote machine.
The argument socket specifies the handle of the socket.
The Network Core calls the listener callback function only when a remote peer has aborted the connection. If the aborted socket is initiated locally by calling tcp_abort, then the callback function is not called.
Code Example
bool tcp_check_send | ( | int32_t | socket | ) |
Check if TCP socket can send data.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_check_send determines whether the TCP socket can send data. It does this by checking whether the TCP connection has been established and whether the socket has received an acknowledgement from the remote machine for data sent previously.
The argument socket specifies the socket handle.
Code Example
netStatus tcp_close | ( | int32_t | socket | ) |
Stop TCP communication and start closing procedure.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_close initiates the procedure to close the TCP connection. It might take some time to close the connection.
The argument socket specifies the socket handle.
The Network Core calls the listener callback function only when a remote peer has closed the connection. If the socket is closed locally by calling tcp_close, then the callback function is not called.
When a socket is of type TCP_TYPE_SERVER
or TCP_TYPE_CLIENT_SERVER
, the socket does not close by calling tcp_close. Only the active connection is closed, and the socket transits to tcpStateLISTEN
. In this state, the socket is still able to accept incoming connections. To close the TCP_TYPE_SERVER
socket, the function tcp_close needs to be called twice.
Code Example
netStatus tcp_connect | ( | int32_t | socket, |
const uint8_t * | ip_addr, | ||
uint16_t | port, | ||
uint16_t | local_port | ||
) |
Initiate a TCP connection to a remote node.
[in] | socket | socket handle obtained with tcp_get_socket. |
[in] | ip_addr | IP address of the remote node. |
[in] | port | port number of the remote node. |
[in] | local_port | local port number or 0 for system assigned local port. |
The function tcp_connect initiates a connection to a remote server. The argument socket is a socket handle on the local machine for communicating.
The argument ip_addr points to the buffer containing the IP address octets of the remote server.
The argument port specifies the TCP port number on the remote machine.
The argument local_port specifies the port on the local machine. If local_port is set to 0, then the Network Core allocates the first free TCP port automatically.
TCP_TYPE_CLIENT
or TCP_TYPE_CLIENT_SERVER
can call the tcp_connect function.Code Example
uint8_t * tcp_get_buf | ( | uint32_t | size | ) |
Allocate memory for TCP send buffer.
[in] | size | number of bytes to allocate. |
The function tcp_get_buf allocates memory for the TCP send buffer into which your application can write the outgoing data packet. The argument size specifies the number of data bytes that the application wants to send.
After the TCP frame has been sent and an acknowledgement has been received from the remote host, the Network Core automatically de-allocates the memory used by the send buffer in the tcp_send function.
A default Maximum Segment Size of 1460 bytes is defined at start-up. However, when establishing a connection with a remote machine, the Network Core might negotiate a different (smaller) value for the Maximum Segment Size.
Code Example
int32_t tcp_get_socket | ( | uint8_t | type, |
uint8_t | tos, | ||
uint32_t | tout, | ||
net_tcp_cb_t | cb_func | ||
) |
Allocate a free TCP socket.
[in] | type | socket type:
|
[in] | tos | IP type of service:
|
[in] | tout | idle timeout in seconds. |
[in] | cb_func | event listening callback function. |
The function tcp_get_socket allocates a free TCP socket. The function initializes all the state variables of the TCP socket to the default state. The argument type specifies the type of the TCP socket:
Socket Type | Description |
---|---|
TCP_TYPE_SERVER | The TCP socket is able to listen on the TCP port for incoming connections. |
TCP_TYPE_CLIENT | The TCP socket is able to initiate a connection to a remote server. |
TCP_TYPE_CLIENT_SERVER | The TCP socket is able to listen on the TCP port for incoming connections and to initiate a connection to a remote server. |
TCP_TYPE_DELAY_ACK | This attribute improves the performance for applications sending large amounts of data like HTTP server. You can combine this attribute with the other attributes using the bitwise-or (|) operation. |
TCP_TYPE_FLOW_CTRL | The TCP socket is able to control TCP Data Flow. You can combine this attribute with the other attributes using the bitwise-or (|) operation. |
TCP_TYPE_KEEP_ALIVE | The TCP socket is able to send keep-alive packets when timeout expires. You can combine this attribute with the other attributes using the bitwise-or (|) operation. |
The argument tos specifies the IP Type Of Service. The most common value for tos is 0.
The argument tout specifies the idle timeout in seconds. The TCP connection is supervised by the keep alive timer. When the connection has been idle for more than tout seconds, the Network Core disconnects the TCP connection or sends a keep-alive packet if TCP_TYPE_KEEP_ALIVE
attribute is set.
The argument cb_func is the event callback function of the TCP socket. The Network Core calls the function whenever a TCP event occurs. Refer to net_tcp_cb_t.
TCP_TYPE_KEEP_ALIVE
attribute for a long-standing connection.Code Example
tcpState tcp_get_state | ( | int32_t | socket | ) |
Determine current state of a TCP socket.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_get_state determines the current state of the TCP socket. The application can monitor the progress when establishing or closing a connection with this function. The most useful state values are tcpStateCLOSED
, tcpStateLISTEN
, and tcpStateESTABLISHED
.
The argument socket specifies the socket handle.
The tcp_get_state function returns the current state of the TCP socket:
State | Description |
---|---|
tcpStateUNUSED | Socket is free and not allocated yet. The function cannot return this value. |
tcpStateCLOSED | Socket is allocated to an application but the connection is closed. |
tcpStateLISTEN | Socket is listening for incoming connections. |
tcpStateSYN_RECEIVED | Socket has received a TCP packet with the flag SYN set. |
tcpStateSYN_SENT | Socket has sent a TCP packet with the flag SYN set. |
tcpStateFIN_WAIT_1 | Socket has sent a FIN packet, to start the closing of the connection. |
tcpStateFIN_WAIT_2 | Socket has received acknowledgement from the remote machine for the FIN packet it sent out from the local machine. Socket is now waiting for a FIN packet from the remote machine. |
tcpStateCLOSING | Socket has received a FIN packet from the remote machine independently |
tcpStateLAST_ACK | Socket is waiting for the last ACK packet to the FIN packet it sent out. |
tcpStateTIME_WAIT | Socket is waiting on a 2 ms timeout before closing the connection. |
tcpStateESTABLISHED | Socket has established a TCP connection. You can transfer data only in this state. |
Code Example
netStatus tcp_listen | ( | int32_t | socket, |
uint16_t | port | ||
) |
Open TCP socket for incoming connection.
[in] | socket | socket handle obtained with tcp_get_socket. |
[in] | port | local port number. |
The function tcp_listen opens a socket for incoming connections by causing the socket to listen at a local TCP port.
The argument socket specifies the socket handle to listen on the local machine.
The argument port specifies the TCP port number to listen at.
TCP_TYPE_SERVER
or TCP_TYPE_CLIENT_SERVER
can call the tcp_listen function.Code Example
uint32_t tcp_max_data_size | ( | int32_t | socket | ) |
Determine maximum number of data bytes that can be sent in TCP packet.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_max_data_size determines the maximum number of bytes that can be sent in the TCP packet (Maximum Segment Size).
The argument socket specifies the handle of the TCP socket.
A default Maximum Segment Size of 1460 bytes is defined at start-up. However, when establishing a connection with a remote machine, the Network Core might negotiate a different (smaller) value for the Maximum Segment Size.
Code Example
netStatus tcp_release_socket | ( | int32_t | socket | ) |
Release TCP socket and free resources.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_release_socket de-allocates the memory used by the TCP socket.
The argument socket specifies the handle of the socket to be released.
Code Example
netStatus tcp_reset_window | ( | int32_t | socket | ) |
Reset TCP window size to a default value from the configuration.
[in] | socket | socket handle obtained with tcp_get_socket. |
The function tcp_reset_window resets the TCP window size to a default value defined with TCP_RECEIVE_WIN_SIZE
macro.
The argument socket specifies the handle of the socket for which to reset the window size.
This function can only be used with sockets that have a TCP flow control enabled. Enable a TCP flow control for the socket by calling the tcp_get_socket function with the TCP_TYPE_FLOW_CTRL
attribute set. This attribute enables using a Sliding Window protocol.
In Flow Control mode, each received data packet reduces the receiving Window Size by the number of data bytes received in the packet. Soon the window size becomes very small or 0. The remote host stops sending data and waits for a window update. As soon as the received data is processed, we can call a tcp_reset_window function to reopen the receiver window for further incoming data.
Depending on the context from where this function was called, it performs the following actions:
tcpStateESTABLISHED
state and the TCP_TYPE_FLOW_CTRL
attribute is not set.Code Example
netStatus tcp_send | ( | int32_t | socket, |
uint8_t * | buf, | ||
uint32_t | len | ||
) |
Send a data packet to remote node.
[in] | socket | socket handle obtained with tcp_get_socket. |
[in] | buf | buffer containing the data. |
[in] | len | length of data in bytes. |
The function tcp_send sends the data packet to a remote machine.
The argument socket specifies the socket handle to use for communication on the local machine.
The argument buf points to the constructed TCP data packet.
The argument len specifies the number of bytes in the data packet.
If the tcp_send fails to send the data, then it releases the memory buffer specified with the argument buf and returns with a status information. The function can not send data if:
Code Example