Network Component  Version 6.6
MDK-Professional Middleware for IP Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TCP Socket

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.
 

Description

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.

Note
More information on the usage of TCP and how to work with the functions is available at TCP Socket.

Typedef Documentation

uint32_t(* net_tcp_cb_t)(int32_t socket, tcpEvent event, const uint8_t *buf, uint32_t len)

TCP Event callback function.

Parameters
[in]socketTCP socket handle of the local machine.
[in]eventEvent type as shown in the table below (tcpEvent).
[in]bufPointer to a buffer containing the received data or to the IP address of the remote machine (see below).
[in]lenNumber of data bytes or port number of the remote machine (see below).
Returns
Integer Value used to decide whether to accept or reject an incoming connection (see details).
  • 1 - incomming connection accepted.
  • 0 - incomming connection rejected.

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
tcpEventEstablishedThe 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:

Enumeration Type Documentation

enum tcpEvent

TCP Callback Events.

Parameter for:

Enumerator:
tcpEventConnect 

Connect request received event.

tcpEventEstablished 

Connection established event.

tcpEventClosed 

Connection was properly closed.

tcpEventAbort 

Connection is for some reason aborted.

tcpEventACK 

Previously send data acknowledged.

tcpEventData 

Data received event.

enum tcpState

TCP States.

Returned by:

Enumerator:
tcpStateUNUSED 

Entry is free and unused.

tcpStateCLOSED 

Entry allocated, socket still closed.

tcpStateLISTEN 

Socket waiting for incoming connection.

tcpStateSYN_RECEIVED 

SYN frame received.

tcpStateSYN_SENT 

SYN packet sent to establish a connection.

tcpStateFIN_WAIT_1 

Tcp_close started FIN packet was sent.

tcpStateFIN_WAIT_2 

Our FIN ack-ed, waiting for remote FIN.

tcpStateCLOSING 

Received FIN independently of our FIN.

tcpStateLAST_ACK 

Waiting for last ACK for our FIN.

tcpStateTIME_WAIT 

Timed waiting for 2MSL.

tcpStateESTABLISHED 

TCP Connection established.

Function Documentation

netStatus tcp_abort ( int32_t  socket)

Instantly stop TCP communication.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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.

Note
After calling tcp_abort you cannot use the socket to send or receive any data. The socket remains allocated until you release it.

Code Example

#include "rl_net.h"
void disconnect_tcp (int32_t tcp_soc) {
..
// This TCP connection needs to close immediately
tcp_abort (tcp_soc);
// Socket will not be needed any more
tcp_release_socket (tcp_soc);
}
bool tcp_check_send ( int32_t  socket)

Check if TCP socket can send data.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
send status:
  • true = Ready to send data.
  • false = Not ready.

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

#include "rl_net.h"
void send_data () {
const uint8_t rem_ip[4] = {192,168,1,100};
uint8_t *sendbuf;
switch (tcp_get_state (socket_tcp)) {
// Connection idle, send Connect Request.
tcp_connect (socket_tcp, rem_ip, 1001, 0);
break;
// We are connected, send command to remote peer.
if (tcp_check_send (socket_tcp)) {
// OK, socket is ready to send data.
sendbuf = tcp_get_buf (2);
sendbuf[0] = BLINKLED;
sendbuf[1] = p2;
tcp_send (socket_tcp, sendbuf, SENDLEN);
}
break;
}
}
netStatus tcp_close ( int32_t  socket)

Stop TCP communication and start closing procedure.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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.

Note
After calling tcp_close the socket still remains allocated until you release it.

Code Example

#include "rl_net.h"
void disconnect_tcp (int32_t tcp_soc) {
..
// This TCP connection is no longer needed
tcp_close (tcp_soc);
// Release TCP Socket in a polling function
}
void poll_socket (int32_t tcp_soc) {
int state;
state = tcp_get_state (tcp_soc);
if (state > tcpStateLISTEN) {
// Closing procedure is on-going
return;
}
if (state == tcpStateLISTEN) [
// Socket has TCP_TYPE_SERVER attribute
// needs additional close request.
tcp_close (tcp_soc);
}
// A socket is in tcpStateCLOSED state now.
tcp_release_socket (tcp_soc);
}
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.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
[in]ip_addrIP address of the remote node.
[in]portport number of the remote node.
[in]local_portlocal port number or 0 for system assigned local port.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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.

Note
Only a socket of type TCP_TYPE_CLIENT or TCP_TYPE_CLIENT_SERVER can call the tcp_connect function.

Code Example

#include "rl_net.h"
int32_t tcp_soc;
uint32_t tcp_callback (int32_t soc, tcpEvent event, const uint8_t *buf, uint32_t len) {
// This function is called on TCP event
..
return (0);
}
void main (void) {
const uint8_t rem_ip[4] = {192,168,1,110};
init ();
// Initialize the Network Core
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER, 0, 30, tcp_callback);
if (tcp_soc >= 0) {
// Start connection
tcp_connect (tcp_soc, rem_ip, 80, 1000);
}
while (1) {
// Run main Network Core 'thread'
..
}
}
uint8_t * tcp_get_buf ( uint32_t  size)

Allocate memory for TCP send buffer.

Parameters
[in]sizenumber of bytes to allocate.
Returns
pointer to the allocated memory.

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.

Note
  • Your application must call the tcp_get_buf function each time it wants to send a TCP data packet.
  • The size of the allocated memory must not exceed the TCP Maximum Segment Size (1460 bytes).
  • Writing more data than the allocated size of the data buffer overwrites the Memory Manager Block links and causes the Network Core to crash.

Code Example

#include "rl_net.h"
#include <string.h>
bool send_datalog () {
uint8_t *sendbuf;
uint32_t maxlen;
if (tcp_check_send (tcp_soc)) {
// The socket is ready to send the data.
maxlen = tcp_max_dsize (tcp_soc);
sendbuf = tcp_get_buf (maxlen);
memcpy (sendbuf, data_buf, maxlen);
tcp_send (tcp_soc, sendbuf, maxlen);
return (true);
}
return (false);
}
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.

Parameters
[in]typesocket type:
  • TCP_TYPE_SERVER = Able to listen.
  • TCP_TYPE_CLIENT = Able to initiate a connection.
  • TCP_TYPE_DELAY_ACK = Send delayed ack.
  • TCP_TYPE_FLOW_CTRL = Control data flow.
  • TCP_TYPE_KEEP_ALIVE = Keep a connection alive.
[in]tosIP type of service:
  • 0 = default value.
[in]toutidle timeout in seconds.
[in]cb_funcevent listening callback function.
Returns
socket handle number or execution status:
  • value >= 0: socket handle number
  • value < 0: error occurred, -value is execution status as defined with netStatus

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_SERVERThe 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_ALIVEThe 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.

Note
  • You must call the tcp_get_socket function before any other function calls to the TCP socket.
  • You must define the listener function to use with the TCP socket.
  • You must use the TCP_TYPE_KEEP_ALIVE attribute for a long-standing connection.

Code Example

#include "rl_net.h"
int32_t tcp_soc;
uint32_t tcp_callback (int32_t soc, tcpEvent event, const uint8_t *buf, uint32_t len) {
// This function is called on TCP event
..
switch (event) {
// Remote host is trying to connect to our TCP socket.
// 'buf' points to Remote IP, 'len' holds the remote port.
..
// Return 1 to accept connection, or 0 to reject connection
return (1);
// Connection was aborted
..
break;
// Socket is connected to remote peer.
..
break;
// Connection has been closed
..
break;
// Our sent data has been acknowledged by remote peer
..
break;
// TCP data frame has been received, 'buf' points to data
// Data length is 'len' bytes
..
break;
}
return (0);
}
void main (void) {
init ();
// Initialize the Network Core
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER, 0, 30, tcp_callback);
if (tcp_soc >= 0) {
// Start listening on TCP port 80
tcp_listen (tcp_soc, 80);
}
while (1) {
// Run main Network Core 'thread'
..
}
}
tcpState tcp_get_state ( int32_t  socket)

Determine current state of a TCP socket.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
status information as defined with tcpState.

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

#include "rl_net.h"
void send_data () {
const uint8_t rem_ip[4] = {192,168,1,100};
uint8_t *sendbuf;
switch (tcp_get_state (socket_tcp)) {
// Connection idle, send Connect Request.
tcp_connect (socket_tcp, rem_ip, 1001, 0);
break;
// We are connected, send command to remote peer.
if (tcp_check_send (socket_tcp)) {
// OK, socket is ready to send data.
sendbuf = tcp_get_buf (2);
sendbuf[0] = BLINKLED;
sendbuf[1] = p2;
tcp_send (socket_tcp, sendbuf, SENDLEN);
}
break;
}
}
netStatus tcp_listen ( int32_t  socket,
uint16_t  port 
)

Open TCP socket for incoming connection.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
[in]portlocal port number.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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.

Note
  • Server applications (such as Telnet and HTTP server) must open a TCP socket for listening.
  • Only sockets of type TCP_TYPE_SERVER or TCP_TYPE_CLIENT_SERVER can call the tcp_listen function.

Code Example

#include "rl_net.h"
int32_t tcp_soc;
uint32_t tcp_callback (int32_t soc, tcpEvent event, const uint8_t *buf, uint32_t len) {
// This function is called on TCP event
..
switch (event) {
// Remote host is trying to connect to our TCP socket.
// 'buf' points to Remote IP, 'len' holds the remote port.
..
// Return 1 to accept connection, or 0 to reject connection
return (1);
// Connection was aborted
..
break;
// Socket is connected to remote peer.
..
break;
// Connection has been closed
..
break;
// Our sent data has been acknowledged by remote peer
..
break;
// TCP data frame has been received, 'buf' points to data
// Data length is 'len' bytes
..
break;
}
return (0);
}
void main (void) {
init ();
// Initialize the Network Core
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER, 0, 30, tcp_callback);
if (tcp_soc >= 0) {
// Start listening on TCP port 80
tcp_listen (tcp_soc, 80);
}
while (1) {
// Run main Network Core 'thread'
..
}
}
uint32_t tcp_max_data_size ( int32_t  socket)

Determine maximum number of data bytes that can be sent in TCP packet.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
maximum segment size in bytes.

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

#include "rl_net.h"
#include <string.h>
bool send_datalog () {
uint8_t *sendbuf;
uint32_t maxlen;
if (tcp_check_send (tcp_soc)) {
// The socket is ready to send the data.
maxlen = tcp_max_data_size (tcp_soc);
sendbuf = tcp_get_buf (maxlen);
memcpy (sendbuf, data_buf, maxlen);
tcp_send (tcp_soc, sendbuf, maxlen);
return (true);
}
return (false);
}
netStatus tcp_release_socket ( int32_t  socket)

Release TCP socket and free resources.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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.

Note
  • You must call the tcp_release_socket function when you do not need the TCP socket any longer.
  • After calling tcp_release_socket the socket is free to use by another process.

Code Example

#include "rl_net.h"
void disconnect_tcp (int32_t tcp_soc) {
..
// This TCP connection needs to close immediately
tcp_abort (tcp_soc);
// Socket will not be needed any more
tcp_release_socket (tcp_soc);
}
netStatus tcp_reset_window ( int32_t  socket)

Reset TCP window size to a default value from the configuration.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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:

  • resets the window size of the socket and returns if called from the callback function. The window size is actually changed in the acknowledge packet generated by the Network Core when the callback function returns.
  • resets the window size and sends out a Window Update packet if called from the other part of the user application.
  • does nothing if the socket is not in tcpStateESTABLISHED state and the TCP_TYPE_FLOW_CTRL attribute is not set.

Code Example

#include "rl_net.h"
int32_t tcp_soc;
uint8_t buf[TCP_RECEIVE_WIN_SIZE];
uint32_t head, tail;
void send_to_uart (void) {
// Send the data received from TCP to UART.
if (uart_busy () || head == tail) {
// Do nothing if UART is busy or when 'buf' is empty.
return;
}
send_uart (buf[tail++]);
if (tail == head) {
// The 'buf' is empty, all bytes sent out to UART.
tail = 0;
head = 0;
tcp_reset_window (tcp_soc);
}
}
uint32_t tcp_callback (int32_t soc, tcpEvent event, const uint8_t *buf, uint32_t len) {
// This function is called on TCP event
..
switch (event) {
// Remote host is trying to connect to our TCP socket.
// 'buf' points to Remote IP, 'len' holds the remote port.
// Return 1 to accept connection, or 0 to reject connection
return (1);
// Connection was aborted
tcp_soc = 0;
break;
// Socket is connected to remote peer.
tcp_soc = soc;
break;
// Connection has been closed
tcp_soc = 0;
break;
// Our sent data has been acknowledged by remote peer
break;
// TCP data frame has been received, 'buf' points to data
// Data length is 'len' bytes
memcpy (&buf[head], buf, len);
head += par;
break;
}
return (0);
}
void main (void) {
init ();
// Initialize the Network Core
0, 30, tcp_callback);
if (tcp_soc >= 0) {
// Start listening on TCP port 8080
tcp_listen (tcp_soc, 8080);
}
head = 0;
tail = 0;
while (1) {
// Run main Network Core 'thread'
send_to_uart ();
..
}
}
netStatus tcp_send ( int32_t  socket,
uint8_t *  buf,
uint32_t  len 
)

Send a data packet to remote node.

Parameters
[in]socketsocket handle obtained with tcp_get_socket.
[in]bufbuffer containing the data.
[in]lenlength of data in bytes.
Returns
status code that indicates the execution status of the function as defined with netStatus.

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:

  • the TCP connection is not established
  • previously sent data is not acknowledged from the remote machine.
Note
  • You must allocate the memory using tcp_get_buf before calling tcp_send.
  • The socket must already be opened and connected for communication.
  • It is not allowed to call tcp_send from the callback function of the socket.

Code Example

#include "rl_net.h"
#include <string.h>
bool send_datalog () {
uint8_t *sendbuf;
uint32_t maxlen;
if (tcp_check_send (tcp_soc)) {
// The socket is ready to send the data.
maxlen = tcp_max_data_size (tcp_soc);
sendbuf = tcp_get_buf (maxlen);
memcpy (sendbuf, data_buf, maxlen);
tcp_send (tcp_soc, sendbuf, maxlen);
return (true);
}
return (false);
}