#include <UDP_SocketServer.hpp>
Inheritance diagram for UDP_SocketServer::
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 |
|
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 } |
|
Definition at line 107 of file UDP_SocketServer.cpp. 00107 { } |
|
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 } |
|
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 } |
|
Definition at line 21 of file UDP_SocketServer.hpp. Referenced by ListenThread().
|