Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Best approach for simple network game

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Best approach for simple network game

    I must admit that I have absolutely no idea about networking. So let's assume I have a simple snake clone for two players via network using synapse. What protocol would I use? TCP/IP, SSH or anything else? At least both computers/games should communicate to each other but how to achieve this?
    Best regards,
    Cybermonkey

  2. #2
    1. Use TCP for "realtime" communication, use HTTP for webpage-like stuff.
    Snake -> TCP.
    Chess -> HTTP.
    2. You should have server.
    2.1. In local network any PC has IP address, so if two ppl connected to same home router, either wired or Wi-Fi, they both have IP address in local network, so there should be no problem.
    2.2. To play over the Internet, either one of PCs should have static IP address, or you should provide one additional PC which would then act as an intermediate server in case u can't rely on players having static IP address. It is not even important if IP address is static or not. One of the PCs should be able to accept connections. When you view website, remote server accepts connection. When you play online game, remote server accepts connection. If there's no intermediate server, then one of player's PCs should act as server. To accept remote connections, configure your home router to accept connections & redirect them to ur PC, then look up your Internet IP address. Even then you can't be sure that you will be able to accept connections, the only way to be sure is to test it. Whether you will be able to accept incoming connections over the Internet or not depends on your Internet Service Provider.

    Most modern online games provide intermediate servers. Because in most cases you can't expect players to accept connections. Because to accept connections you should configure router, and then in most cases you also should pay your ISP additional fee, otherwise you won't be able to accept connections.

    U can still develop a game to be played using local area network, then there should be no problem.

    This all could change when IPv6 becomes more widely used. In IPv6 u have more addresses, so ISP's won't have to restrict IP addresses exposed to the Internet. Currently shortage of IPv4 addresses is the reason why every PC can't have static IP address. Same with mobile phones. However, what I said earlier about local area network is also true for Wi-Fi powered phones. Each phone in local area network has IP, so it can accept incoming connections....

    Lawl. I hope I managed to make some sense
    Last edited by dj_sharp; 20-05-2014 at 12:20 PM.

  3. #3
    Yes, thanks. I forgot to mention that this snake "demo" should only work in a local network. I heard something about using UDP, though ...
    Best regards,
    Cybermonkey

  4. #4
    I believe that there is no good reason for using UDP. If you try to use UDP for this, you will only complicate things for yourself. If you use UDP, you will have to implement connection control & data delivery checking yourself. TCP is the way to go. I spent some time trying to figure out how to transfer data over TCP using synapse.... It is not that hard, but lack of detailed documentation complicates things, I had lots of WTFs before I got it working in my application

  5. #5
    Would you mind posting a small (console) example? Do I need a web server installed on one of the computers? If not, how can I "catch" the socket connection? (Do I really need two computers to test, or are two programs on one computer enough?)
    Best regards,
    Cybermonkey

  6. #6
    Quote Originally Posted by dj_sharp View Post
    2.1. In local network any PC has IP address, so if two ppl connected to same home router, either wired or Wi-Fi, they both have IP address in local network, so there should be no problem.
    Every networked PC or device has an IP assigned to it.
    Any two networked PC's that are on the same network can directly comunicate between each other without any problems.
    But if you have two PC's on two different networks then direct comunication between them are not posible without those two networks either being bridged together or data is being routed from one network into another.
    Now due to security reasons most networks are protected by firewals which prevent all information to be routed into it. So in order to comunicate with a PC on such network you need to open (unblock) proper port in firewall for data to get through.

    Quote Originally Posted by dj_sharp View Post
    2.2. To play over the Internet, either one of PCs should have static IP address, or you should provide one additional PC which would then act as an intermediate server in case u can't rely on players having static IP address.
    Static IP is not required. You only need to exchange IP adresses between players. Static IP adress only comes usefull if you are hosting presistance multiplayer game so that in case of server conection loss you don't need to manually send your new IP adress to other players.
    And even this could be making your PC to become part of an domain (PC IP is provided by domain server).

    Quote Originally Posted by dj_sharp View Post
    Whether you will be able to accept incoming connections over the Internet or not depends on your Internet Service Provider.
    ISP providers don't block incoming connections. Incoming connections are only blocked by built-in firewall of your internet gateway. But in most cases you can configure firewall settings of your gateway and add proper exceptions to it.

    Quote Originally Posted by dj_sharp View Post
    Most modern online games provide intermediate servers. Because in most cases you can't expect players to accept connections.
    The main reason why modern online games rely on intermediate servers is to provide a game lobby through which players can easily find desired multiplayer sesion, join or create one. This eliminaes the need to manually share IP adresses between players.

    Quote Originally Posted by dj_sharp View Post
    Because to accept connections you should configure router, and then in most cases you also should pay your ISP additional fee, otherwise you won't be able to accept connections.
    Paying additional fee to able to accept incoming connections? What? I have never heard anything about it.
    The only aditional fee that ISP providers are known to charge is to get a static IP asigned to you. But nowadays more and more ISP providers assing static IP adress to their users by default (athleast here in Slovenia). I myself had to ask my ISP provider not to asign me a static IP adress.

    Quote Originally Posted by dj_sharp View Post
    However, what I said earlier about local area network is also true for Wi-Fi powered phones. Each phone in local area network has IP, so it can accept incoming connections....
    Only if those connections are comming from that local network.


    As for deciding between TCP or UDP:
    UDP protocol is intendet for general broadcasting of data to multiple computers. Becouse it doesen't have data delivery checkings system integrated it is mostly used for broadcasting non esential data (video or audio streaming).
    TCP on the other hand has built in packet delivery checking system which makes sure that packet was delivered. If not the packet is resended. With the difference of the UDP TPC protocol can deliver its packets to only one computer in time. So if you are sending data to let us say 1 computers you will have to send that data ten times (once for each computer).

    So since yin your game you will probably be sending data only between a few computers and you need to know wheter data arrived to another computer or not using TCP is the best choice.

    As for how you implement multiplayer functionality into your game I gues you would want to have one computer to act as server.
    This computer will also take care about game simulation. So it will be constantly sending information of game state to other clients.
    Clients on the other hand will be only sending input information (which keys were pressed/released).

  7. #7
    TCP is connection-based. You establish a connection between two hosts, and then send data over that. The protocol guarantees that data arrives whole (any packets lost are automatically re-sent) and in order, but obviously, this comes at a price - managing and re-establishing the connection, re-sending the packets et cetera takes some time, so TCP is generally slower than UDP.

    UDP is connectionless. Kinda like snail-mail, you just slap an address on a packet and hurl it into the vast void of the network in hope that whatever intermediate servers will be able to successfully forward that to desired destination. Downside is that, obviously, the packets may get lost or arrive out of order. But this also an advantage - if you send player position data over the net, you always want the latest data. If an old packet gets lost anyhow, there's no need to resend, because you probably got fresher data, anyway. And if old data arrives later than newer data - you can insert a timestamp into packet data, then compare timestamps and ignore all old packets coming in.

    On LAN, TCP should suffice, as the ping on local network will most likely be <1ms anyway, and you get all that guaranteed data integrity benefits. Heck, on low ping networks you can even do this the archaic way: send player X packet to player Y, and don't advance the game for player X until he gets the packet from player Y.

  8. #8
    Nice to see this thread, as we will need it in a future project.

    BTW, I read about synapse at FPC wiki and I see this is a general-purpose networking library. Does nomebody made any gaming using it, or there's any tutorial about how to use it in games?
    No signature provided yet.

  9. #9
    Quote Originally Posted by Ñuño Martínez View Post
    Does nomebody made any gaming using it, or there's any tutorial about how to use it in games?
    I only made some simple demo for nxPascal on how to create server and client even in single Lazarus or Delphi application. It shows how to send custom binary packets or text messages. I have noticed small bugs in the code so expect them... not had motivation to do engine coding lately, especially with community engine coming up. For the main part it works well though, and i have done a small game test with 2 flying ships over Internet too.
    https://code.google.com/p/nxpascal/s...e/#svn%2Ftrunk
    nxNetwork in source and demos.

  10. #10
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Nuno: I do use synapse in my projects that need network code (it even has serial support) which is really useful. If I recall, theres tutorials and documentation that is quite thorough in the source archive which I found sufficient to get going as I mentioned earlier. As for use in games - I have toyed with making a simple 2D MMORPG type engine and synapse worked wonders. Seeing as I did it just to see how hard it would be I got to the point where everything boots (server and client) and assets can be fetched/received from the server and data can be synced back and forth but beyond that it was pretty much just a question of content. What I can say is thats its a robust library that makes things very easy to code (said code was about a day and half of on/off lax development).

    If anyone has any queries, I'll gladly help out if its something I ran into though nothing really comes to mind off the bat
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •