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

UDP_SocketServer Class Reference

#include <UDP_SocketServer.hpp>

Inheritance diagram for UDP_SocketServer::

UDP_Socket List of all members.

Public Methods

 UDP_SocketServer (short TFTP_Port=2000)
virtual ~UDP_SocketServer ()
virtual bool send (void *msg, size_t msgLen)

Static Private Methods

void * ListenThread (void *data)

Private Attributes

sockaddr_in client

Constructor & Destructor Documentation

UDP_SocketServer::UDP_SocketServer short    TFTP_Port = 2000
 

Definition at line 7 of file UDP_SocketServer.cpp.

00007                                                     : 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 }

__inline UDP_SocketServer::~UDP_SocketServer   [virtual]
 

Definition at line 107 of file UDP_SocketServer.cpp.

00107 { }


Member Function Documentation

void * UDP_SocketServer::ListenThread void *    data [static, private]
 

Definition at line 52 of file UDP_SocketServer.cpp.

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 }

__inline bool UDP_SocketServer::send void *    msg,
size_t    msgLength
[virtual]
 

Reimplemented from UDP_Socket.

Definition at line 98 of file UDP_SocketServer.cpp.

00099 {
00100 #ifdef DEBUG
00101     option.showChar( "SendPDU=", (char*)msg, msgLength );
00102 #endif
00103     
00104   return  UDP_Socket::send( msg, msgLength, &client );
00105 }


Member Data Documentation

struct sockaddr_in UDP_SocketServer::client [private]
 

Definition at line 21 of file UDP_SocketServer.hpp.

Referenced by ListenThread().


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:50:22 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001