Sign in to follow this  

Need some info to get started with network programming

This topic is 2566 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

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

Share this post


Link to post
Share on other sites
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/.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?
Two choices here. You can either have (a) the user manually setup port forwarding on their router, or (b) use a 3rd-party server to facilitate NAT punchthrough.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
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?


Not only that, OSX has the UNIX 03 certification and is allowed to use the UNIX brand. (Most BSD and Linux based systems don't)

Share this post


Link to post
Share on other sites
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...

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


Most routers have an web based interface, try to enter this address in your web browser:
http://192.168.0.1

Most routers have a login, after that you will probably have a menu with lots of options where you can configure your router, port forwarding should be there.

Share this post


Link to post
Share on other sites
I've now manually forwarded a port, or I hope I have... I had to write the global port range and the base host port. For the base host port I wrote the port that I want to forward. So what is the port range? And when I've done that, I still have no idea how to connect to another computer. Could anybody explain that to me?

Share this post


Link to post
Share on other sites
Quote:
Original post by domok
I've now manually forwarded a port, or I hope I have... I had to write the global port range and the base host port. For the base host port I wrote the port that I want to forward. So what is the port range? And when I've done that, I still have no idea how to connect to another computer. Could anybody explain that to me?


Ok, so I guess you want your server to accept clients on the Internet, for just connecting two computers on the same network all this port forwarding is not necessary.

If you just want to forward incoming connections on port 2000 to your server at 192.168.1.10 to port 2000 I guess the port range is 2000 - 2000.


Share this post


Link to post
Share on other sites
I don't want to connect to a server in the same local area network. I want to connect to a server on any computer. So that when you launch the server application on your computer, I can connect to the server from my computer. Obviously an IP like 192.168.1.30 is of no use to do that, I need a unic IP of your computer.

Say I wanted to ping you. To do this I would need your unique IP.
ping 80.99.26.31

sorry I'm really confused........

Share this post


Link to post
Share on other sites
The Forum FAQ answers exactly this question (and many more). Look at "how does my client find my server." In brief, the answer is either a shared central lobby server, or using DNS.
Each server on the Internet will have to use port forwardning if you use DNS or a simple lobby server; if you use an advanced lobby server with NAT introduction, you won't have to do port forwarding.

Share this post


Link to post
Share on other sites
I've already seen the FAQ but I still don't get it.

Isn't there a way that works regardless to firewall and nat and whatever to connect to a server. I don't want an extra hardware for the server, I want it to work on any computer.

For example if you make a peer-to-peer software, any computer has to connect to any other. That means that there must be one single way to accomplish that.
When using enet, you need the function:

enet_address_set_host( &Address, HostName );

somewhere in the code. So you have to know the HostName. Since it can't be something like 192.168.0.65 you have to find out the real one. I don't understand the methods that are written in the FAQ, but do they, in the end, provide this correct HostName?

I can't find any resources on this topic, do you know of any?

Share this post


Link to post
Share on other sites
Quote:

For example if you make a peer-to-peer software, any computer has to connect to any other. That means that there must be one single way to accomplish that.

Peer to peer is impossible when both peers are behind a NAT. Peer to peer "works" because some peers are publicly addressable, i.e. they are not behind a NAT.

For games, a central server can be used to initiate NAT punch-through for two peers who are behind NATs.

Quote:

Since it can't be something like 192.168.0.65 you have to find out the real one. I don't understand the methods that are written in the FAQ, but do they, in the end, provide this correct HostName?

The only way to know your public address is to ask a machine on the public internet. There is no other way. The most common way of doing this is to have a public server which you control.

The central server can then be used to store a list of all servers looking for players. This is how matchmaking services are built.

Quote:

I don't want an extra hardware for the server, I want it to work on any computer.

Then rent a server. Or use an existing computer with an always-on, non-NATed connection as the server. If you are going through a NAT, then you aren't really on the internet and you cannot do this.

Some people get private addresses from their ISP, they can never host such a service. Others get a public IP, but the router is assigned it and it creates a NAT for the devices connected to it. You can often configure these routers to forward inbound connections to a given computer, or to forward connections on a particular port to a given computer.

I have a little machine in my house. With the correct router settings, all connections on ports I want are forwarded to it. I use a free dynamic DNS service to give me a usable hostname even when my IP is occasionally changed.

You might be able to rig up the same, at least while you are testing your game. If you ever wanted to release it, you'll have to come up with a more stable and reliable solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by domok
Isn't there a way that works regardless to firewall and nat and whatever to connect to a server. I don't want an extra hardware for the server, I want it to work on any computer.


What the FAQ lists is "it."

You either have to have a server with a public IP, or you have to set up port forwarding (which is similar to a public IP, topology-wise), or you have to bounce off a server that does have a public IP or port forward set up.

Share this post


Link to post
Share on other sites
So how can I set up port forwarding? I think I've done that in the router settings (192.168.1.1). But what does that change? Can I connect now to this computer without using a public server? If yes how?

thanks

Share this post


Link to post
Share on other sites
Your router should have received a public IP from your ISP.
Its often dynamic (your ISP will change it from time to time).
It should be the same ipadress shown when you visiting http://www.ipadress.org/ (unless you are several routers).

Now from another computer you should be able to connect to that ipadress on the port you forwarded.

Share this post


Link to post
Share on other sites

This topic is 2566 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