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
/* * 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); }
/* * 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); }