// broadcasting.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h>

#define MAX_MSGLEN 80
#define MAX_ADDLEN 80
#define MAX_MSLOTNAME 80
#define usEndPoint 4444

typedef enum _MODE { CLIENT=0, SERVER } MODE;
typedef enum _PROTOCOL { UDP=0, IPX, MAILSLOT } PROTOCOL;

//BOOL __stdcall
//CtrlCHandler (
//    DWORD dwEvent
//    );
//
//void __stdcall
//Usage (
//    CHAR *pszProgramName
//    );
//
//CHAR * __stdcall
//IpxnetAddr (
//    CHAR *lpBuffer,
//	CHAR *lpsNetnum, 
//	CHAR *lpsNodenum 
//	);
//
//void __stdcall
//DoStartup ( void );
//
//void __stdcall
//DoCleanup ( void );
//
//
// Global Variables
//

// If Startup was successful, fStarted is used to keep track.
//BOOL fStarted = FALSE;

// Global socket descriptor
//SOCKET sock = INVALID_SOCKET;

//void __cdecl 
int main (
    INT argc,
    CHAR **argv
    )
{

 	printf("Hello to me \n");
    //
    // Install the CTRL+BREAK Handler
    //
    //if ( FALSE == SetConsoleCtrlHandler ( (PHANDLER_ROUTINE) CtrlCHandler, TRUE  ) ) 
    //{
	//	PrintError ( "main", "SetConsoleCtrlHandler", GetLastError ( ) );
    //}  
  
    //
    // Initialize the global socket descriptor.
    //
    sock = socket ( AF_INET, SOCK_DGRAM, 0 );

    //if ( INVALID_SOCKET ==  sock)
    //{
	//		PrintError ( "DoUdp", "socket", WSAGetLastError() );
    //}
    
//    return;



//
// DoUdpClient () function implements the broadcast routine for an UDP
// client. The function sets the SO_BROADCAST option with the global socket.
// Calling this API is important. After binding to a local port, it sends an 
// UDP boradcasts to the IP address INADDR_BROADCAST, with a particular
// port number.
//

//void __stdcall
//DoUdpClient (
//    USHORT usEndPoint
//    )
//{
  
  // IP address structures needed to fill the source and destination 
  // addresses.
  SOCKADDR_IN saUdpServ, saUdpCli;
  
  INT err;
  
  CHAR achMessage[MAX_MSGLEN];
  
  // Variable to set the broadcast option with setsockopt ().
  BOOL fBroadcast = TRUE;

    
    err = setsockopt ( sock,		/*Socket file descriptor					*/
		       SOL_SOCKET,			/*Socket Option Class						*/
		       SO_BROADCAST,		/*Change the socket attributes to BROADCAST */
		       (CHAR *) &fBroadcast,
		       sizeof ( BOOL )
		       );

    //if ( SOCKET_ERROR == err )
    //{
	//	PrintError ( "DoUdpClient", "setsockopt", WSAGetLastError ( )  );
    //}

    //
    // bind to a local socket and an interface.
    //
    saUdpCli.sin_family = AF_INET;
    saUdpCli.sin_addr.s_addr = htonl ( INADDR_ANY );
    saUdpCli.sin_port = htons ( 0 );

    err = bind ( sock, (SOCKADDR *) &saUdpCli, sizeof (SOCKADDR_IN) );

    //if ( SOCKET_ERROR == err )
    //{
	//	PrintError ( "DoUdpClient", "bind", WSAGetLastError ( ) );
    //}

    //
    // Fill an IP address structure, to send an IP broadcast. The 
    // packet will be broadcasted to the specified port.
    //
    saUdpServ.sin_family = AF_INET;
    saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
    saUdpServ.sin_port = htons ( usEndPoint );

    lstrcpy ( achMessage, "A Broadcast Datagram" );
	printf ("Sending a message to %s \n",inet_ntoa(saUdpServ.sin_addr));
    err = sendto ( sock,
		   achMessage,
		   lstrlen ( achMessage ),
		   0,
		   (SOCKADDR *) &saUdpServ,
		   sizeof ( SOCKADDR_IN )
		   );

    //if ( SOCKET_ERROR == err )
    //{
	//	PrintError ( "DoUdpClient", "sendto", WSAGetLastError ( ) );
	//}

    fprintf ( stdout, "%d bytes of data broadcasted\n", err );

    //
    // Call the cleanup routine.
    //
    //DoCleanup ( );

    return;
//}
}