Keil Logo

Technical Support

On-Line Manuals

RL-ARM User's Guide (MDK v4)

RL-RTX RL-FlashFS RL-TCPnet TCP Socket Opening TCP Connection TCP Active Open TCP Passive Open Sending TCP Data Example for Sending Data Multiple TCP Connections UDP Socket Opening UDP Connection Sending UDP Data When DHCP Enabled When ARP Cache Empty Example for Sending Data IP Multicasting Multiple UDP Connections Configuring RL-TCPnet Static Configuration System Definitions Ethernet Network Interface PPP Network Interface SLIP Network Interface UDP Socket TCP Socket BSD Socket HTTP Server Telnet Server TFTP Server TFTP Client FTP Server FTP Client DNS Client SMTP Client SNMP Agent SNTP Client Error Function Runtime Configuration Library Files Using RL-TCPnet Stand Alone With RTX Kernel Event Driven Operation IP Address Assignment Ethernet Interface PPP Interface SLIP Interface Localhost Applications HTTP Server Script Language CGI Functions Ajax Support Using XML XML Example How it works SOAP Support SOAP Interface Large POST Messages Web Pages Default Page Error Pages Web on SD Card Web Update File System Interface Http Caching How it works Internal Web External Web Multi-user Authentication Using RAM File System FCARM File Converter PRINT Directive NOPRINT Directive PAGEWIDTH Directive PAGELENGTH Directive ROOT Directive Telnet Server Command Line Interface Multi-user Authentication Sending Reply Message Short Reply Long Reply Continuous Screen Update TFTP Server File System Interface TFTP Client File System Interface FTP Server File System Interface Multi-user Authentication Supported Commands FTP Client File System Interface SMTP Client SNMP Agent MIB Database MIB Interface MIB Entry MIB Table DNS Resolver Starting DNS Device Drivers Ethernet Driver Interrupt Mode Modem Driver Serial Driver Using Serial Link Cable Connection Modem Connection Windows Dial-up Add Direct Serial Link New Dial-up Connection Configure PPP Dial-up Configure SLIP Dial-up Debugging Enabling Debug Debug Level Redirecting Output Function Overview BSD Routines CGI Routines Ethernet Routines FTP Routines HTTP Routines IGMP Routines Miscellaneous Routines Modem Routines PPP Routines Serial Routines SLIP Routines SMTP Routines SNMP Routines System Functions TCP Routines Telnet Routines TFTP Routines UDP Routines RL-CAN RL-USB Example Programs Library Reference Appendix

Example for Sending Data

In the following example, the basic concept is to send large amounts of data using a TCP socket. This example sends 64 Kbytes to the remote IP address 192.168.0.100, which is listening on port 1000. The TCP socket is permanently allocated and is not released when the data is sent or when the connection is closed.

  1. Initialize the RL-TCPnet system and allocate a free TCP socket:
    #include <rtl.h>
    
    U8 tcp_soc;
    U8 soc_state;
    BOOL wait_ack;
    
    void main (void) {
      init_ ();
      tcp_soc = tcp_get_socket (TCP_TYPE_CLIENT, 0, 120, tcp_callback);
      soc_state = 0;
    
  2. Run the main thread of the RL-TCPnet system and call the send_data() function from an endless loop:
      while (1) {
        timer_poll ();
        main_TcpNet ();
        send_data ();
      }
    }
    
  3. The send_data() function must be implemented as a state machine. It opens an Active TCP connection, sends data, and closes the TCP connection in the end. When the soc_state is 0, the connection is initiated:
    void send_data (void) {
      static const U8 rem_IP[4] = {192,168,0,100};
      static int bcount;
      U32 max;
      U8 *sendbuf;
    
      switch (soc_state) {
        case 0:
          tcp_connect (tcp_soc, rem_IP, 1000, 0);
          bcount    = 0;
          wait_ack  = __FALSE;
          soc_state = 1;
          return;
    
  4. Next, state 1 is waiting for the TCP_EVT_CONNECT event. This event is received in the tcp_callback() event callback function, which places the send_data process into state 2 (sending data state).
  5. In state 2, allocate the maximum possible size of transmit buffer, fill it with some data, and send it. The maximum possible transmit buffer is allocated to reduce the number of packets and improve the transfer speed.

    After the packet is sent, wait for the remote acknowledge before proceeding with the next data packet.

        case 2:
          if (wait_ack == __TRUE) {
            return;
          }
          max = tcp_max_dsize (tcp_soc);
          sendbuf = tcp_get_buf (max);
          for (i = 0; i < max; i += 2) {
            sendbuf[i]   = bcount >> 8;
            sendbuf[i+1] = bcount & 0xFF;
            if (bcount >= 32768) {
              soc_state = 3;
              break;
            }
          }
          tcp_send (tcp_soc, sendbuf, i);
          wait_ack = __TRUE;
          return;
    
  6. State 3 is achieved when the data transfer is finished. Wait for the last packet to be acknowledged and then close the TCP connection.
        case 3:
          if (wait_ack == __TRUE) {
            return;
          }
          tcp_close (tcp_soc);
          soc_state = 4;
          return;
      }
    }
    
  7. The embedded application waits for the TCP socket to connect before starting to send data. When the data packet is sent, the application waits for the acknowledge before creating and sending the next data packet. Use the callback listener function to wait for the remote acknowledge.
    U16 tcp_callback (U8 soc, U8 event, U8 *ptr, U16 par) {
      /* This function is called on TCP event */
    
      switch (event) {
         ..
        case TCP_EVT_CONNECT:
          /* Socket is now connected and ready to send data. */
          soc_state = 2;
          break;
        case TCP_EVT_ACK:
          /* Our sent data has been acknowledged by remote peer */
          wait_ack = __FALSE;
          break;
           ..
      }
      return (0);
    }
    

Note

  • This assumes that the Network Interface Adapter is selected, enabled, and properly configured in the Net_Config.c configuration file.
  • If the system runs out of TCP sockets, the application hangs in an endless loop in the system error function with the error code ERR_TCP_ALLOC.
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.