Sign in to follow this  

SDL_Net IP Address Problem

This topic is 3865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using SDL_Net to program a multiplayer game and for the most part it has gone smoothly. But I want to be able to broadcast the server over UDP to make it easier for clients to connect. My problem is that when I try to get the ip address that the server resolves I just get 0.0.0.0 as my IP. This seems odd considering the fact that it successfully opens a TCP socket using the IP and can talk to clients. If anyone could help me debug this problem that would be wonderful. if(SDLNet_ResolveHost(&ip,NULL,port)==-1) { printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError()); exit(3); } // perform a byte endianess correction for the next printf ipaddr=SDL_SwapBE32(ip.host); // output the IP address nicely printf("IP Address : %d.%d.%d.%d\n", ipaddr>>24, (ipaddr>>16)&0xff, (ipaddr>>8)&0xff, ipaddr&0xff);

Share this post


Link to post
Share on other sites
I don't think you have posted enough code, or enough of an explanation.

SDLNet_ResolveHost with NULL as a "hostname" second parameter, fills in the ip address so you can bind and listen locally. This doesn't match what you seem to be saying about broadcasts.

Where is this code located, in reference to the broadcast recieve?


The way I see it, you have something listening on a specific UDP port. When a packet comes in at that port, you echo something back at them. The client can then pick up the ipaddress from the packet struct and use it to connect using TCP, assuming your game runs over TCP.

Share this post


Link to post
Share on other sites
Quote:
Original post by DDRBoxman
Ok so if I do a UDP broadcast the client could resolve the server IP and I wouldn't have to send my IP address to the client in a packet?


Don't send your IP to the client in the payload. All UDP datagrams already have the ipaddress in the datagram header, which is accessible as the "address" member of the UDPpacket struct;

You could send useful things like the servers name, current player / max player count, map / level (if appropriate) etc.

Then the client can choose which server based on something more informative than just an IP address [smile].

Share this post


Link to post
Share on other sites
Thanks for clearing that up.
If I wanted the server to display its IP address then how would I fix that because the way that all of the example programs do it returns the IP as 0.0.0.0

Share this post


Link to post
Share on other sites
Quote:
Original post by DDRBoxman
Thanks for clearing that up.
If I wanted the server to display its IP address then how would I fix that because the way that all of the example programs do it returns the IP as 0.0.0.0


You mean how to tell what IP address the server has, from the server itself?

This is typically not required, nor always meaningful (with NAT etc). Also you can get machines with multiple interfaces with different addresses.

The best way would be to bounce a query off another machine. Other than that you will have to look at platform specific APIs I imagine. Or try parse the output of ipconfig/ifconfig, if you enjoy pain [grin]

What would you want to do with said IP address once you have it?

Share this post


Link to post
Share on other sites

This topic is 3865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this