This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM2634: How to work with multiple clients with 2 different board

Part Number: AM2634

Hi TI,

I am working on 2 different different customized board in that one board i was running client program and another board i was running server program so 

Board 1 - as Client which is continiously sending the data

Board 2 - as Server which is waiting for client msg once client message we got it will print like data received.

Client code:

void AppSocket_showMenu_1(void);
void clientTest();
/* ========================================================================== */
/*                         Structure Declarations                             */
/* ========================================================================== */
 
struct App_hostInfo_t
{
    struct sockaddr_in socketAddr;
};
 
/* ========================================================================== */
/*                          Function Declarations                             */
/* ========================================================================== */
static void Appsocket_fillHostSocketInfo(struct App_hostInfo_t* pHostInfo,uint16_t port);
 
/* ========================================================================== */
/*                            Global Variables                                */
/* ========================================================================== */
 
static uint8_t gRxDataBuff[APP_SOCKET_MAX_RX_DATA_LEN];
 
struct App_hostInfo_t gHostInfo[6];
 
static char   gHostServerIp4[MAX_IPV4_STRING_LEN] = "192.168.2.250";
uint32_t udp_count_1 = 0U;
/* ========================================================================== */
/*                          Function Definitions                              */
/* ========================================================================== */
 
 
static void Appsocket_fillHostSocketInfo(struct App_hostInfo_t* pHostInfo,uint16_t port)
{
    ip_addr_t ipAddr;
    int32_t addr_ok;
    memset(&pHostInfo->socketAddr, 0, sizeof(pHostInfo->socketAddr));
    struct sockaddr_in*  pAddr = &pHostInfo->socketAddr;
 
 
    IP_SET_TYPE_VAL(dstaddr, IPADDR_TYPE_V4);
    addr_ok = ip4addr_aton(gHostServerIp4, ip_2_ip4(&ipAddr));
 
    pAddr->sin_len = sizeof(pHostInfo->socketAddr);
    pAddr->sin_family = AF_INET;
    pAddr->sin_port = PP_HTONS(port);
    inet_addr_from_ip4addr(&pAddr->sin_addr, ip_2_ip4(&ipAddr));
    EnetAppUtils_assert(addr_ok);
 
    return;
}
 
static void AppSocket_simpleClient(void* pArg)
{
    struct sockaddr* pAddr = pArg;
    int32_t sock = -1, ret = 0;
    uint32_t len = 0, buf_len = 0;
    struct timeval opt = {0};
 
//    for (uint32_t i = 0; i < APP_SOCKET_NUM_ITERATIONS; i++)
    {
        EnetAppUtils_print(" Connecting to: %s:%" PRId32 "\r\n", gHostServerIp4, SOCK_HOST_SERVER_PORT);
        sock = lwip_socket(pAddr->sa_family, SOCK_DGRAM, 0);          /* create the socket */
        if (sock < 0)
        {
            EnetAppUtils_print("ERR: unable to open socket\r\n");
//            continue;
        }
        /* set recv timeout (100 ms) */
        opt.tv_sec = 0;
        opt.tv_usec = 100 * 1000;
        ret = lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(opt));
        if (ret != 0)
        {
            ret = lwip_close(sock);
            EnetAppUtils_print("ERR: set sockopt failed\r\n");
//            continue;
        }
 
//        for ( uint32_t i = 0; i < APP_SEND_DATA_NUM_ITERATIONS; i++)          /* Send data to Host */
        {
            memset(&snd_buf, 0, sizeof(snd_buf));
            buf_len = snprintf(snd_buf, sizeof(snd_buf), "Hello over UDP %" PRId32 "\r\n", udp_count_1);
 
            CacheP_wbInv(snd_buf, sizeof(snd_buf), CacheP_TYPE_ALLD);
            ret = lwip_sendto(sock, snd_buf, buf_len, 0,pAddr, sizeof(*pAddr));
 
            if (ret != buf_len)
            {
                ret = lwip_close(sock);
                EnetAppUtils_print("ERR: socket write failed\r\n");
//                continue;
            }
            EnetAppUtils_print("Message to host: %s\r\n", snd_buf);
 
            ret = lwip_recvfrom(sock, gRxDataBuff, APP_SOCKET_MAX_RX_DATA_LEN, 0, pAddr, &len);
            gRxDataBuff[ret] = '\0';
            EnetAppUtils_print("Message from host: %s\r\n", gRxDataBuff);
        }
 
        /* close */
        ret = lwip_close(sock);
        EnetAppUtils_print("Closed Socket connection\r\n");
//        ClockP_sleep(2);
    }
    return;
}
 
void AppSocket_showMenu_1(void)
{
    ip_addr_t ipAddr;
    int32_t addr_ok = 0;
    EnetAppUtils_print(" UDP socket Menu: \r\n");
 
    do
    {
//        EnetAppUtils_print(" Enter server IPv4 address:(example: 192.168.101.100)\r\n");
//        DebugP_scanf("%s", gHostServerIp4);
        addr_ok = ip4addr_aton(gHostServerIp4, ip_2_ip4(&ipAddr));
        TaskP_yield();
    } while (addr_ok != 1);
}
  
void clientTest()
{
    AppSocket_simpleClient(&gHostInfo[1].socketAddr);
    udp_count_1++;
 
}
 
void AppSocket_startClient_1(void)
{
    uint16_t port[6] = {1};
 
    AppSocket_showMenu_1();
 
 
    for(uint16_t i=0;i <1;i++)
    {
        Appsocket_fillHostSocketInfo(&gHostInfo[i+1],port[i]);
    }
 
    clientTest();
 
 
}

 

Server code:

#define UDP_CONN_PORT_IGMP    1
#define MCAST_IPV4_ADDR       LWIP_MAKEU32(192, 168, 2, 250)  // 192.168.1.204
#define UDP_RECV_BUFSIZE      100
#define UTILS_ALIGN(x,align)  ((((x) + ((align) - 1))/(align)) * (align))


#define BASE_PORT 1
#define NUM_PORTS 1

struct App_hostInfo_t
{
    struct sockaddr_in socketAddr;
};

static void AppUdp_ServerTask(void *arg);
//static struct App_hostInfo_t gHostInfo[NUM_PORTS];
static struct App_hostInfo_t gHostInfo;
void serverTest();
int sock;
//static struct App_hostInfo_t gHostInfo;
static void Appsocket_fillSocketInfo(struct App_hostInfo_t* pHostInfo)
{
    err_t err;
    memset(&pHostInfo->socketAddr, 0, sizeof(pHostInfo->socketAddr));

    struct sockaddr_in*  pAddr = &pHostInfo->socketAddr;
    pAddr->sin_family = AF_INET;
    pAddr->sin_port = htons(1);
    pAddr->sin_addr.s_addr = htonl(INADDR_ANY);

    DebugP_log("UDP server: Port %d\r\r\n",UDP_CONN_PORT_IGMP);
    if ((sock = lwip_socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        DebugP_log("UDP server: Error creating Socket\r\r\n");
    }

    err = lwip_bind(sock, pAddr, sizeof(*pAddr));
    if (err != ERR_OK)
    {
        DebugP_log("UDP server: Error on bind: %d\r\r\n", err);
        lwip_close(sock);
    }

    return;
}

static void AppUdp_ServerTask(void *pArg)
{

    struct sockaddr* pAddr = pArg;
    const ip4_addr_t multi_addr = { .addr = PP_HTONL(MCAST_IPV4_ADDR) };
    char recv_buf[UTILS_ALIGN(UDP_RECV_BUFSIZE, 32)] __attribute__ ((aligned(32)));
    struct sockaddr_in from = { 0 };
    socklen_t fromlen = sizeof(from);
    int32_t count;

    while (true)
    {
        count = lwip_recvfrom(sock, recv_buf, UDP_RECV_BUFSIZE, 0, (struct sockaddr*) &from, &fromlen);
        if (count <= 0)
            continue;

        DebugP_log("Packet recieved \r\n");

        count = (count >= UDP_RECV_BUFSIZE) ? UDP_RECV_BUFSIZE - 1 : count;
        recv_buf[count] = '\0';
        CacheP_wbInv(recv_buf, sizeof(recv_buf), CacheP_TYPE_ALLD);
        DebugP_log("Client: %s\r\n", recv_buf);
        if (sendto(sock, recv_buf, count, 0, (struct sockaddr*) &from, fromlen) < 0)
        {
            DebugP_log("Error in write\r\n\r");
        }
        else
        {
            DebugP_log("Echo pkt completed\r\n\r");
        }
        ClockP_sleep(1);
    }
}

void serverTest()
{
    AppUdp_ServerTask(&gHostInfo);
}
void AppUdp_startServer()
{
    Appsocket_fillSocketInfo(&gHostInfo);

    while(1)
    {
        serverTest();
    }

}

in this server code i am just observing the data of client message so 

client side : image

server side : image 

issue is only once i am receive the data from client side after that what is the error why the data is not receiving from the client can you justifyl.