[SDL] local IP address (SDL_net)

Christopher Larsen christopherleelarsen at gmail.com
Sat Jun 9 18:47:05 PDT 2007


 Yes you can...

Ulrich Eckhardt <doomster <at> knuut.de> writes:

> 
> On Wednesday 23 May 2007 18:12:41 Alvin wrote:
> > Does SDL_net provide a way of getting the local host's IP addresss? Not the
> > loop-back address, but its routable address?
> 
> No. BTW: you can have more than one such address!
> 
> > I've searched through SDL_net's docs and demos, but I can't seem to find
> > this. I tried the "dnr" demo for SDL_net, but it says that it cannot
> > resolve the hostname?
> 
> Don't know about that demo.
> 
> > I can do it using the Linux system calls, but I prefer to use SDL_net so
> > the solution will be automagically portable.
> 
> Well, using the Berkeley sockets API (which is what you mean with system 
> calls), you are already pretty portable. Other than that, a patch to SDL_net 
> would surely also be welcome.
> 
> Uli
	// Check what my real IP is first
	BUG("Check what my real IP is: ");
	const char* tempstr = SDLNet_ResolveIP(&myIPaddress);		// Get 
the network name of this computer
	if (tempstr) BUG("tempstr is valid");
	if (strlen(tempstr) > 79) ERROR("ERR: Filename too long!");
	strcpy(myNetworkName, tempstr);
	fprintf(stdout,"myNetworkName is: ");
	fprintf(stdout, " %s \n", myNetworkName); fflush(stdout);

	SDLNet_ResolveHost(&myIPaddress, myNetworkName, FOUNDATION_PORT);
	// Fill up an ip with my network name
	iterator = (Uint8*) &myIPaddress.host;			// This simple 
elegant code reads the host Uint32 member into a readable IP
	fprintf(stdout, "%d.", *iterator++);			//
	fprintf(stdout, "%d.", *iterator++); 			//
	fprintf(stdout, "%d.", *iterator++); 			//
	fprintf(stdout, "%d ", *iterator);				//
	fprintf(stdout,"myIPaddress.port is: ");
	fprintf(stdout, " %d \n", myIPaddress.port); 
	fflush(stdout);








More information about the SDL mailing list