#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <errno.h> /* LOCAL_UDP_PORT and FOREIGN_UDP_PORT must be defined at compile time */ #define FOREIGN_ADDR "192.168.135.2" unsigned char test_message[ 1024 ] = { "abcdefghijklmnopqrstuvwxyz" }; unsigned char rcv_message[ 1024 ]; #define ATTEMPT( x ) if ( (x) < 0 ) \ { \ perror( "\nERROR: apinull.c " ); \ printf( "Died at line %d, errno= %d.\n", \ __LINE__, errno ); \ exit( 1 ); \ } int main() { int iTotSize; int iSockOpt; int iLoopCount; struct sockaddr_in l_addr; struct sockaddr_in f_addr; int sock = socket( AF_INET, SOCK_DGRAM, 0); bzero( (char *)&l_addr, sizeof( struct sockaddr_in ) ); bzero( (char *)&f_addr, sizeof( struct sockaddr_in ) ); l_addr.sin_family = f_addr.sin_family = AF_INET; l_addr.sin_port = htons( LOCAL_UDP_PORT ); f_addr.sin_port = htons( FOREIGN_UDP_PORT ); f_addr.sin_addr.s_addr = inet_addr( FOREIGN_ADDR ); ATTEMPT( bind( sock, (struct sockaddr *)&l_addr, sizeof( l_addr ) ) ) ATTEMPT( connect( sock, (struct sockaddr *)&f_addr, sizeof( f_addr ) ) ) iTotSize = strlen( test_message ); printf( "apinull : sending %d byte string: %s, to port 0x%x.\n", iTotSize, test_message, FOREIGN_UDP_PORT ); ATTEMPT( write( sock, &test_message, iTotSize ) ); bzero( &rcv_message, sizeof( rcv_message ) ); ATTEMPT( iTotSize = read( sock, &rcv_message, sizeof( rcv_message ) ) ); printf( "apinull : received %d byte string: %s.\n", iTotSize, rcv_message ); close(sock); exit(0); return(0); }