BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
a62ca13
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
BlinkenRecv.c
BlinkenLib v.0.4.1 (2005-12-05)
Christian Heimke
commited
a62ca13
at 2011-07-15 09:03:25
BlinkenRecv.c
Blame
History
Raw
/* BlinkenLib * version 0.4.1 date 2005-12-05 * Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info> * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html * a blinkenarea.org project * powered by eventphone.de */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "BlinkenLib.h" int main( int argCnt, char * * args ) { int i, udpSocket, bound, val, timeout; char txt[64]; unsigned short port; struct sockaddr_in addr; stBlinkenMovie * pMovie; //print info printf( "BlinkenLib - BlinkenRecv\n" "version 0.4.1 date 2005-12-05\n" "Copyright 2004-2005 Stefan Schuermans <1stein@schuermans.info>\n" "Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html\n" "a blinkenarea.org project\n" "powered by eventphone.de\n\n" ); //print syntax if( argCnt <= 1 ) { printf( "syntax: %s <parameter> [...]\n\n" "parameters:\n" " -l [<ip>:]<port>\n" " local address (defaults to 0.0.0.0:2323)\n" " must occur before -r and -o, may only occur once\n" " -r <ip>[:<port>]\n" " remote addess (defaults to every remote address)\n" " -t <milliseconds>\n" " set timeout (to detect movie end, defaults to 5000)\n" " -o <file>\n" " receive movie and write it to file (*.blm, *.bmm, *.bml, *.bbm)\n\n", args[0] ); return 0; } //create udp socket udpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if( udpSocket == -1 ) { printf( "cannot create UDP socket\n" ); return -1; } bound = 0; //process parameters timeout = 5000; for( i = 1; i < argCnt; i++ ) { //local address if( strcmp( args[i], "-l" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = inet_addr( txt ); if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set local address to \"%s\"\n", args[i] ); else bound = 1; } else if( sscanf( args[i], "%hu", &port ) == 1 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = htonl( INADDR_ANY ); if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set local address to \"%s\"\n", args[i] ); else bound = 1; } else printf( "invalid local address \"%s\"\n", args[i] ); } else printf( "missing local address for \"-l\"\n" ); } //remote address else if( strcmp( args[i], "-r" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = inet_addr( txt ); if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set remote address to \"%s\"\n", args[i] ); } else if( sscanf( args[i], "%32[0-9.]", txt ) == 1 ) { addr.sin_family = AF_INET; addr.sin_port = htons( 23230 ); addr.sin_addr.s_addr = inet_addr( txt ); if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set remote address to \"%s\"\n", args[i] ); } else printf( "invalid remote address \"%s\"\n", args[i] ); } else printf( "missing remote address for \"-r\"\n" ); } //timeout else if( strcmp( args[i], "-t" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%hu", &val ) == 1 ) timeout = val; else printf( "invalid timeout value \"%s\"\n", args[i] ); } else printf( "missing timeout value for \"-t\"\n" ); } //receive movie and write it to file else if( strcmp( args[i], "-o" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( ! bound ) //try to bind if not bound { printf( "no local address to receive movie for file \"%s\" to,\n" " using default local address \"0.0.0.0:2323\"\n", args[i] ); addr.sin_family = AF_INET; addr.sin_port = htons( 2323 ); addr.sin_addr.s_addr = htonl( INADDR_ANY ); if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set local address to \"0.0.0.0:2323\"\n" ); else bound = 1; } if( bound ) { printf( "receiving movie for file \"%s\"...\n", args[i] ); pMovie = BlinkenMovieReceive( udpSocket, timeout, NULL ); if( pMovie == NULL ) printf( "could not receive movie for file \"%s\"\n", args[i] ); else { if( BlinkenMovieSave( pMovie, args[i] ) < 0 ) printf( "could not write movie \"%s\"\n", args[i] ); else printf( "movie \"%s\" written\n", args[i] ); BlinkenMovieFree( pMovie ); } } else printf( "no local address set to receive movie for file \"%s\" on\n", args[i] ); } else printf( "missing output filename for \"-o\"\n" ); } //unknown parameter else printf( "unknown parameter \"%s\", call without parameters to get help\n", args[i] ); } //for( i ... //close socket close( udpSocket ); return 0; }