Need some info to get started with network programming

Started by
22 comments, last by flodihn 13 years, 4 months ago
Hello

I'm programming in C++ and want to start network programming. I have found enet is really good, but then I saw that it doesn't support Mac and I really need it to be cross platform. So I'm looking for a network library (doesn't really matter if TCP or UDP), that is free and cross platform.

Or is it a very bad idea to do it on your own without a wrapper as it is in Beej's Guide to Network Programming? I won't be sending around huge packets and of course it has to be fast, but as there won't be many players this shouldn't be a problem.

And another question that must have been asked a 100 times but I couldn't find an answer: I can connect with enet to a server that runs on my on computer with 127.0.0.1 but how does this work with server on another computer (with firewall)?

Answer to FAQ: "To programmatically determine the CORRECT address to use, you have to bounce a query off a server that's available on the public internet, and look at what address it returns back. This will work correctly for situations where you're behind NAT (routers/firewalls)."

Could someone explain that to me....

Thanks for the info

Dominic
Advertisement
I use SDL_Net, its free, open source and cross platform.

Quote:
And another question that must have been asked a 100 times but I couldn't find an answer: I can connect with enet to a server that runs on my on computer with 127.0.0.1 but how does this work with server on another computer (with firewall)?

127.0.0.1 also referred to as loop back interface or ipadress is a special ipadress that always resolves to your own computer.

So if you have a two computers (computer1 and computer2), running a server on computer1 on 127.0.0.1 at port 2000 while trying to connect from computer2 to 127.0.0.0 at port 2000 will result that computer2 only tries to connect to itself.

So if you want to run a server on a network, you must listen on an ipadress within the local network adress range.
If you are in windows, opening a command prompt and typing "ipconfig /all" will you lots of information about your current ipadress.

Quote:
Answer to FAQ: "To programmatically determine the CORRECT address to use, you have to bounce a query off a server that's available on the public internet, and look at what address it returns back. This will work correctly for situations where you're behind NAT (routers/firewalls)."

I guess this could refer to DNS (Domain Name System), for example if your program connects to "www.gamedev.net", the underlying DNS resolver (handled by the OS) will send a query the a DNS server to ask what ipadress is associated with the name "www.gamedev.net".

The name servers are often provided by your ISP (in most cases you will get your ipadress and name server information from your ISP DHCP server)
But there is nothing stoping you from using custom ones, such as http://www.opendns.com/.
I use winsock. It's not that hard. :)
The OP asked for an open source / cross platform library:
Quote:
I have found enet is really good, but then I saw that it doesn't support Mac and I really need it to be cross platform. So I'm looking for a network library (doesn't really matter if TCP or UDP), that is free and cross platform.

I doubt winsock is any of those.
Thanks flodihn

SDL_Net sounds good. But is it a good idea to use it without using SDL for graphics, sound etc?

And I know that 127.0.0.1 can't be used to connect to other computer but when I show the ipconfig my address is shown like 192.168.x.y and that isn't the right IP either. And when I use the IP that i get from http://www.ip-adress.com/ it doesn't work either. So how can I get the correct IP when I am behind NAT?

winsock is of course good and easy, but as I said I need it to be cross platform.

Or maybe someone knows: CAN ENET SOMEHOW WORK ON MAC or is that not possible at all??
Quote:Original post by domok
Or maybe someone knows: CAN ENET SOMEHOW WORK ON MAC or is that not possible at all??
I don't see why it wouldn't. Why don't you give it a try?
Quote:Original post by domok
I have found enet is really good, but then I saw that it doesn't support Mac and I really need it to be cross platform.
Wherever did you get that idea? Enet had always supported Mac.

It treats Mac as a 'unix-like operating system' in the manual, which is how Macs are treated by most cross-platform software.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by domok
Thanks flodihn

SDL_Net sounds good. But is it a good idea to use it without using SDL for graphics, sound etc?

Yeah you can use SDL_Net without SDL for graphics, for example I use Ogre3d for graphics, OIS input for keyboard/mouse input, SDL_Mixer (for music), SDL_Net for networking and SDL for Joystick input.

Quote:Original post by domok
And I know that 127.0.0.1 can't be used to connect to other computer but when I show the ipconfig my address is shown like 192.168.x.y and that isn't the right IP either. And when I use the IP that i get from http://www.ip-adress.com/ it doesn't work either. So how can I get the correct IP when I am behind NAT?

Are the computer you try to connect on the same local area network (beyond same NAT/router).

If they are you should be able to use 192.168.x.x, make sure any firewalls on the computers are not blocking you.
If you run your server on one of the computers on 192.168.1.10:2000 you should be able from the other computer start a command prompt and type "telnet 192.168.1.10 2000", if telnet just trying to connect forever a firewall on one of the computers is blocking you.

If you are running a server and want computers outside your NAT to connect, you start the server at 192.168.1.10:2000 for example, then you must tell your router to forward any traffic on port 2000 (or any arbitrary valid port number) to 192.168.1.10:2000.
Since most ISP changes your ipadress once and while, I recommend a dynamic dns server, for example dyndns.org.
Then I'll propably stick with enet. I've just seen unix and windows on the enet page and so I thought it doesn't support mac...

When I connect to another computer on the same local area network it works with 192.168.x.y but I want to connect to any computer. You've written how to do that, but I don't really understand it. How can I tell the router to forward any traffic on port 2000? And I won't have a central server. The first player who starts the game will be the server and upload his ip somewhere on the internet, where the other players can download it. Is it possible this way? And how can it be done?

Thanks for all the answers
Quote:Original post by domok
Then I'll propably stick with enet. I've just seen unix and windows on the enet page and so I thought it doesn't support mac...
You do realize that OSX is based on UNIX?

This topic is closed to new replies.

Advertisement