Hi!
I wrote (pure Java) a simple clone of the Kee Games tank game .It was a two players game , but i’m working in some very basic enemy AI.Meanwhile , i would like instead to play it online with a
friend .The problem is that the tutorials around are very complex and aimed to many players .
There is a simple way to solve my problem ?
Thanks in advance.
P.S.:
i have the basic networking knowledge you can find in every decent Java book
P.P.S.:
not to say that a simple tip like where to look is more than enough
Unfortunately, networking is a complex problem, whether it’s 2 players or 1000. You will need to run the game on the server, and send the player’s inputs to the server. The server will then need to send the game state back to the clients for them to show to the player. This leads to lag though, so you need to compensate for that.
What is the actual part of networking your stuck on? Is it the sockets and sending data, or designing a network paradigm?
Not entirely true. To avoid a server you could also use P2P (Age of Empires II: HD Edition does that and Factorio did as well in an early state). I’d recommened you to go with a server / client architecture since P2P is kinda outdated / has serveral problem. Imo it’s easier to program a game for 2 players than for x players tho
[quote]To avoid a server you could also use P2P
[/quote]
Firewall and NAT want to have a word with you.
Most players don’t know how to do port-forwarding so you would effectively still need a server in the middle.
[quote]It definitel easier to program a game for 2 players than for x players
[/quote]
I would say it depends less on player count and more on the type of gameplay, it get’s complex even with 2 players as soon as interpolation, physics or lag compensation are required.
With two players you could spam the gamestate every frame but there are still some difficulties like handling lag when firing bullets or interpolating smoothly between snapshots even when they come in in the wrong order, etc.
You might even need to synchronize the game time and measure the RTT to correctly sync up bullet positions, since it takes some time until the other side has received the packet and spawned the bullet.
It would be very bad if the players perceptions of hit vs miss diverge too far.
[quote]Unfortunately, networking is a complex problem, whether it’s 2 players or 1000.
[/quote]
THIS :point: :point: :point: :point: :point:
[quote]There is a simple way to solve my problem ?
[/quote]
Not if you want smooth internet play. If you only want to play in local networks ala WLAN/LAN then just spam the gamestate every frame and let the player who shot decide if he did hit or not.
If you want smooth internet play get ready to read a few dozen articles, learn new libraries, create utter garbage and then repeat that process from the beginning.
Try out KryoNet, it’s one of the easier Libraries for networking and has serialization built in.
Read everything from this site that seems remotely relevant to your problem: https://gafferongames.com/
and look for the keywords state, interpolation, client server and synchronization in that blog
Pro-Tip: The slower your gameplay is the easier it is to synchronize. Just try it, maybe it’s already good enough with basic synchronization.
[quote]Firewall and NAT want to have a word with you.
Most players don’t know how to do port-forwarding so you would effectively still need a server in the middle.
[/quote]
True, don’t get me wrong I’m not recommending to do it that way but for purposes of completeness it’s a valid alternative to a server / client concept.
I think the pro-tip might be really valuable for you, synchronizing a round based game for example is indeed much easier than synchronizing a realtime game.
I’d also recommened kryonet
I don’t know a lot about networking–but it seems to me that it might be possible to get something going using WebSockets.
A lot of older Java books cover Sockets, but not WebSockets. I wonder if because it is a relative late-comer, compared to other solutions, that some folks here might not have delved in, especially if they already have another tech that works for them. Maybe they’ve looked into this and discarded the option for some reason?
I have successfully used WebSocket to read from a website that is broadcasting motion capture data in real time. I haven’t tried server-side coding, beyond the tutorials below. But it well be that something simple to patch clients together, with just a bit of JavaScript on HTML would do the trick, if one didn’t want to get into Java EE. Someone correct me if I am wrong!
Background technical information on WebSockets is here:
http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html
I was able to get a client working using the Project Tyrus implementation. No Java EE involved.
https://tyrus-project.github.io/
I was able to successfully work through both of the following tutorials.
Tutorial with JavaFX client, Java EE back end:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/BinaryWebSocket/binaryWebSocket.html
Tutorial with a JavaScript/HTML5 client:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html#overview
Java EE has a distribution for WebServer use: “Web Profile SDK”. It is considerably smaller than the full-blown Java EE. I was able to run both of the tutorials with this smaller distribution.
Websockets are in strong relation with HTTP. Websockets has been created to solve problem of server side events which is hard and weird to achieve with http (for example long polling). It can be used in any case but if you don’t want to support browser client- you just don’t need it. It’s additional overhead in comparision to simplier technology build on top of sockets (kryonet for example). Btw websocket is tcp only so if you want advantages of udp, you just won’t have it
Forgive the delay answering your kind answers .
First of all , thanks !
I casually found a youtube tutorial that looks decent :
Meanwhile , i follow your tips ,even if sometime if you were speaking chineese it was
the same for me
Thx again.