Will Perone

Here is how to resolve your own ip and get a free port to use for a client in socket code. Since SDL Net doesn't have this functionality, I had to code it myself.
unsigned int ip;   // the ip in ipv4 format
unsigned short port; // the port to use

char hostname[64];
gethostname(hostname, 64);
hostent *hostent= gethostbyname(hostname);
ip= *(unsigned int *)hostent->h_addr_list[0];  // this gets the local ip

sockaddr_in sa;
int  namelen;
getsockname(s, (sockaddr *)&sa, &namelen);
port= sa.sin_port; // this is the port we will use to connect with
You should never use a static port for client connections; you should always attempt to find a free port with a method like this. There is a bit of a catch, if you are on a LAN, this will resolve your ip on the LAN; not the router's external ip. The ip is stored in the 4 octet ipv4 format so to print it out you have to go through some small hoops:
printf("Your ip is %i.%i.%i.%i", ip&0xFF, (ip>>8)&0xFF, (ip>>16)&0xFF, ip>>24);	
No Comments yet, be the first!
<- for private contact