Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 4038 times)
0 Members and 1 Guest are viewing this topic.
Offline JakeBerry

Junior Newbie

« Posted 2011-05-10 16: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 Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #1 - Posted 2011-05-10 17: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:

* 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 13: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 18: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.
Pages: [1]
  ignore  |  Print  

EgonOlsen (47 views)
2018-06-10 19:43:48

EgonOlsen (29 views)
2018-06-10 19:43:44

EgonOlsen (49 views)
2018-06-10 19:43:20

DesertCoockie (214 views)
2018-05-13 18:23:11

nelsongames (130 views)
2018-04-24 18:15:36

nelsongames (129 views)
2018-04-24 18:14:32

ivj94 (870 views)
2018-03-24 14:47:39

ivj94 (131 views)
2018-03-24 14:46:31

ivj94 (782 views)
2018-03-24 14:43:53

Solater (146 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!