00001 #ifndef __J2K__UDP_Socket_CPP__
00002 #define __J2K__UDP_Socket_CPP__
00003
00004 #include <j2k/Net/UDP/UDP_Socket.hpp>
00005
00006 inline UDP_Socket::UDP_Socket()
00007 : mySocket( INVALID_SOCKET) { }
00008
00009 inline void UDP_Socket::setSocket( SOCKET socket )
00010 {
00011 mySocket = socket;
00012 }
00013
00014 inline SOCKET UDP_Socket::getSocket()
00015 {
00016 return mySocket;
00017 }
00018
00019 inline UDP_Socket::~UDP_Socket()
00020 {
00021 if ( mySocket != INVALID_SOCKET )
00022 {
00023 closesocket( mySocket );
00024 }
00025 }
00026
00027
00028 BOOL UDP_Socket::send( void* msg, size_t msgLength,
00029 struct sockaddr_in* dest )
00030 {
00031 signed int Sent;
00032
00033 printf( "Message being sent to host %s port %u size %u \n",
00034 inet_ntoa( dest->sin_addr ),
00035 ntohs( dest->sin_port ),
00036 msgLength
00037 );
00038
00039 size_t pduLength = msgLength;
00040 if ( pduLength > 60 ) pduLength = 60;
00041 option.showChar( "Send:", (char*)msg, pduLength );
00042 Sent = sendto( mySocket, msg, msgLength, 0,
00043 (struct sockaddr *)dest,
00044 sizeof( struct sockaddr_in )
00045 );
00046
00047 memset( msg, 0, msgLength );
00048 if ( Sent != msgLength )
00049 {
00050 printf( "Error sending UDP packet from listen socket\n" );
00051 fflush( stdout );
00052 return FALSE;
00053 }
00054
00055 return TRUE;
00056 }
00057
00058 void UDP_Socket::PrintIP( ULONG ipname )
00059 {
00060 ULONG ipn = ipname;
00061 UCHAR ip1, ip2, ip3, ip4;
00062
00063 ip4 = ipn % 256;
00064 ipn >>= 8;
00065
00066 ip3 = ipn % 256;
00067 ipn >>= 8;
00068
00069 ip2 = ipn % 256;
00070 ipn >>= 8;
00071
00072 ip1 = ipn % 256;
00073 ipn >>= 8;
00074
00075 printf( "\nIP =[ %u | %u.%u.%u.%u ]\n", \
00076 ipname, ip1, ip2, ip3, ip4 );
00077
00078 fflush( stdout );
00079 }
00080
00081 void UDP_Socket::getHostData( char* host )
00082 {
00083 if ( atoi( host) )
00084 {
00085 ULONG ip = inet_addr( host );
00086 hostdata = gethostbyaddr( (char*)&ip, sizeof(ip), PF_INET );
00087
00088 } else {
00089
00090 hostdata = gethostbyname( host );
00091 }
00092
00093 if ( hostdata == NULL )
00094 {
00095 printf( "Error getting host address\n" );
00096 fflush( stdout );
00097 exit( 1 );
00098 }
00099 }
00100
00101 void UDP_Socket::PrintInfo()
00102 {
00103 printf( "\n\nHostName: %s\n", hostdata->h_name );
00104 printf( "IP address: %s\n", inet_ntoa(*((struct in_addr*) hostdata->h_addr )) );
00105
00106 option.setHostName( hostdata->h_name );
00107
00108
00109
00110 fflush( stdout );
00111 }
00112
00113 #endif