winsock2 get server IP

Started by
6 comments, last by Antheus 12 years, 3 months ago
Short version: How can I get the IP of an external server?


I am learning winsock2 and set up a very very simple test client-server application (TCP). Basically there is one exe for the server and one exe for the client, each in a separate vc++ 2010 project. Here are the codes for server and client (with error checking and includes removed to shorten)

Server:

int main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);

SOCKET client = INVALID_SOCKET;
SOCKET listener = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in anews;
anews.sin_addr.S_un.S_addr = INADDR_ANY;
anews.sin_family = AF_INET;
anews.sin_port = htons(5001);

bind(listener, (sockaddr*)&anews, sizeof(sockaddr));
listen(listener, SOMAXCONN);
client = accept(listener, NULL, NULL);

send(client, "Hello, client", 13, 0);
std::cout << "sent message" << std::endl;

getchar();

closesocket(client);
closesocket(listener);
WSACleanup();
return 0;
}


Client:

int main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);

std::string serverIP;
std::cout << "Enter the IP of the server" << std::endl;
std::cin >> serverIP;

SOCKET client = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in anews;
anews.sin_port = htons(5001);
anews.sin_addr.S_un.S_addr = inet_addr(serverIP.c_str());
anews.sin_family = AF_INET;

while (connect(client, (sockaddr*)&anews, sizeof(sockaddr_in)) == SOCKET_ERROR)
{
std::cout << "Failed to connect to server, retrying..." << std::endl;
}

char buf[100] = {0};
recv(client, buf, 100, 0);
std::cout << buf << std::endl;
getchar();
getchar();

closesocket(client);
WSACleanup();
return 0;
}


So I start the client application, and type "127.0.0.1" for the server IP, to use the loopback interface. THen it displays 'failed to connect' repeatedly, until I start the server application, which then sends the message instantly. THat's all working. It also works if I type my public IP in the client which is something like "144.32.36.186".

However:

Most online games do not need you to manually enter the server IP in order to connect. Even games without dedicated servers, where one of the players is the server. I want to know how I can get my client to automatically find a server, where the server IP can change arbitrarily.
(Then I can try a UDP version :) )
Advertisement
Usually you have one static server, with a registered domain name, like www.something.com, that all the servers connect to and say that they want to be servers. Then the clients connect to the same static master-server, which sends back all the registered server IPs that it has received, so the clients can know who they can connect to.
Is there a way of setting up such a 'master server' for free? I'm sure there are plenty of free multiplayer games that do the same.

Is there a way of setting up such a 'master server' for free?


Not really. It requires you being owner of an actual IP address/block.

Instead, buy a domain ('mysomethingoranother.com'), then configure that to point at your physical server (the IP). All of this is done through domain registrars and is a fairly straight-forward practice. :Prices start at ~$2 / year.

Client then queries DNS server with domain name and receives the currently configured IP for that name.


For various flaky alternatives, there exists dynamic DNS. These come with tons of potential problems and are unlikely to be a viable long-term solution, They work similar to usual registrars, but instead of owning a top level domain (xyz.com), they provide you with a subdomain (xyz.provider.com). In turn, they can be free, can change frequently, but come with little to no guarantees. For example, if you forget to update the address or update it to frequently, they may remove it or it may not update properly for all clients. DynamicDNS is typically used to provide domains for dynamic IPs, such as provided by ISP.
OK, so I gather that my options are:

1. Use my own PC as the master-server
All game players that want to host a game use the game server application to connect to my PC, and say 'hello, we want to host a game, here is our IP'
All game players trying to join an existing game use the game client application to connect to my PC, get a list of server IPs, and connect to one of them to play
(Players that want to host a game and then play in that same game will run the game server alongside the game client. Of course, I could just have the same application have the ability to be host or client, as I expect most games do)

2. Same as above, but use a domain.

I don't fully understand how this DNS/DynDNS works. I want to be able to upload an application (the master-server), and have control over when it runs, and I can't justify buying/renting anything, because I am only starting to learn now!

OK, so I gather that my options are:

1. Use my own PC as the master-server
All game players that want to host a game use the game server application to connect to my PC, and say 'hello, we want to host a game, here is our IP'
All game players trying to join an existing game use the game client application to connect to my PC, get a list of server IPs, and connect to one of them to play
(Players that want to host a game and then play in that same game will run the game server alongside the game client. Of course, I could just have the same application have the ability to be host or client, as I expect most games do)

2. Same as above, but use a domain.

I don't fully understand how this DNS/DynDNS works. I want to be able to upload an application (the master-server), and have control over when it runs, and I can't justify buying/renting anything, because I am only starting to learn now!


You could also use a website as the master server (using for example cURL to send and retrieve information from it),

DNS is just the domain name system, it translates a domainname (something.something.com for example) to an ip address (such as 125.16.123.14)

If you don't want to pay anything then you have to host the master server yourself or implement it as a webservice (Which there are free hosts for, allthough they do tend to suck) and you have to either buy a domain or use a free one (such as dyn.com) to ensure that the clients can always find the server. (If you have a static ip from your isp you can get by for quite some time without a domain but i wouldn't recommend it since you might move, change isp, etc in the future)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
First, the FAQ actually talks about this, and has links.

Second:


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I want to be able to upload an application (the master-server), and have control over when it runs, and I can't justify buying/renting anything,

[/font][/quote]

Why is it not allowed to cost anything to learn? Learning is one of the best investments you can make with your money! Try to not spend money on consumption, instead.

There are some cloud service providers that allow you to run "for free" for some small amount of time or scale. Google App Engine, for example -- but then you have to write the app using their API. Or Amazon ECC, which gives you one free "micro" instance for a year when signing up.

enum Bool { True, False, FileNotFound };


Why is it not allowed to cost anything to learn?


Most common obstacle is lack of a credit card or equivalent that is required for such services rather than cost itself.

This topic is closed to new replies.

Advertisement