Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Net/UDP/UDP_SocketServer.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__UDP_SocketServer_CPP__
00002 #define __J2K__UDP_SocketServer_CPP__
00003 
00004 #include <j2k/Net/UDP/UDP_SocketServer.hpp>
00005 
00006 // Port 2000 by default
00007 UDP_SocketServer::UDP_SocketServer( short TFTP_Port ) : UDP_Socket()
00008 {
00009    memset( &server, 0, sizeof( struct sockaddr_in ) );
00010    memset( &client, 0, sizeof( struct sockaddr_in ) );
00011 
00012    server.sin_family      = PF_INET;
00013    server.sin_addr.s_addr = htonl( INADDR_ANY );
00014    server.sin_port        = htons( TFTP_Port );
00015 
00016    memset( buffer, 0, UDP_BUFFER_SIZE );
00017    gethostname( buffer, 80 );
00018 
00019    mySocket = socket(PF_INET, SOCK_DGRAM, 0);
00020 
00021    if ( mySocket < 0 )
00022    {
00023       printf( "Error: listen socket creation failed\n" );
00024    }
00025   
00026    if( bind( mySocket, (struct sockaddr*)&server, sizeof(server) ) != 0 )
00027    {
00028       printf("Error: bind on listen socket failed\n");
00029       closesocket( mySocket );
00030       exit( 3 );
00031    }
00032 
00033    getHostData( buffer );
00034    memset( buffer, 0, UDP_BUFFER_SIZE );
00035 
00036    // Start thread
00037 
00038    PrintInfo();
00039    int ThreadID;
00040    pthread_t thread;
00041    ThreadID = pthread_create( &thread, 0,
00042                               UDP_SocketServer::ListenThread,
00043                               (void*)this
00044                             );
00045    if ( ThreadID )
00046    {
00047       printf("Error creating listen thread\n");
00048       exit( 3 );
00049    }
00050 }
00051 
00052 void* UDP_SocketServer::ListenThread( void* data )
00053 {
00054     UDP_SocketServer* me = (UDP_SocketServer*)data;
00055 
00056    int clientAddrLength = sizeof( me->client );
00057   
00058    while( 1 )  // Infinite loop
00059    {  
00060       memset( me->buffer, 0, UDP_BUFFER_SIZE );
00061       int result = recvfrom( me->mySocket,
00062                              me->buffer,
00063                              UDP_BUFFER_SIZE, 0,
00064                              (struct sockaddr *) &me->client, 
00065                              (socklen_t       *) &clientAddrLength 
00066                            );
00067 
00068       size_t pduLength = result;
00069       if ( pduLength > 60 ) pduLength = 60;
00070 
00071       // We retrieve the client name from recvfrom()
00072 
00073       if ( result >= 0 )
00074       {
00075           printf( "Received %d bytes\n", result);
00076           printf( "Message received from host %s port %d\n",
00077                    inet_ntoa( me->client.sin_addr ),
00078                    ntohs(     me->client.sin_port )
00079           );
00080 
00081       option.showChar( "Received:", (char*)me->buffer, pduLength );
00082  
00083           fflush( stdout );
00084 
00085          // Call TFTP_PacketParser
00086          UDP_CALLBACK();
00087          fflush( stdout );      
00088 
00089       } else if ( result < -1 ) {         
00090          printf("[%d]", result );
00091          printf( "Error while receiving message: recvfrom() < 0 " );
00092          fflush( stdout );      
00093       }      
00094    }  
00095 }
00096 
00097 // Reply to the message
00098 inline BOOL UDP_SocketServer::send( void* msg, size_t msgLength  )
00099 {
00100 #ifdef DEBUG
00101     option.showChar( "SendPDU=", (char*)msg, msgLength );
00102 #endif
00103     
00104   return  UDP_Socket::send( msg, msgLength, &client );
00105 }
00106 
00107 inline UDP_SocketServer::~UDP_SocketServer() { }
00108 
00109 #endif
00110 
00111 
00112 
00113 
00114 

Generated on Sun Oct 14 18:46:35 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001