Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to do simple local network games?  (Read 2577 times)
0 Members and 1 Guest are viewing this topic.
Offline JakeBerry

Junior Newbie





« Posted 2011-05-10 18:58:00 »

Hi everyone, I'm just now getting into learning network stuff such as sockets etc., and my goal has been to make fun simple little games like pong and lightcycles that play over a network. I don't know much about networking but I assumed that there would be a way to have one application open on say, a computer at school, that listens for any other instances of it on the network and when the other opens up they connect and play... I've already managed to make pong that will connect if I open up two copies of the game on one computer and it works, but not on two computers side by side. After looking at it I realized it must be because the sockets are using "localhost", which comes straight out of the examples that got me started.

Is it possible to have simple games that just hop onto a local network, like at school, or does it have to be more complex?

If it is possible, how do I find the host name to use?

Thanks for your patience guys, I'm new and there doesn't seem to be a lot of help online for this as far as I've searched around  Undecided
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2011-05-10 19:45:37 »

You can send a UDP broadcast. This sends out a UDP message to all computers on a subnet. Typically, it goes like this:

1) The server is listening on a UDP port.
2) The client sends out UDP broadcast and then waits X seconds for a response.
3) The server responds to the UDP message with another UDP message.
4) If the client gets a UDP message within the timeout, it assumes it came from a server. The message received has the IP of the server, and the client can try to connect via TCP, etc.

A couple notes:

* The broadcast will be blocked by most network hardware. It is really only useful for discovering servers running on the LAN.

* UDP messages can get lost, so the client might want to send a few broadcasts.

* You can see the UDP broadcast code for the client side here:
http://code.google.com/p/kryonet/source/browse/trunk/kryonet/src/com/esotericsoftware/kryonet/Client.java#408

* You can simplify your life by requiring the user to input an IP for the client to connect to. Find the IP address of the server (eg by going to it and typing "ipconfig") and use that for the client.

* For running a server visible to the internet, you can use a service like DynDNS to give your server a name instead of needing to remember the IP. You'll need to configure any routers that your server is behind. Setup the router so that communication on the ports your server uses goes to your server machine.

Offline JakeBerry

Junior Newbie





« Reply #2 - Posted 2011-05-11 15:52:47 »

Thanks so much, this is really helpful!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jeffpk

Senior Newbie


Medals: 2



« Reply #3 - Posted 2011-05-21 20:06:58 »

Whatever you do, dont try to do all your game traffic on multi-cast.  If you use multi-cast for all your game traffic then two sessions of the same game on the same subnet will conflict.  

I have a chapter all about this in "Practical Java game Programming."  It covers both discovery and communication and includes example code.

http://www.amazon.com/Practical-Programming-Charles-River-Development/dp/1584503262/ref=sr_1_1?ie=UTF8&s=books&qid=1306001203&sr=8-1
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (25 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!