/** C Source **************************************************************** * * NAME udp-ex.c * * DESCRIPTION * UDP example program. * This program will open a UDP socket and display any UDP packets received * on STDIO and send a UDP packet when a key is pressed. * * To send a UDP message, open the STDIO window of the Dynamic C IDE and * press a key. * * TARGET * Rabbit 2000 processor. * * TOOLS * Dynamic C V7.06 Premiere * * Editor tab stop = 4. Tabs converted to spaces. * * REVISION LOG * yyyy mmm dd Who Ver. Description * 2002 Jan 10 ssk 1.00 Created. * ***************************************************************************** * The software contained in this file is released to the public domain * January 2002 by Scot Kornak, Calgary, Alberta, Canada. * * This is unsupported software and it is provided without warranty. * It may be freely used and modified for private or commercial applications. * If you modify it, I encourage you to publicly release the modified version. *****************************************************************************/ /** Include Files ***********************************************************/ /* Define dummy IP address to avoid DCRTCP.LIB warnings */ #define MY_IP_ADDRESS "172.16.10.xx" #define MY_NETMASK "255.255.0.0" #define MAX_UDP_SOCKET_BUFFERS (1) /* Number of buffers used for UDP */ #use "dcrtcp.lib" /** Local Constants and types ***********************************************/ #define UDP_PORT 1234 /* REMOTE_UDP_MASK * "255.255.255.255" = accept all packets. * "0" = accept any remote address. The connection will be limited to the * first remote host to connect. */ #define REMOTE_UDP_MASK "255.255.255.255" #define DEFAULT_REMOTE_UDP "172.16.10.46" /* Send address used by default if no packets received yet. */ /* More IP configuration parameters are in the tcp_config() calls below. */ #define UDP_BUFFER_LEN (40) /** Functions ***************************************************************/ void main( void ); /** Function **************************************************************** * * NAME * main - Open a UDP socket and wait for packets. * * DESCRIPTION * This routine opens a UDP socket and display any UDP packets received * on STDIO. * * If a key is pressed on STDIO, a UDP packet is sent to the address * of the last received packet (or the default address if none have been * received yet). * * INPUTS None. * * OUTPUTS None, does not exit. * * HISTORY * yyyy mmm dd Who Description * 2002 Jan 10 ssk Created. * ****************************************************************************/ void main( void ) { udp_Socket udpSocket; int pass; unsigned long remoteIP; unsigned int remotePort; unsigned char *ptr; unsigned char udpBuffer[ UDP_BUFFER_LEN ]; unsigned int sendCount; memset( udpBuffer, 0, UDP_BUFFER_LEN ); /* Clear buffer */ sendCount = 0; sock_init(); tcp_config( MY_IP, "172.16.10.30" ); tcp_config( NETMASK, "255.255.0.0" ); tcp_config( NAMESERVER, "172.16.0.1" ); tcp_config( GATEWAY, "139.142.15.2" ); remoteIP = resolve( DEFAULT_REMOTE_UDP ); remotePort = UDP_PORT; pass = udp_open( &udpSocket, UDP_PORT, resolve( REMOTE_UDP_MASK ), 0, NULL ); if( !pass ) { printf( "UDP OPEN ERROR\n" ); exit( 1 ); } while( 1 ) { tcp_tick( NULL ); /* Process all sockets */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Check for UDP messages. Get remote address of received packet. */ pass = udp_recvfrom( &udpSocket, udpBuffer, UDP_BUFFER_LEN, &remoteIP, &remotePort ); if( pass >= 0 ) { printf( "Received-> %s\n", udpBuffer ); ptr = (unsigned char*)&remoteIP; printf( "Remote IP = %d.%d.%d.%d\n", ptr[ 3 ], ptr[ 2 ], ptr[ 1 ], ptr[ 0 ] ); printf( "Remote port = %d\n", remotePort ); } if( pass < -1 ) { printf( "UDP RX ERROR\n" ); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Send UDP message if key pressed */ if( kbhit() ) { getchar(); sendCount++; sprintf( udpBuffer, "Send #%u", sendCount ); printf( "%s\n", udpBuffer ); /* Send UDP packet */ pass = udp_sendto( &udpSocket, udpBuffer, strlen( udpBuffer ) + 1, remoteIP, remotePort ); if( pass < 0 ) { printf( "UDP SEND ERROR\n" ); } } } } /* EOF */