/* * udpHello.c * * This program implements a UDP echo server, which echos back any * input it receives. * * Copyright (C) 2007 Texas Instruments Incorporated - http://www.ti.com/ * * * 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. * */ #include #include // // dtask_udp_hello() - UDP Echo Server Daemon Function // (SOCK_DGRAM, port 7) // // Returns "1" if socket 's' is still open, and "0" if its been closed // //int dtask_udp_hello( SOCKET s, UINT32 unused ) int dtask_udp_hello() { SOCKET s; struct sockaddr_in sin1; struct sockaddr_in sin2; int i,tmp,test; char *pBuf = "Haba dere!"; UINT32 buffersize = 11; struct timeval timeout; HANDLE hBuffer; char *IPAddr = "192.168.0.199"; Task_sleep(10); /* Allocate the file environment for this task */ fdOpenSession( TaskSelf() ); // Create test socket s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if( s == INVALID_SOCKET ) { printf("failed socket create (%d)\n",fdError()); goto leave; } // Bind socket bzero( &sin1, sizeof(struct sockaddr_in) ); if( bind( s, (PSA) &sin1, sizeof(sin1) ) < 0 ) { printf("failed bind (%d)\n",fdError()); goto leave; } // Prepare address for the SendTo bzero( &sin1, sizeof(struct sockaddr_in) ); sin1.sin_family = AF_INET; sin1.sin_len = sizeof( sin1 ); sin1.sin_addr.s_addr = inet_addr(IPAddr); //sin1.sin_port = htons(1001); sin1.sin_port = 5000; // Configure our timeout to be 5 seconds timeout.tv_sec = 5; timeout.tv_usec = 0; setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) ); setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) ); while(1) { if( (tmp = sendto( s, &pBuf, buffersize, 0,(PSA)&sin1, sizeof(sin1) ))< 0 ) { printf("send failed (%d)\n",fdError()); break; } else { printf("send successful :(%d)\n",tmp); } } leave: fdClose(s); return(0); }