Sending and Receiving through a socket

Sending and Receiving through a socket

Home Forums Networking Software Sending and Receiving through a socket

Tagged: ,

This topic contains 3 replies, has 3 voices, and was last updated by  steve smith 2 years, 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #17307

    toyo ayeni
    Participant

    hi

    I’m using a similar configuration as the webserver-no-src example to implement a tcp/ip server on the STM32F7 discovery board (uC/OS-III, uC/TCPIP). This server can both send and receive through a socket.
    I’m using the NetSock_RxDataFrom() api to receive and the NetSock_TxDataTo() to send.
    When the task receiving data from a connected client receives, the first character in the stream is a
    random character. The sending task also appends the text data with a random character.

    Can someone explain whats going on?

    #17533

    Mark Mulrooney
    Moderator

    We’re not aware of any issues in the STM32F7 TCP/IP examples. Could you provide the application code that is causing the problem?

    #17574

    toyo ayeni
    Participant

    Mark

    Code =>

    *** I’ve got a live watch on rx_buf to see its content when a connected client sends data. The first byte in the array is always junk. no clue where its coming from.

    I’m following an example I found in the uC-TCPIP folder to initialize the server.

    #include <stdarg.h>
    #include <stdio.h>
    #include <stm32f7xx_hal.h>

    #include <cpu.h>
    #include <lib_math.h>
    #include <lib_mem.h>
    #include <os.h>
    #include <os_app_hooks.h>

    #include <app_cfg.h>
    #include <bsp.h>
    #include <bsp_led.h>
    #include <bsp_clock.h>
    #include <Source/net_cfg_net.h>
    #include <Source/net_sock.h>
    #include <Source/net_app.h>
    #include <Source/net_util.h>
    #include <Source/net_ascii.h>
    #include <app_tcpip.h>

    /*
    *********************************************************************************************************
    * LOCAL DEFINES
    *********************************************************************************************************
    */

    #define TCP_SERVER_PORT 7004
    #define TX_BUF_SIZE 100

    /*
    *********************************************************************************************************
    * LOCAL GLOBAL VARIABLES
    *********************************************************************************************************
    */

    /* ————— APPLICATION GLOBALS —————- */
    static OS_TCB AppTaskStartTCB;
    static CPU_STK AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE];

    static OS_TCB AppTCPServerTaskTCB;
    static CPU_STK AppTCPServerTaskStk[APP_CFG_TCP_SERVER_TASK_STK_SIZE];

    /*
    *********************************************************************************************************
    * FUNCTION PROTOTYPES
    *********************************************************************************************************
    */

    static void AppTaskStart (void *p_arg);
    static void AppTCPServerTask (void *p_arg);

    /*
    *********************************************************************************************************
    * main()
    *
    * Description : This is the standard entry point for C code. It is assumed that your code will call
    * main() once you have performed all necessary initialization.
    *
    * Arguments : none
    *
    * Returns : none
    *
    * Notes : 1) HAL library initialization:
    * a) Configures the Flash prefetch, intruction and data caches.
    * b) Configures the Systick to generate an interrupt. However, the function ,
    * HAL_InitTick(), that initializes the Systick has been overwritten since Micrium’s
    * RTOS has its own Systick initialization and it is recommended to initialize the
    * Systick after multitasking has started.
    *
    *********************************************************************************************************
    */

    int main(void)
    {
    OS_ERR err;

    HAL_Init(); /* See Note 1. */

    BSP_SystemClkCfg(); /* Initialize CPU clock frequency to 216Mhz */

    CPU_Init(); /* Initialize the uC/CPU services */

    Mem_Init(); /* Initialize Memory Managment Module */
    Math_Init(); /* Initialize Mathematical Module */

    CPU_IntDis(); /* Disable all Interrupts. */

    OSInit(&err); /* Init uC/OS-III. */
    App_OS_SetAllHooks();

    OSTaskCreate(&AppTaskStartTCB, /* Create the start task */
    “App Task Start”,
    AppTaskStart,
    0u,
    APP_CFG_TASK_START_PRIO,
    &AppTaskStartStk[0u],
    AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE / 10u],
    APP_CFG_TASK_START_STK_SIZE,
    0u,
    0u,
    0u,
    (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
    &err);

    OSStart(&err); /* Start multitasking (i.e. give control to uC/OS-III). */

    while (DEF_ON) { /* Should Never Get Here. */
    ;
    }
    }

    /*
    *********************************************************************************************************
    * STARTUP TASK
    *
    * Description : This is an example of a startup task. As mentioned in the book’s text, you MUST
    * initialize the ticker only once multitasking has started.
    *
    * Arguments : p_arg is the argument passed to ‘AppTaskStart()’ by ‘OSTaskCreate()’.
    *
    * Returns : none
    *
    * Notes : 1) The first line of code is used to prevent a compiler warning because ‘p_arg’ is not
    * used. The compiler should not generate any code for this statement.
    *********************************************************************************************************
    */

    static void AppTaskStart (void *p_arg)
    {
    #if (APP_CFG_TCPIP_EN == DEF_ENABLED)
    CPU_BOOLEAN started;
    #endif
    OS_ERR err;

    (void)p_arg;

    BSP_Init(); /* Initialize BSP functions */

    #if OS_CFG_STAT_TASK_EN > 0u
    OSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */
    #endif

    #ifdef CPU_CFG_INT_DIS_MEAS_EN
    CPU_IntDisMeasMaxCurReset();
    #endif

    /*********************************************************************
    ** TCP/IP initialization
    **********************************************************************/

    started = AppTCPIP_Init(); /* Initialize uC/TCPIP Module. */

    if (started != DEF_YES) {
    APP_TRACE_INFO((“Initializing uC/TCP-IP … FAILED!! \r\n”));
    }

    //create the TCP server task
    OSTaskCreate(&AppTCPServerTaskTCB,
    “App TCP Server Task”,
    AppTCPServerTask,
    0u,
    APP_CFG_TCP_SERVER_TASK_PRIO,
    &AppTCPServerTaskStk[0u],
    AppTCPServerTaskStk[APP_CFG_TCP_SERVER_TASK_STK_SIZE / 10u],
    APP_CFG_TCP_SERVER_TASK_STK_SIZE,
    0u,
    0u,
    0u,
    (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
    &err);

    while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
    BSP_LED_Toggle(0u);
    OSTimeDlyHMSM(0u, 0u, 1u, 0u,
    OS_OPT_TIME_HMSM_STRICT,
    &err);
    }
    }

    /*
    *********************************************************************************************************
    * AppTCPServerTask()
    *
    * Description : TCP Server Application Task.
    *
    * Argument(s) : none
    *
    * Return(s) : none
    *
    * Caller(s) : AppTaskStart()
    *
    * Note(s) : none.
    *********************************************************************************************************
    */

    static void AppTCPServerTask (void *p_arg)
    {
    NET_SOCK_ID sock_listen;
    NET_SOCK_ID sock_child;
    NET_SOCK_ADDR_IPv4 server_sock_addr_ip;
    NET_SOCK_ADDR_IPv4 client_sock_addr_ip;
    NET_SOCK_ADDR_LEN client_sock_addr_ip_size;
    NET_SOCK_RTN_CODE rx_size;
    CPU_CHAR rx_buf[RX_BUF_SIZE];
    NET_SOCK_RTN_CODE tx_size;
    NET_SOCK_DATA_SIZE tx_rem;
    CPU_INT32U home_ntwk = NET_IPv4_ADDR_THIS_HOME_NTWK;
    CPU_INT08U *p_buf;
    CPU_BOOLEAN fault_err;
    NET_ERR err;
    OS_ERR err1;

    (void)&p_arg; // to prevent compiler warning

    while(DEF_TRUE)
    {
    // —————– OPEN IPV4 SOCKET —————–
    sock_listen = NetSock_Open(NET_SOCK_PROTOCOL_FAMILY_IP_V4,
    NET_SOCK_TYPE_STREAM,
    NET_SOCK_PROTOCOL_TCP,
    &err);
    if (err != NET_SOCK_ERR_NONE) {
    return;
    }

    // ———— CONFIGURE SOCKET’S ADDRESS ————
    NetApp_SetSockAddr((NET_SOCK_ADDR *)&server_sock_addr_ip,
    NET_SOCK_ADDR_FAMILY_IP_V4,
    TCP_SERVER_PORT,
    (CPU_INT08U *)&home_ntwk,
    NET_IPv4_ADDR_SIZE,
    &err);
    switch (err) {
    case NET_APP_ERR_NONE:
    break;

    case NET_APP_ERR_FAULT:
    case NET_APP_ERR_NONE_AVAIL:
    case NET_APP_ERR_INVALID_ARG:
    default:
    NetSock_Close(sock_listen, &err);
    return;
    }

    // ——————- BIND SOCKET ——————–
    NetSock_Bind( sock_listen,
    (NET_SOCK_ADDR *)&server_sock_addr_ip,
    NET_SOCK_ADDR_SIZE,
    &err);
    if (err != NET_SOCK_ERR_NONE) {
    NetSock_Close(sock_listen, &err);
    return;
    }

    // —————— LISTEN SOCKET ——————-
    NetSock_Listen(sock_listen, 1, &err);
    if (err != NET_SOCK_ERR_NONE) {
    NetSock_Close(sock_listen, &err);
    return;
    }

    fault_err = DEF_NO;

    while (fault_err == DEF_NO) {
    client_sock_addr_ip_size = NET_SOCK_ADDR_IPv4_SIZE;

    // ———- ACCEPT NEW INCOMING CONNECTION ———-
    sock_child = NetSock_Accept( sock_listen,
    (NET_SOCK_ADDR *)&client_sock_addr_ip,
    &client_sock_addr_ip_size,
    &err);
    switch (err) {
    case NET_SOCK_ERR_NONE:

    do {
    // —– WAIT UNTIL RECEIVING DATA FROM A CLIENT ——
    client_sock_addr_ip_size = sizeof(client_sock_addr_ip);
    rx_size = NetSock_RxDataFrom( sock_child,
    rx_buf,
    RX_BUF_SIZE,
    NET_SOCK_FLAG_NONE,
    (NET_SOCK_ADDR *)&client_sock_addr_ip,
    &client_sock_addr_ip_size,
    DEF_NULL,
    DEF_NULL,
    DEF_NULL,
    &err);

    } while (fault_err == DEF_NO);

    // —————- CLOSE CHILD SOCKET —————-
    NetSock_Close(sock_child, &err);
    if (err != NET_SOCK_ERR_NONE) {
    fault_err = DEF_YES;
    }

    break;

    case NET_SOCK_ERR_NONE_AVAIL:
    case NET_SOCK_ERR_CONN_SIGNAL_TIMEOUT:
    break;

    default:
    fault_err = DEF_YES;
    break;
    }
    }

    OSTimeDlyHMSM(0u, 0u, 1u, 0u,
    OS_OPT_TIME_HMSM_STRICT,
    &err);

    }//end while(DEF_TRUE)

    // ————- FATAL FAULT SOCKET ERROR ————-
    NetSock_Close(sock_listen, &err); // This function should be reached only when a fatal …
    // fault error has occurred.
    }

    #19940

    steve smith
    Participant

    Hello everyone
    I am new here and i also need application code.
    best regards
    steve smith

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

View the complete site map

x
Loading...