lan games in java

Just wondering if anyone would know a good tutorial for lan networks in java, I understand sockets and the like, as well as the whole concept of doing say collision detection on one computer and sending the updated data to all the clients, but the only actual server and client java program I could find a tutorial for creates a server that crashes immediately after the client disconnects, I don’t understand the (coding) difference between the tutorial he showed and a lan network program, or even whether or not his was already lan to begin with. Any tutorials or help would be nice, ty

here is the code for that tutorial I found:
server:
http://pastebin.com/r9AdU2BY

client and dataPackage:
http://pastebin.com/ahgRRhtW

Just sent you a PM.
But, regarding not knowing whether the examples were ‘LAN’ or not:
I believe LAN is just connecting both computers to the host address ‘127.0.0.1’ while 1 or another computer hosts the server on that same address.
So a non LAN Server/Client pair would just be the server hosted off a different address than 127.0.0.1 (So it would be a real IP).

LAN games are on the same network segment. This means they will all have addresses of the form XXX.XXX.XXX.YYY where XXX is the same for all the clients and server. That means no problems getting past routers and you can use broadcast packets to transmit your client data to all other clients in one go (if you use UDP rather than TCP), if you are running peer-peer rather than client-server.

The nice thing about LAN games is that you can build in automatic discovery of clients, so user setup is a breeze. Also, because ping is low, less effort is required for lag compensation, so FPS games run really well.

Obviously LAN games can only be run on computers at the same location (limited by the maximum length of an ethernet cable run) so no good for internet play. On the other hand, since you are only playing with friends, there is no need to build in anti-cheat code (unless you have really sneaky friends)

The first thing to decide is client-server or peer-peer. Then decide TCP/IP versus UDP. For FPS LAN games I would suggest UDP broadcast peer-peer. For role playing games TCP/IP client-server.

Got to go…

Sockets work the same whether it’s LAN, IP, or localhost.

A bad example of what never to do: leaving catch blocks empty
Just print the stack trace for caught exceptions to find the reason of the crash.

I chose the wrong wording, the server program does not crash (i.e. close?), it freezes, i have to open up task manager to shut the thing off.

You could look up the code for a simple chatroom, and make sure the network functionality is robust enough before integrating it into your game.

After all, network communication is no different from a simple chat, you just process the messages differently (not to mention you’ll want to be able to send chat text too).

As for the mentioned LAN/WAN difference, if you have a TCP/IP capable client/server architecture, you have both options. The only relevant difference, as has been mentioned above, is scanning for servers, which on a LAN can be easily managed because you know the IP range of all devices on the same subnet.

For WAN (Internet) play, either players need to know their connection IP (your game could provide this information) and manually enter it to connect, or there needs to be a master server that players connect to in order to find open games (which could be set up easily on a website, but it might require upkeep).

Providing direct IP capabilities is important, as you might be on a LAN, but on different subnets, which could render your server search function useless.

If I may go on a tangent, this issue really irks me about gaming today. Despite what publishers claim, all games are perfectly LAN capable, as they have TCP/IP capabilities, yet in increasingly many cases they restrict this by forcing the user to rely on the online master server for matchmaking, which means that, once the owners inevitably turn the server off, the game’s online mode becomes useless.

I find it sad that old games had more flexibility just by allowing users to manually type the server IP address than current games.

Tangent over! :smiley: