SendMsg.cpp

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
#include "MainDefs.h"

#include <Windows.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>



int SendMsg(char* MessageToSend){
    
     struct sockaddr_in addr;
     int fd, cnt;
     struct ip_mreq mreq;
     char Message[255];
     char UserName[255];
     char MsgToSend[255];
     unsigned int dwUserNameSz=255;

     /* create what looks like an ordinary UDP socket */
     if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  return(1);
     }

     /* set up destination address */
     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=inet_addr(MCAST_CHANNEL);
     addr.sin_port=htons(PORT);
     

     GetUserName(UserName,&dwUserNameSz);

     /* now just sendto() our destination! */
     memset(Message,0,255);
     memset(MsgToSend,0,255);
    sprintf(MsgToSend,"%s: %s",UserName,MessageToSend);
    if (sendto(fd,MsgToSend,strlen(MsgToSend),0,(struct sockaddr *) &addr, sizeof(addr)) < 0) {
        perror("sendto");
	return(1);
    }
    return(0);
    
}