/* * Written by Darryl Dixon 08/05/2002, based upon modified code * from fpont 12/99; pont.net; udpClient.c * Program is designed to take two command line arguments and use * them to send a UDP packed to the appropriate socket to talk to a * POM server (Patrol Operations Manager) * pomsender.cpp */ #include #include #include #include #include #include #include #include #include #include #define REMOTE_SERVER_PORT 9561 /* POM listens on this UDP Port for a correctly formatted string... */ int main(int argc, char *argv[]) { char headerString[7] = "EXMGC."; char completeString[512] = ""; int sd, rc; struct sockaddr_in cliAddr, remoteServAddr; struct hostent *host; char *headerPtr; char *completePtr; headerPtr = headerString; completePtr = completeString; /* First check command line arguments */ if (argc != 3) { printf("usage : %s \"\"\n", argv[0]); exit(1); } /* Now we get the POM server IP address */ host = gethostbyname(argv[1]); if (host == NULL) { printf("%s: unknown host '%s'... Perhaps try using an IP address instead.\n", argv[0], argv[1]); exit(1); } /* Now we need to create the data packet to send: */ remoteServAddr.sin_family = host->h_addrtype; memcpy((char *) &remoteServAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); /* 9561 UDP for POM */ /* Next create the socket we are going to send from: */ sd = socket(AF_INET,SOCK_DGRAM,0); /* SOCK_DGRAM as opposed to SOCK_STREAM... */ if (sd < 0) { printf("%s: cannot open socket.\n",argv[0]); exit(1); } /* Bind a port to send from (we'll take whatever we can get :) */ cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = htonl(INADDR_ANY); cliAddr.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); if (rc < 0) { printf("%s: cannot bind a port.\n", argv[0]); exit(1); } /* Let's create the string that we're going to send now: */ strncat(completePtr, headerPtr, 7); strncat(completePtr, argv[2], 505); /* Now finally we can send the data: */ rc = sendto(sd, completeString, strlen(completeString)+1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr)); if (rc < 0) { printf("%s: cannot send data %s\n",argv[0],argv[2]); close(sd); exit(1); } return EXIT_SUCCESS; }