Connecting without knowing servers IP

Started by
2 comments, last by oliii 11 years, 5 months ago
If I have 2 computers connected to the same router/switch, is it possible to write an application that can be running on both so that they can discover each other with only knowing a port number?

I havent really done much socket programming, but from what Ive done you usually have the server listening for connections on some port and the client is able to connect to the server by knowing the ip of the server and the appropiate port to send the request to.

How can I set it up so that clients can still connect to the server, .. but without knowing the servers IP?

Would it be a case of just repeatedley sending requests to a range of address you know the server could be on? or is there a better way?
Advertisement
You could just use a broadcast address to discover each other.
This is somewhat akin to matchmaking. The reason that large scale multiplayer games do not need to program the IP addresses of private hosts is because they use an intermediary. NAT punchthrough relies on this concept to permit the overcoming of NAT translation. The resultant idea is that you always have one intermediary who has a specific static address and port which remains open all of the time. To connect peers, the host may register themselves on this intermediary, and the client can access a host list from the intermediary to understand who to connect to.
Broadcast only works for computers sharing the same subnet (LAN games).

Ohterwise you need an introducer / matchmaker. These usually comes in the for of a registered domain (for which you can find the IP via a DNS lookup). It doesn't need to be, your matchmaking server could just be some machine on the net with a static IP and opened port.

Each player hosting a game would register themselves with that matchmaking server. Typically, a new session host would register himself with the matchmaking server, which will give the server the host's public IP. Then it's just a matter of keeping that session host IP and host session properties stored away for the duration of the game.

Then anyone wanting to connect to that session host would query the matchmaking server. Then the server returns the address of the session host in the query response.

The second benefit of using a matchmaking server is that it can act as an introducer for NAT punch-through, which is necessary for enabling players being routers to connect to each other.

Without a matchmaking server, you cannot know the IP address of the host. the host has to discover its own public internet address, and somehow send his IP to the client, via IM, email, or some form of game invite.

Something like this, although I'm not entirely sure about its reliability.
[source lang="cpp"]
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")
void show_ip_address()
{
HINTERNET hInternet, hFile;
DWORD rSize;
static char buffer[64];
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hFile = InternetOpenUrl(hInternet, "http://automation.whatismyip.com/n09230945.asp", NULL, 0, INTERNET_FLAG_RELOAD, 0);
InternetReadFile(hFile, &buffer, sizeof(buffer), &rSize);
buffer[rSize] = '\0';
InternetCloseHandle(hFile);
InternetCloseHandle(hInternet);
MessageBoxA(0, buffer, "", MB_OK);
}
[/source]

Everything is better with Metal.

This topic is closed to new replies.

Advertisement