Click here to Skip to main content
15,913,090 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Simple cpp question Pin
Toni7829-Apr-03 20:13
Toni7829-Apr-03 20:13 
GeneralRe: Simple cpp question Pin
David Crow30-Apr-03 3:15
David Crow30-Apr-03 3:15 
GeneralSerialization problem Pin
fermar8429-Apr-03 16:50
fermar8429-Apr-03 16:50 
GeneralRe: Serialization problem Pin
Neville Franks29-Apr-03 21:09
Neville Franks29-Apr-03 21:09 
QuestionHow to Broadcast? Pin
Aidman29-Apr-03 13:20
Aidman29-Apr-03 13:20 
AnswerRe: How to Broadcast? Pin
JohnnyG29-Apr-03 15:13
JohnnyG29-Apr-03 15:13 
GeneralRe: How to Broadcast? Pin
Aidman29-Apr-03 22:41
Aidman29-Apr-03 22:41 
GeneralRe: How to Broadcast? Pin
JohnnyG30-Apr-03 3:11
JohnnyG30-Apr-03 3:11 
Yes, you do. Here is a sample program I wrote to perform broadcast. It works on both Linux, Posix (for our embedded system), and Windows. For Windows, you need to #define WINMACHINE:

// TEST PROGRAM TO BROADCAST ON UDP SOCKET.  Should work on both POSIX and Windows
// to compile/run in Windows, define WINMACHINE .e.g #define WINMACHIN
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>      /* strlen() */


// platform specific includes   
#ifdef WINMACHINE               // for Windows

#include <windows.h>
#include <winsock.h>
#include <wincon.h>
#include <conio.h>

#else                           // for unix/POSIX

#include <machine/mpc823.h>
#include <sys/types.h>
#include <sys/socket.h>  /* sockaddr_in */
#include <sys/errno.h>      // for global errno
#include <netinet/in.h>  /* AF_INET, etc. */
#include <pthread.h>     /* pthread_create() */
#include <netinet/udp.h>

//#include <curses.h>

#endif

#define TXOUTBUFSZ 10000                                // Output buffer size
#define RADAR_PORT 44068                                 // IP/UDP Port number for trans/rcv

// platform specific data declarations and/or definitions
#ifdef WINMACHINE               // for Windows

WSADATA             wsaData;
SOCKET              sock;
SOCKADDR_IN         dst_addr, src_addr;
int                 dst_addr_len = sizeof(dst_addr);

#else                           // for unix/POSIX

int                 sock;
struct sockaddr_in  dst_addr, src_addr;
#endif

// non-platform specific declarations
int                 rc;


int main()
{
    char sampleMsg[132];
    int optval = 1;
    int optlen = sizeof(int);

	dst_addr.sin_family = AF_INET;
	dst_addr.sin_port = htons(RADAR_PORT);
	dst_addr.sin_addr.s_addr = INADDR_BROADCAST;  // inet_addr("255.255.255.255"); //htonl(INADDR_ANY);

    strcpy(sampleMsg, "This is a test sample msg from the box. ");

#ifdef WINMACHINE               // for Windows
	// Start WinSock in version 2.0 of the protocol
	rc = WSAStartup (MAKEWORD (1, 0), &wsaData);  // was MAKEWORD(1,1)
	if(rc != 0)
	{
		printf("Error starting WinSock\n");
		goto error;
	}

	// Create a UDP socket for receiving datagrams
	sock = socket(AF_INET,SOCK_DGRAM, 0);
	if(sock == INVALID_SOCKET)
	{
		printf("Error creating receive socket\n");
		goto error;
	}

	// Set up the socket to do broadcast messages...this may not be needed
	rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, optlen);
	if(rc == SOCKET_ERROR)
	{
		printf("Error setting up receive buffer\n");
		goto error;
	}

    while(!kbhit())
    {
        printf("Sending data: %s  Any key to quit.\n", sampleMsg);
        rc = sendto(sock, sampleMsg, strlen(sampleMsg) + 1, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr));
        if(rc == SOCKET_ERROR)
        {
            printf("Couldn't send data.\n");
            goto error;
        }
        Sleep(1000);
    }

    closesocket(sock);
    goto finish;
#else                           // for unix/POSIX

    sysInit(); 		    /* initialize pKernel   */
    startNetwork();     /* start network stack  */
 if (setuid(0)== -1)
 printf("setuid not eq root");
    /* network initialization code. Make a UDP socket                                               */
    if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0) 
    {
        printf("Network socket initialization failed.\n");
        fflush(stdout);
        goto error;
    }   

	// Set up the socket to do broadcast messages...this may not be needed
	rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, optlen);
	if(rc)
	{
		printf("Error setting up receive buffer\n");
		goto error;
	}

src_addr.sin_family = AF_INET;

src_addr.sin_port = htons(RADAR_PORT);
src_addr.sin_addr.s_addr = htonl(INADDR_ANY);  // inet_addr("255.255.255.255")

if (bind(sock,(struct sockaddr*) &src_addr,sizeof (src_addr)) == -1)
printf("bind failed \n");


	while(1)
	{
           printf("Sending data: %s  Any key to quit.\n", sampleMsg);
	   if(sendto(sock, sampleMsg, strlen(sampleMsg) + 1, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr)) < 0)
	   {
	     printf("Error: could not send data. \n");
	     fflush(stdout);
	     goto error;
	   }
	   sleep(1);
	}

 
	goto finish;

#endif
finish:
    printf("Done.\n");
return 0;                       // success

error: 

#ifdef WINMACHINE
        printf("\nsock Error in main():  %ld Hit any key\n", WSAGetLastError());
#else
        printf("\nsock Error in main():  %ld Hit any key\n", errno);
#endif

	    getchar();
	    return 1;
}

GeneralRe: How to Broadcast? Pin
Aidman30-Apr-03 9:10
Aidman30-Apr-03 9:10 
GeneralRe: How to Broadcast? Pin
Aidman30-Apr-03 9:45
Aidman30-Apr-03 9:45 
GeneralRe: How to Broadcast? Pin
JohnnyG30-Apr-03 11:11
JohnnyG30-Apr-03 11:11 
GeneralHow to make a nonblocking receive socket? Pin
Aidman30-Apr-03 11:15
Aidman30-Apr-03 11:15 
GeneralRe: How to Broadcast? Pin
JohnnyG30-Apr-03 11:21
JohnnyG30-Apr-03 11:21 
QuestionChanging the Image Size? Pin
Pesto29-Apr-03 13:09
Pesto29-Apr-03 13:09 
AnswerRe: Changing the Image Size? Pin
Chris Losinger29-Apr-03 13:48
professionalChris Losinger29-Apr-03 13:48 
GeneralRe: Changing the Image Size? Pin
Pesto29-Apr-03 22:36
Pesto29-Apr-03 22:36 
AnswerRe: Changing the Image Size? Pin
RaajaOfSelf29-Apr-03 17:40
RaajaOfSelf29-Apr-03 17:40 
GeneralTemplate again Pin
grscot29-Apr-03 12:23
grscot29-Apr-03 12:23 
GeneralRe: Template again - Formatting Pin
Neville Franks29-Apr-03 12:39
Neville Franks29-Apr-03 12:39 
GeneralRe: Template again - Formatting Pin
peterchen29-Apr-03 20:54
peterchen29-Apr-03 20:54 
GeneralRe: Template again - Formatting Pin
Neville Franks29-Apr-03 21:00
Neville Franks29-Apr-03 21:00 
GeneralGetCurrentImage Pin
Emiliano29-Apr-03 11:50
Emiliano29-Apr-03 11:50 
GeneralWant to create a color palette like in the Control Panel Pin
netstv29-Apr-03 11:49
netstv29-Apr-03 11:49 
GeneralRe: Want to create a color palette like in the Control Panel Pin
John R. Shaw29-Apr-03 14:44
John R. Shaw29-Apr-03 14:44 
GeneralTemplate problem Pin
grscot29-Apr-03 10:49
grscot29-Apr-03 10:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.