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.

communication not happening between two concerto(f28m35) board using UDP protocol.

Hi,

I'm trying to communicate two concerto boards using UDP protocol. one board as a server and another one as a client.

configuration the board:

server: 

assigned ip :10.9.40.242

assigned port number: 6000

Client :

assigned ip :10.9.40.241

port number : 6000.

here I'm attached the client and server program. I'm using these program for communication.

Please guide me to solve this.

Thank you

Basavanagouda

4578.udpEcho_Client.c
/*
 * Copyright (c) 2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *    ======== udpEcho.c ========
 */


/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/Memory.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/utils/Load.h>

 /* NDK Header files */
#include <ti/ndk/inc/netmain.h>
#include <ti/ndk/inc/_stack.h>

/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>

/* Example/Board Header files */
#include "Board.h"
#include "UARTUtils.h"
#include "USBCDCD_LoggerIdle.h"

#define UDPPORT 1000
#define SERVER "127.0.0.1"
/*
 *  ======== udpHandler ========
 *  Echo back all UDP data received.
 *
 *  Since we are using UDP, the same socket is used for all incoming requests.
 */
Void udpHandler(UArg arg0, UArg arg1)
{
    SOCKET lSocket;
    struct sockaddr_in sLocalAddr;
    struct sockaddr_in client_addr;
    struct timeval to;
    int addrlen = sizeof(sLocalAddr);
    int status,fdStatus;
    HANDLE hBuffer;
  //  IPN LocalAddress = 0;

    int nbytes, nbytes1;
       char *buffer;
    char UDPbuffer[]="\nThis is  udp clnt test ...\n";



    fdOpenSession(TaskSelf());

    lSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (lSocket < 0) {
        System_printf("udpHandler: socket failed\n");
        Task_exit();
        return;
    }

    memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
    sLocalAddr.sin_family = AF_INET;
    sLocalAddr.sin_len = sizeof(sLocalAddr);
    sLocalAddr.sin_port = htons(6000);             //(arg0);
    sLocalAddr.sin_addr.s_addr =inet_addr("10.9.40.242"); // ;(SERVER);

    status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
     if (status < 0) {
         System_printf("udpHandler: bind failed: returned: %d, error: %d\n",
                 status, fdError());
         fdClose(lSocket);
         Task_exit();
         return;
     }

     /* Give user time to connect with udpSendReceive client app */
      to.tv_sec  = 5;
      to.tv_usec = 0;
      if (setsockopt( lSocket, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) ) < 0) {
          System_printf("udpHandler: setsockopt SO_SNDTIMEO failed\n");
          fdClose(lSocket);
          Task_exit();
          return;
      }

      if (setsockopt( lSocket, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) ) < 0) {
          System_printf("udpHandler: setsockopt SO_RCVTIMEO failed\n");
          fdClose(lSocket);
          Task_exit();
          return;
      }


    /* Give user time to connect with udpSendReceive client app
   Loop while we receive data */
    while (1)
    {
    	nbytes = sendto(lSocket, UDPbuffer, 25, 0,(struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));

    	if(nbytes < 0)
    	{
    		fdStatus = fdError();
    		if (fdStatus == EWOULDBLOCK)
    		{
				System_printf("udpHandler: Waiting for client to send UDP data\n");
				continue;
			}
    		else
    		{
    			printf("send FD error = %d",fdStatus);
    		}

    	}
    	else
    	{
    		printf("number byte client send = %d\n",nbytes);
    		nbytes1 = recvncfrom(lSocket, (void **)&buffer, MSG_DONTWAIT,(struct sockaddr *)&client_addr, &addrlen, &hBuffer);
    		if(nbytes1 < 0)
    		{
				fdStatus = fdError();
				if (fdStatus == EWOULDBLOCK)
				{
					printf("udpHandler: Waiting for client to send UDP data\n");
					continue;
				}
				else
				{
					printf("recieve FD error = %d",fdStatus);
					break;
				}
    		}
    		else
    		{
    			printf("number byte client recied = %d\n",nbytes1);
    			recvncfree( hBuffer );
    		}


    	}

    }

    System_printf("udpHandler stop, lSocket = 0x%x\n", lSocket);

    if (lSocket) {
        fdClose(lSocket);
    }

    fdCloseSession(TaskSelf());

    Task_exit();
}


void uart()
{

    add_device("UART", _MSA, UARTUtils_deviceopen,
               UARTUtils_deviceclose, UARTUtils_deviceread,
               UARTUtils_devicewrite, UARTUtils_devicelseek,
               UARTUtils_deviceunlink, UARTUtils_devicerename);

    /* Open UART0 for writing to stdout and set buffer */
    freopen("UART:0", "w", stdout);
    setvbuf(stdout, NULL, _IOLBF, 128);

    /* Open UART0 for reading from stdin and set buffer */
    freopen("UART:0", "r", stdin);
    setvbuf(stdin, NULL, _IOLBF, 128);

    /*
     *  Initialize UART port 0 used by SysCallback.  This and other SysCallback
     *  UART functions are implemented in UARTUtils.c. Calls to System_printf
     *  will go to UART0, the same as printf.
     */
    UARTUtils_systemInit(0);

    System_printf("Starting the UART Console example\n");

    /* Initialize the USB CDC device for logging transport */
    USBCDCD_init();
}

/*
 *  ======== main ========
 */
int main(void)
{

    /* Call board init functions */
      Board_initGeneral();
  	  Board_initGPIO();
      Board_initEMAC();
      Board_initUART();
      Board_initUSB(Board_USBDEVICE);
      uart();

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    System_printf("Starting the UDP Echo example\nSystem provider is set to "
                  "SysMin. Halt the target to view any SysMin contents in"
                  " ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* Start BIOS */
    BIOS_start();

    return (0);
}
1663.udpEcho_Server.c
/*
 * Copyright (c) 2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *    ======== udpEcho.c ========
 */


/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/Memory.h>
#include <xdc/runtime/System.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/utils/Load.h>

 /* NDK Header files */
#include <ti/ndk/inc/netmain.h>
#include <ti/ndk/inc/_stack.h>

/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>

/* Example/Board Header files */
#include "Board.h"
#include "UARTUtils.h"
#include "USBCDCD_LoggerIdle.h"

#define UDPPORT 1000

/*
 *  ======== udpHandler ========
 *  Echo back all UDP data received.
 *
 *  Since we are using UDP, the same socket is used for all incoming requests.
 */
Void udpHandler(UArg arg0, UArg arg1)
{
    SOCKET lSocket;
    struct sockaddr_in sLocalAddr;
    struct sockaddr_in client_addr;
    struct timeval to;
    //fd_set readfds;
    int addrlen = sizeof(client_addr);
    int status;
    HANDLE hBuffer;
  //  IPN LocalAddress = 0;

    int nbytes,Byte_send;
    char *buffer ;
    char UDPbuffer[]="\nThis is  udp svr test ...\n";

    fdOpenSession(TaskSelf());

    lSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (lSocket < 0) {
        System_printf("udpHandler: socket failed\n");
        Task_exit();
        return;
    }

    memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
    sLocalAddr.sin_family = AF_INET;
    sLocalAddr.sin_len = sizeof(sLocalAddr);
    sLocalAddr.sin_port = htons(6000);
    sLocalAddr.sin_addr.s_addr =inet_addr("10.9.40.242");

    status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
    if (status < 0) {
        System_printf("udpHandler: bind failed: returned: %d, error: %d\n",
                status, fdError());
        fdClose(lSocket);
        Task_exit();
        return;
    }

    /* Give user time to connect with udpSendReceive client app */
    to.tv_sec  = 5;
    to.tv_usec = 0;
    if (setsockopt( lSocket, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) ) < 0) {
        System_printf("udpHandler: setsockopt SO_SNDTIMEO failed\n");
        fdClose(lSocket);
        Task_exit();
        return;
    }

    if (setsockopt( lSocket, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) ) < 0) {
        System_printf("udpHandler: setsockopt SO_RCVTIMEO failed\n");
        fdClose(lSocket);
        Task_exit();
        return;
    }


    while (1)
	{
    	nbytes = recvncfrom(lSocket, (void **)&buffer, MSG_WAITALL,(struct sockaddr *)&client_addr, &addrlen, &hBuffer);

    	if (nbytes >= 0)
    	{
			/* Echo the data back */
    		printf("Sever recieved bytes  :  %d",nbytes);
    		Byte_send = sendto(lSocket, UDPbuffer, 20, 0,(struct sockaddr *)&client_addr, sizeof(client_addr));
			if(Byte_send > 0)
			{
				printf("Sever seding bytes  :  %d",Byte_send);
				recvncfree( hBuffer );
			}
		}
		else
		{
			status = fdError();
			if (status == EWOULDBLOCK)
			{
				printf("udpHandler: Waiting for client to send UDP data\n");
				continue;
			}
			else
			{
				printf("udpHandler: recvfrom failed: returned: %d, error: %d\n",nbytes, status);
				fdClose(lSocket);
				//flag = false;
				break;
			}
		}
	}
  System_printf("udpHandler stop, lSocket = 0x%x\n", lSocket);

    if (lSocket) {
        fdClose(lSocket);
    }

    fdCloseSession(TaskSelf());

    /*
     *  Since deleteTerminatedTasks is set in the cfg file,
     *  the Task will be deleted when the idle task runs.
     */
    Task_exit();
}



void uart()
{
    add_device("UART", _MSA, UARTUtils_deviceopen,
               UARTUtils_deviceclose, UARTUtils_deviceread,
               UARTUtils_devicewrite, UARTUtils_devicelseek,
               UARTUtils_deviceunlink, UARTUtils_devicerename);

    /* Open UART0 for writing to stdout and set buffer */
    freopen("UART:0", "w", stdout);
    setvbuf(stdout, NULL, _IOLBF, 128);

    /* Open UART0 for reading from stdin and set buffer */
    freopen("UART:0", "r", stdin);
    setvbuf(stdin, NULL, _IOLBF, 128);

    /*
     *  Initialize UART port 0 used by SysCallback.  This and other SysCallback
     *  UART functions are implemented in UARTUtils.c. Calls to System_printf
     *  will go to UART0, the same as printf.
     */
    UARTUtils_systemInit(0);

    System_printf("Starting the UART Console example\n");

    /* Initialize the USB CDC device for logging transport */
    USBCDCD_init();
}

/*
 *  ======== main ========
 */
int main(void)

{

    /* Call board init functions */
    Board_initGeneral();
	Board_initGPIO();
    Board_initEMAC();
    Board_initUART();
    Board_initUSB(Board_USBDEVICE);
    uart();

    /* Turn on user LED */
    GPIO_write(Board_LED0, Board_LED_ON);

    System_printf("Starting the UDP Echo example\nSystem provider is set to "
                  "SysMin. Halt the target to view any SysMin contents in"
                  " ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    /* Start BIOS */
    BIOS_start();

    return (0);
}

  • Basavanagouda,

    Are you able to use the stack and access the ethernet driver to write data ? Are you seeing any activity on a scope ?

    Thanks

    Noah

     

  • Hi,

    Thank you for your reply,

    Yes I'm able to use stack, client able to send data to server and server receiving it, but server not sending data back to client.. client IP is not updating in server sockaddr_in structure.

    I'm not understanding few points here

    1. At the sever side what I need to update in sockaddr_in  structure.(which IP and port number should be assigned for socket in  "bind") .

    2. At the client side what  I need to update in sockaddr_in  structure.(which IP address I need to for client socket).

    3.What will happen  if I use INADDR_ANY or 127.0.0.1.  (I used both and tried  but not able to communicate)

    Thank you

    Basavanagouda.

  • Basavanagouda,

    TI-RTOS provides an UDP echo server example. Have you tried that? Can you please provide more details like error code from server/client sockets?

    Regarding your questions:
    1. It should be the IP address of the server (or INADDR_ANY) and the port that the server is listening to.
    2. It should be the IP address of the server that the client is connecting to.
    3. 127.0.0.1 is a loopback IP. It cannot be connected to from an external client.

    Vikram
  • Hi Vikarm,
    Thank you for your reply,

    Problem got solved, I created client socket with client IP not with a server IP(assigned client IP in client sockaddr_in).
    but in your reply client socaddr_in must be assigned with a server IP, I tried this but server not able to reply back to client .
    I'm confused now whether my method is correct or wrong.


    Thank you


    Basavangouda.
  • Hi Basavanagouda,

    Glad that the problem was solved.

    Regarding my reply, I meant the address that is used when you call connect() in the client code should be of the server.

    See: pubs.opengroup.org/.../connect.html

    Vikram
  • Looking at your UDP client implementation, I see that you are calling sendto() (instead of connect()/send()). So in your case, the "dest_addr" parameter would contain the server's IP address.

    ssize_t sendto(int socket, const void *message, size_t length,
    int flags, const struct sockaddr *dest_addr,
    socklen_t dest_len);

    For more info, see: pubs.opengroup.org/.../sendto.html

    Hope this helps.

    Vikram
  • Hi,

    I tried your suggestion. I assigned  server IP address to client sockaddr_in structure(10.9.40.242). client sending data to server and server receives it.

    But server is not able to reply back to client. Because at recvncfrom(lSocket, (void **)&buffer, MSG_WAITALL,(struct sockaddr *)&client_addr, &addrlen, &hBuffer);

    client_addr is updating with client structure(it having client port number and server IP 10.9.40.242).

    I'm using same client_addr handle to send data back to client [ sendto(lSocket, UDPbuffer, 20, 0,(struct sockaddr *)&client_addr, sizeof(client_addr)) ; ]. So its updated with a server IP address not with a client. its not sending data to client.

    If I assign client address(10.9.40.241) in client sockaddr_in both way communication is happening. How its happening?

    Please check the code.

    Client.c    [Client Board IP 10.9.40.241]

        memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
        sLocalAddr.sin_family = AF_INET;
        sLocalAddr.sin_len = sizeof(sLocalAddr);
        sLocalAddr.sin_port = htons(6000);             //(arg0);
        sLocalAddr.sin_addr.s_addr =inet_addr("10.9.40.241");

        sendto(lSocket, UDPbuffer, 25, 0,(struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));

    Server.c   [server board IP 10.9.40.242]

        memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
        sLocalAddr.sin_family = AF_INET;
        sLocalAddr.sin_len = sizeof(sLocalAddr);
        sLocalAddr.sin_port = htons(6000);
        sLocalAddr.sin_addr.s_addr =inet_addr("10.9.40.242");

     status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
        if (status < 0) {
            System_printf("udpHandler: bind failed: returned: %d, error: %d\n",
                    status, fdError());
            fdClose(lSocket);
            Task_exit();
            return;
        }

    recvncfrom(lSocket, (void **)&buffer, MSG_WAITALL,(struct sockaddr *)&client_addr, &addrlen, &hBuffer);

    sendto(lSocket, UDPbuffer, 20, 0,(struct sockaddr *)&client_addr, sizeof(client_addr));

    Thank you

    Basavanagouda.

  • Basavanagouda,

    A network client or a server would need to know its peer address to be able to send data. I would recommend this useful guide that I refer for socket programming: http://beej.us/guide/bgnet. Please take a look at the guide.

    Thanks,
    Vikram
  • Hi,

    Thank you

    Regards

    Basavanagouda

  • Hi ,

    I have met the same problem as you , I use the board as the UDP client to send data to the server, and the server can receive the data,

    then I let the server reply data to the client , but the client can not receive the data.

    So I need your help ! Can I see your source code of the UDP client ?

    thanks a lot.
  • Hi,

    I use the UDP protocol of TI-RTOS to communicate with the server, and the board as the UDP client , the PC as the server, Now ,the client can send data to the server ,and the server can also receive the data sent by client, and the server can also send data back to the client ( the server software notice that the data sent by server is sent successfully!), but the UDP client recvfrom function can not return the dada!c

    Can you help me to solve the problem ?In other ways, can you show me the example code of the UDP client?
  • Hi

    Please refer above posts.

    I have created client socket with client IP and Server Port.

    I think you are  using Server IP address at client socket as legacy socket programming, give your client ip address instead of server ip.

    It will work. but I don't know why its happening like this.

    Thank you'

    Basavanagouda

  • Hi

    Please refer above posts.

    I have created client socket with client IP and Server Port.

    I think you are  using Server IP address at client socket as legacy socket programming, give your client ip address instead of server ip.

    It will work. but I don't know why its happening like this.

    Thank you'

    Basavanagouda