00001 #include "UnixSocketSender.hpp"
00002
00003 UnixSocketSender::UnixSocketSender( char* SendMsg_Host,
00004 short SendMsg_Port )
00005 {
00006 ListenSocket = INVALID_SOCKET;
00007
00008 memset( &dest, 0, sizeof( struct sockaddr_in ) );
00009
00010 getHostData( SendMsg_Host );
00011
00012 dest.sin_family = PF_INET;
00013 dest.sin_addr = *(in_addr*)( hostdata->h_addr_list[0] );
00014 dest.sin_port = htons( SendMsg_Port );
00015 }
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 UnixSocketSender::~UnixSocketSender() { }
00031
00032 inline void UnixSocketSender::setSocket( SOCKET fdSocketNo ) {
00033 ListenSocket = fdSocketNo;
00034 }
00035
00036 bool UnixSocketSender::SendMsg( char* Msg, size_t msgLen )
00037 {
00038 signed int Sent;
00039
00040 printf( "Message being sent to host %s port %i\n",
00041 inet_ntoa( dest.sin_addr ),
00042 ntohs( dest.sin_port )
00043 );
00044
00045 Sent = sendto( ListenSocket, Msg, msgLen, 0,
00046 (struct sockaddr *)&dest,
00047 sizeof( struct sockaddr_in )
00048 );
00049
00050 if ( Sent != msgLen )
00051 {
00052 printf("Error sending UDP packet from listen socket\n");
00053 fflush(0);
00054 return false;
00055 }
00056
00057 return true;
00058 }
00059
00060 void UnixSocketSender::getHostData( char* SendMsg_Host )
00061 {
00062 if ( atoi( SendMsg_Host) )
00063 {
00064 ULONG ip = inet_addr( SendMsg_Host );
00065 hostdata = gethostbyaddr( (char*)&ip, sizeof(ip), PF_INET );
00066
00067 } else {
00068
00069 hostdata = gethostbyname( SendMsg_Host );
00070 }
00071
00072 if ( hostdata == NULL )
00073 {
00074 printf( "Error getting host address\n" );
00075 fflush( 0 );
00076 exit( 1 );
00077 }
00078 }