Added a function.

This commit is contained in:
irungentoo 2013-06-27 17:19:09 -04:00
parent 87633f6631
commit 3986206de8
3 changed files with 21 additions and 4 deletions

View File

@ -40,6 +40,16 @@ uint64_t current_time()
} }
int random_int()
{
#ifdef WIN32
//TODO replace rand with a more random windows function
return rand();
#else
return random();
#endif
}
//our UDP socket, a global variable. //our UDP socket, a global variable.
static int sock; static int sock;
@ -87,7 +97,11 @@ int init_networking(IP ip ,uint16_t port)
{ {
return -1; return -1;
} }
#else
srandom((uint32_t)current_time());
#endif #endif
srand((uint32_t)current_time());
//initialize our socket //initialize our socket
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

View File

@ -79,6 +79,9 @@ typedef struct
//returns current time in milleseconds since the epoch. //returns current time in milleseconds since the epoch.
uint64_t current_time(); uint64_t current_time();
//return a random number
int random_int();
//Basic network functions: //Basic network functions:
//Function to send packet(data) of length length to ip_port //Function to send packet(data) of length length to ip_port

View File

@ -96,9 +96,6 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
srand(time(NULL));
int randdomnum = rand();
memcpy(self_client_id, &randdomnum, 4);
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
if (argc < 4) { if (argc < 4) {
@ -107,13 +104,16 @@ int main(int argc, char *argv[])
} }
addfriend(argv[3]); addfriend(argv[3]);
//initialize networking //initialize networking
//bind to ip 0.0.0.0:PORT //bind to ip 0.0.0.0:PORT
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT); init_networking(ip, PORT);
int randdomnum = random_int();
memcpy(self_client_id, &randdomnum, 4);
perror("Initialization"); perror("Initialization");
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));