Code:
#include "C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\stdafx.h"
#include <stdio.h> // For perror() call
#include <stdlib.h> // For exit() call
#include <windows.h>
#include <winsock.h> // Include Winsock Library
#pragma comment (lib, "wsock32.lib") // Visual C++ -> Include library
#ifndef HAVE_ZERO
#define bzero(ptr,n) memset(ptr,0,n) // to initalize client and server structures
#define SA struct sockaddr
#define SB struct sockaddr
#endif
#define CLIIP "205.207.115.132" //Testing - ip address of DESTINATION
//#define CLIIP "205.207.115.255" //Testing - broadcast ip address
int main(int argc, char**argv) {
int sockfd, socksd; // Handle to sockets
struct servent *sp=NULL; // Pointer to servent structure
struct sockaddr_in cliaddr, servaddr;
char mesg[1000]; // Message buffer for incoming message
char tmesg[1000]="MISC XXX XXX HELLO WORLD"; // Outgoing test message
typedef int socklen_t;
socklen_t len, clen;
long n, m=0;
// ***** socket option vars ***
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
int iOptLen = sizeof(int);
// ****************************
WSADATA wsaData; // Windows socket
//INITIALIZE WINSOCK
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
perror("connection Error.");
exit(EXIT_FAILURE);
}
//OPEN SOCKET - gather server info and bind for receive.
if ((sp=getservbyname("monitorudp",NULL))==NULL) {
printf("error %d", WSAGetLastError());
}
sockfd=socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd<0) {
perror("cannot open socket ");
exit(EXIT_FAILURE);
}
bzero(&servaddr.sin_addr, sizeof(servaddr)); //initialize server structure
servaddr.sin_family=AF_INET;
servaddr.sin_port=sp->s_port;
if(bind (sockfd, (SA*) &servaddr, sizeof(servaddr))<0) perror("ERROR on binding");
//OPEN SOCKET and fill in client structure for send.
socksd=socket(AF_INET, SOCK_DGRAM, 0);
if (socksd<0) {
perror("cannot open socket ");
exit(EXIT_FAILURE);
}
bzero(&cliaddr.sin_addr, sizeof(cliaddr)); //initialize client structure
cliaddr.sin_family=AF_INET;
//cliaddr.sin_port=htons(10001);
cliaddr.sin_port=0;
cliaddr.sin_addr.s_addr = inet_addr(CLIIP);
if (setsockopt(socksd, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != SOCKET_ERROR) {
printf("Set SO_BROADCAST: ON\n");
}
//LOOP ->RECEIVE DATAGRAMS FROM SERVER, AND OUTPUT TO CLIENTS (will be changed to BROADCAST)
len=sizeof(servaddr);
clen=sizeof(cliaddr);
for (;;){
n=recvfrom(sockfd, mesg, 1000, 0, (SA*) &servaddr, &len);
if (n<0) {
perror("receive failed");
exit(EXIT_FAILURE);
}
else if (n>0) {
mesg[n]='\0'; // NULL terminate
printf("received %s\n",mesg);
printf("received %d\n", n);
}
m=sendto(socksd, tmesg, n, 0,(SB*) &cliaddr, sizeof(clen));
if (m<0) {
perror("send failed");
//exit(EXIT_FAILURE); commented out for testing
}
else if (m>0) {
tmesg[m]='\0'; // add NULL terminate
printf("sending %s\n", tmesg);
printf("sent %d\n", m);
}
}
//CLOSE SOCKET
if(closesocket(sockfd)<0) perror("ERROR on closing");
//CLEAN UP WINSOCK
if(WSACleanup()==NULL) {
printf("error %d", WSAGetLastError());
}
//END PROGRAM
return 0;
}