Keil Logo

Technical Support

On-Line Manuals

RL-ARM User's Guide (MDK v4)

RL-RTX RL-FlashFS RL-TCPnet RL-CAN RL-USB Example Programs Library Reference Data Types BIT BOOL CAN_ERROR CAN_msg CDC_LINE_CODING DCACHE FAT_VI FATINFO FCACHE FILE FINFO CID_REG FATVAR Media_INFO OS_ID RL_TIME S8 S16 S32 S64 U8 U16 U32 U64 Include Files can_cfg.h file_config.h net_config.h rtl.h rtx_can.h rtx_config.h stdio.h Reference _alloc_box _calloc_box _declare_box _declare_box8 _free_box _init_box _init_box8 accept ADC_IF_GetRequest ADC_IF_SetRequest arp_cache_ip arp_cache_mac arp_get_info bind CAN_init CAN_receive CAN_request CAN_rx_object CAN_send CAN_set CAN_start CAN_tx_object cgi_func cgi_process_data cgi_process_var cgx_content_type closesocket cls.config cls.get_last_error cls.init cls.unconfig cls.uninit com_getchar com_putchar com_tx_active connect dhcp_cbfunc dhcp_disable efs.EraseChip efs.EraseSector efs.Init efs.ProgramPage efs.ReadData efs.UnInit fanalyse fat.DeviceCtrl fat.Init fat.ReadInfo fat.ReadSect fat.UnInit fat.WriteSect fattrib fcheck fclose fdefrag fdelete feof ferror ffind fflush fformat ffree fgetc fgets finit FIQ_Handler fopen fprintf fputc fputs fread frename fs_get_date fs_get_time fscanf fseek ftell ftp_accept_host ftp_check_account ftp_evt_notify ftp_fclose ftp_fdelete ftp_ffind ftp_file_access ftp_fopen ftp_fread ftp_frename ftp_fwrite ftp_get_user_id ftpc_cbfunc ftpc_connect ftpc_fclose ftpc_fopen ftpc_fread ftpc_fwrite funinit fvol fwrite get_host_by_name gethostbyname getpeername getsockname HID_GetReport HID_SetReport http_accept_host http_check_account http_date http_fclose http_fgets http_file_access http_finfo http_fopen http_fread http_get_content_type http_get_info http_get_lang http_get_session http_get_user_id http_get_var icmp_ping igmp_join igmp_leave init_ethernet init_serial init_TcpNet int_disable_eth int_enable_eth interrupt_ethernet ioc_getcb ioc_init ioc_read_info ioc_read_sect ioc_uninit ioc_write_sect ioctlsocket isr_evt_set isr_mbx_check isr_mbx_receive isr_mbx_send isr_sem_send isr_tsk_get listen main_TcpNet mci.BusMode mci.BusSpeed mci.BusWidth mci.CheckMedia mci.Command mci.Delay mci.Init mci.ReadBlock mci.SetDma mci.UnInit mci.WriteBlock modem_dial modem_hangup modem_init modem_listen modem_online modem_process modem_run MSC_Inquiry MSC_MemoryRead MSC_MemoryVerify MSC_MemoryWrite nand.BlockErase nand.Init nand.PageRead nand.PageWrite nand.UnInit os_dly_wait os_evt_clr os_evt_get os_evt_set os_evt_wait_and os_evt_wait_or os_itv_set os_itv_wait os_mbx_check os_mbx_declare os_mbx_init os_mbx_send os_mbx_wait os_mut_init os_mut_release os_mut_wait os_resume os_sem_init os_sem_send os_sem_wait os_suspend os_sys_init os_sys_init_prio os_sys_init_user OS_Tick_Handler os_tick_init os_tick_irqack os_time_get os_tmr_call os_tmr_create os_tmr_kill os_tsk_create os_tsk_create_ex os_tsk_create_user os_tsk_create_user_ex os_tsk_delete os_tsk_delete_self os_tsk_pass os_tsk_prio os_tsk_prio_self os_tsk_self poll_ethernet ppp_close ppp_connect ppp_is_up ppp_listen recv recvfrom rewind send send_frame sendto slip_close slip_connect slip_is_up slip_listen smtp_accept_auth smtp_cbfunc smtp_connect snmp_set_community snmp_trap sntp_get_time socket spi.BusSpeed spi.CheckMedia spi.Init spi.RecBuf spi.Send spi.SendBuf spi.SetSS spi.UnInit tcp_abort tcp_check_send tcp_close tcp_connect tcp_get_buf tcp_get_socket tcp_get_state tcp_listen tcp_max_dsize tcp_release_socket tcp_reset_window tcp_send tftp_accept_host tftp_fclose tftp_fopen tftp_fread tftp_fwrite tftpc_fclose tftpc_fopen tftpc_fread tftpc_fwrite tftpc_get tftpc_put timer_tick tnet_accept_host tnet_cbfunc tnet_ccmp tnet_check_account tnet_get_info tnet_get_user_id tnet_msg_poll tnet_process_cmd tnet_set_delay tsk_lock tsk_unlock udp_close udp_get_buf udp_get_socket udp_mcast_ttl udp_open udp_release_socket udp_send ungetc USB_Core USB_Device USB_EndPoint0 USB_EndPointNumber USB_IRQHandler USB_ISR USB_ReadEP USB_TaskInit USB_WriteEP usbd_adc_init USBD_CDC_ACM_DataAvailable USBD_CDC_ACM_DataRead USBD_CDC_ACM_DataSend USBD_CDC_ACM_GetChar USBD_CDC_ACM_Notify USBD_CDC_ACM_PortGetLineCoding USBD_CDC_ACM_PortInitialize USBD_CDC_ACM_PortReset USBD_CDC_ACM_PortSetControlLineState USBD_CDC_ACM_PortSetLineCoding USBD_CDC_ACM_PortUninitialize USBD_CDC_ACM_PutChar usbd_cdc_init usbd_cdc_ser_availchar usbd_cdc_ser_closeport usbd_cdc_ser_initport usbd_cdc_ser_linestate usbd_cdc_ser_openport usbd_cdc_ser_read usbd_cdc_ser_write usbd_cls_dev_req usbd_cls_ep_req usbd_cls_if_req usbd_cls_init usbd_cls_sof usbd_configured usbd_connect usbd_hid_get_protocol usbd_hid_get_report usbd_hid_get_report_trigger usbd_hid_init usbd_hid_set_protocol usbd_hid_set_report usbd_init usbd_msc_init usbd_msc_read_sect usbd_msc_start_stop usbd_msc_write_sect usbd_reset_core usbd_vcom_chkserstate usbd_vcom_serial2usb usbd_vcom_usb2serial usbh_ehci_hw_delay_ms usbh_ehci_hw_get_capabilities usbh_ehci_hw_init usbh_ehci_hw_irq_en usbh_ehci_hw_pins_config usbh_ehci_hw_port_power usbh_ehci_hw_reg_rd usbh_ehci_hw_reg_wr usbh_engine usbh_engine_all usbh_get_error_string usbh_get_last_error usbh_hid_data_in usbh_hid_data_in_callback usbh_hid_data_out usbh_hid_get_last_error usbh_hid_kbd_getkey usbh_hid_mouse_getdata usbh_hid_parse_report_desc usbh_hid_status usbh_hw_delay_ms usbh_hw_ep_add usbh_hw_ep_config usbh_hw_ep_remove usbh_hw_get_capabilities usbh_hw_get_connect usbh_hw_get_speed usbh_hw_init usbh_hw_pins_config usbh_hw_port_power usbh_hw_port_reset usbh_hw_urb_cancel usbh_hw_urb_submit usbh_init usbh_init_all usbh_mem_alloc usbh_mem_free usbh_mem_init usbh_msc_get_last_error usbh_msc_read usbh_msc_read_config usbh_msc_status usbh_msc_write usbh_ohci_hw_delay_ms usbh_ohci_hw_get_capabilities usbh_ohci_hw_init usbh_ohci_hw_irq_en usbh_ohci_hw_pins_config usbh_ohci_hw_port_power usbh_ohci_hw_reg_rd usbh_ohci_hw_reg_wr usbh_transfer usbh_uninit usbh_uninit_all Library Files Appendix

tcp_get_socket

Summary
#include <rtl.h>

U8 tcp_get_socket (
    U8   type,          /* Type of TCP socket. */
    U8   tos,           /* Type Of Service. */
    U16  tout,          /* Idle timeout period before disconnecting. */
    U16 (*listener)(    /* Function to call when a TCP event occurs. */
        U8  socket,     /* Socket handle of the local machine. */
        U8  event,      /* TCP event such as connect, or close. */
        U8* ptr,        /* Pointer to IP address of remote machine, */
                        /*   or to buffer containing received data. */
        U16 par ));     /* Port number of remote machine, or length */
                        /* of received data. */
Description

The tcp_get_socket function 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 to 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, TCPnet disconnects the the TCP connection or sends a keep-alive packet if TCP_TYPE_KEEP_ALIVE attribute is set.

The argument listener is the event listening function of the TCP socket. TCPnet calls the listener function whenever a TCP event occurs. The arguments to the listener function are:

  • socket: TCP socket handle of the local machine.
  • event: Specifies the type of event that occurred as shown in the table below.
  • ptr: If event is TCP_EVT_DATA, ptr points to a buffer containing the received data. For all other events, ptr points to the IP address of the remote machine.
  • par: If event is TCP_EVT_DATA, par specifies the number of bytes of data received. For all other events, par specifies the port number used by the remote machine.

TCPnet uses the return value of the callback function listener only when the event is TCP_EVT_CONREQ. 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, TCPnet accepts the incoming connection. If the listener function returns 0, TCPnet rejects the incoming connection. You can thus define the listener function to selectively reject incoming connections from particular IP addresses.

Event Type Description
TCP_EVT_CONREQ A Connect Request has been received from a remote client that wants to connect to the server running on TCPnet.
TCP_EVT_CONNECT The TCP socket has connected to the remote machine.
TCP_EVT_CLOSE The TCP connection has been properly closed.
TCP_EVT_ABORT The TCP connection has been aborted.
TCP_EVT_ACK Acknowledgement has been received from the remote host for the previously sent data.
TCP_EVT_DATA A TCP data packet has been received.

The tcp_get_socket function is in the RL-TCPnet library. The prototype is defined in rtl.h.

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 longstanding connection.
Return Value

The tcp_get_socket function returns the handle of the allocated TCP socket. If the function could not allocate a socket, it returns 0.

See Also

tcp_connect, tcp_listen, tcp_release_socket, tcp_reset_window

Example
#include <rtl.h>

U8 tcp_soc;

U16 tcp_callback (U8 soc, U8 event, U8 *ptr, U16 par) {
  /* This function is called on TCP event */
   ..
  switch (event) {
    case TCP_EVT_CONREQ:
      /* Remote host is trying to connect to our TCP socket. */
      /* 'ptr' points to Remote IP, 'par' holds the remote port. */
       ..
      /* Return 1 to accept connection, or 0 to reject connection */
      return (1);
    case TCP_EVT_ABORT:
      /* Connection was aborted */
       ..
      break;
    case TCP_EVT_CONNECT:
      /* Socket is connected to remote peer. */
       ..
      break;
    case TCP_EVT_CLOSE:
      /* Connection has been closed */
       ..
      break;
    case TCP_EVT_ACK:
      /* Our sent data has been acknowledged by remote peer */
       ..
      break;
    case TCP_EVT_DATA:
      /* TCP data frame has been received, 'ptr' points to data */
      /* Data length is 'par' bytes */
       ..
      break;
  }
  return (0);
}

void main (void) {

  init ();
  /* Initialize the TcpNet */
  init_TcpNet ();
  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 TcpNet 'thread' */
    main_TcpNet ();
     ..
  }
}
  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.