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

UnixSocketReceiver Class Reference

#include <UnixSocketReceiver.hpp>

List of all members.

Public Methods

 UnixSocketReceiver (short Listen_Port)
 ~UnixSocketReceiver ()
bool Listen ()
int getSocket ()

Static Private Methods

void * ListenThread (void *data)

Private Attributes

int ListenSocket
sockaddr_in srv
sockaddr_in client


Constructor & Destructor Documentation

UnixSocketReceiver::UnixSocketReceiver short    Listen_Port
 

Definition at line 3 of file UnixSocketReceiver.cpp.

00004 {
00005    ListenSocket = INVALID_SOCKET;
00006 
00007    memset( &srv,    0, sizeof( struct sockaddr_in ) );
00008    memset( &client, 0, sizeof( struct sockaddr_in ) );
00009 
00010    srv.sin_family      = PF_INET;
00011    srv.sin_addr.s_addr = htonl( INADDR_ANY );
00012    srv.sin_port        = htons( Listen_Port );
00013 }

UnixSocketReceiver::~UnixSocketReceiver  
 

Definition at line 28 of file UnixSocketReceiver.cpp.

00029 {
00030    if ( ListenSocket != INVALID_SOCKET ) 
00031    {
00032       closesocket(ListenSocket);
00033    }
00034 }


Member Function Documentation

bool UnixSocketReceiver::Listen  
 

Definition at line 73 of file UnixSocketReceiver.cpp.

Referenced by main().

00074 {
00075    ListenSocket = socket(PF_INET, SOCK_DGRAM, 0);
00076 
00077    if ( ListenSocket == INVALID_SOCKET ) 
00078    {
00079       printf( "Error: listen socket creation failed\n" );
00080       return false;
00081    }
00082   
00083    if( bind(ListenSocket, (struct sockaddr*)&srv, sizeof(srv) ) != 0 )
00084    {
00085       printf("Error: bind on listen socket failed\n");
00086       closesocket(ListenSocket);
00087       return false;
00088    }
00089  
00090    int ThreadID;
00091 
00092    pthread_t thread;
00093    ThreadID = pthread_create( &thread, 0,
00094                               UnixSocketReceiver::ListenThread,
00095                               (void*)this
00096                             );
00097    if ( ThreadID )
00098    {
00099       printf("Error creating listen thread\n");
00100       return false;
00101    } else {
00102       return true;
00103    }
00104 }

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

Definition at line 41 of file UnixSocketReceiver.cpp.

00042 {
00043    char buf[ BUFFER_SIZE ];
00044    UnixSocketReceiver* socketData = (UnixSocketReceiver*)data;
00045 
00046    int msglen = sizeof( socketData->client);
00047   
00048    while(1)  // Infinite loop
00049    {  
00050       int result = recvfrom( socketData->ListenSocket,
00051                              buf,
00052                              sizeof(buf) - 1, 0,
00053                              (struct sockaddr*)&socketData->client, 
00054                              (socklen_t *)&msglen 
00055                            );
00056 
00057       if ( result > 0 )
00058       {
00059 
00060          buf[ result ] = 0;
00061 
00062          printf("Message received from host %s port %i\n",
00063                 inet_ntoa( socketData->client.sin_addr ),
00064                 ntohs(     socketData->client.sin_port )
00065                );
00066       
00067          printf("%s",buf);
00068          fflush(0);
00069       }  
00070    }  
00071 }

int UnixSocketReceiver::getSocket   [inline]
 

Definition at line 36 of file UnixSocketReceiver.cpp.

Referenced by main().

00037 {
00038    return ListenSocket;
00039 }


Member Data Documentation

int UnixSocketReceiver::ListenSocket [private]
 

Definition at line 20 of file UnixSocketReceiver.hpp.

Referenced by ListenThread().

struct sockaddr_in UnixSocketReceiver::client [private]
 

Definition at line 23 of file UnixSocketReceiver.hpp.

Referenced by ListenThread().

struct sockaddr_in UnixSocketReceiver::srv [private]
 

Definition at line 22 of file UnixSocketReceiver.hpp.


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