Send random numbers as udp

Hi!
I am kind of a newbie programmer that just got stuck on a problem and hope for a bit of help.
I am making a program that should generate random values for x, y, z in a scope specified from the user and then send those as udp packets.

The program generates the numbers but it is with the sending there seems to be a problem.
I can get a response from the udp server that tells a package have arrived but the content seem to be to small and not in the expected format.

I add the code for you to look at and i know it is uggly :)

data i get from server is:
got packet from 127.0.0.1
packet is 12 bytes long
packet contains "-1074814364"

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;

}

Topic archived. No new replies allowed.