1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
using namespace std;
struct position
{
int x;
int y;
int z;
};
int random(int);
int random(int max)
{
return rand()%max;
}
int main(void)
{
srand(time(0));
int sockd, port, max, total, send, i;
string addr;
struct sockaddr_in their_addr;
struct hostent *host;
struct position p, *pd;
pd = &p;
i=0;
pd->x = 0;
pd->y = 0;
pd->z = 0;
cout << "Write the address:\n";
cin >> addr;
cout << "Portnumber:\n";
cin >> port;
cout << "Input Max:";
cin >> max;
cout << "Input total:";
cin >> total;
host = gethostbyname(addr.c_str());
if (host == NULL)
{
herror("gethostbyname");
exit(1);
}
if ((sockd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}
memset(&their_addr,0,sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(port);
their_addr.sin_addr = *((struct in_addr *)host->h_addr);
for (i=0; i<=total; i++)
{
pd->x = random(max);
pd->y = random(max);
pd->z = random(max);
send = sendto(sockd, (void *)pd, sizeof(*pd), 0,(struct sockaddr *)&their_addr,
sizeof(their_addr));
}
return 0;
}
|