dimanche 31 janvier 2016

How can I use send() to send a random port number using TCP?

I am trying to send a client a random port number that the client can then use to connect to the server again. I am really having trouble getting this to happen for some reason.

Also, a bonus question: Why does using rand() not give me random numbers? It will give me a random number the first time I run the program and then every time after that it gives a slightly increased version of the first "random" number (so the first time I run it might give 2000, and then the next time it gives 2004 and so on).

srand(time(0));
int random = rand() % 1025 + 64511; 
char rand_port[sizeof(random)];
*((int*)rand_port) = random;

send(Client, r_port, (int)strlen(r_port), 0);

Everything other than this one part of my code works fine and it all works fine if I replace "r_port" with a string like "6500". I really just can't figure out how to get the function to let me do this.

This should be the easiest part of this program yet I have spent hours trying to make this work and I have not been able to find anything helpful on the internet.




Aucun commentaire:

Enregistrer un commentaire