Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  server for a java game  (Read 376 times)
0 Members and 1 Guest are viewing this topic.
Offline dav0012

Senior Newbie





« Posted 2014-04-28 11:55:33 »

hello all, am new to java programming.. pardon the siliness of my question :
 i made a java game, involving moving some object (class name piece)
every location of every pieces is stored inside an Array.

i am switching my game into a multiplayer one, for that i'll need to create a server.
am confused about the server Huh
my server should be developped in java itself right?
using socket programming Huh
Offline orogamo
« Reply #1 - Posted 2014-04-28 12:09:14 »

hello all, am new to java programming.. pardon the siliness of my question :
 i made a java game, involving moving some object (class name piece)
every location of every pieces is stored inside an Array.

i am switching my game into a multiplayer one, for that i'll need to create a server.
am confused about the server Huh
my server should be developped in java itself right?
using socket programming Huh

Well it depends on the Game.
If it's a simplistic game, you could use TCP. (I think minecraft still uses TCP, correct me if I'm wrong)
But for more advanced games you should use UDP. (Like games that are network heavy, f.ex Call Of Duty, Battlefield etc.)

At first I would choose TCP + DataStream (DataInputStream & DataOutputStream).

TCP is easier to learn for someone new to networking, because DataStreams contains functions like: readInt(), readLong(), writeInt(), writeLong() etc.

UDP is a bit more advanced were you receive/send Datagram Packets which contains bytes only.
But if you wrap that in a ByteArrayStream(or whatever its called) it's just as easy(just a bit more code) Smiley

Read up on TCP & UDP and find out which protocol works for your game Smiley

EDIT:
Quote
my server should be developped in java itself right?
It would be easier to create the server in java (atleast when the game is), but you're free to choose which ever language you want Smiley
Offline dav0012

Senior Newbie





« Reply #2 - Posted 2014-04-28 12:27:40 »

hello all, am new to java programming.. pardon the siliness of my question :
 i made a java game, involving moving some object (class name piece)
every location of every pieces is stored inside an Array.

i am switching my game into a multiplayer one, for that i'll need to create a server.
am confused about the server Huh
my server should be developped in java itself right?
using socket programming Huh

Well it depends on the Game.
If it's a simplistic game, you could use TCP. (I think minecraft still uses TCP, correct me if I'm wrong)
But for more advanced games you should use UDP. (Like games that are network heavy, f.ex Call Of Duty, Battlefield etc.)

At first I would choose TCP + DataStream (DataInputStream & DataOutputStream).

TCP is easier to learn for someone new to networking, because DataStreams contains functions like: readInt(), readLong(), writeInt(), writeLong() etc.

UDP is a bit more advanced were you receive/send Datagram Packets which contains bytes only.
But if you wrap that in a ByteArrayStream(or whatever its called) it's just as easy(just a bit more code) Smiley

Read up on TCP & UDP and find out which protocol works for your game Smiley

EDIT:
Quote
my server should be developped in java itself right?
It would be easier to create the server in java (atleast when the game is), but you're free to choose which ever language you want Smiley

thats 1 incredibly helpful answer... U Rock Smiley Smiley Smiley

last thing,
my game is logic is as such :

player1(host)
player2(client)

-1st player1 choose game type and setting, and send details to server.
-server pu host & game detail in a list (available game)
-client(player2) pick game from list and request server for conection detail to host(player1)
-server send connection detail to both host and client so they can create a connection between them
-host and client initiate connection and start game

during the game the only data exchange is a small array
from what i just read, i think TCP and writing the server in java will be okay.

i would like to to get your opinion orogamo

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline orogamo
« Reply #3 - Posted 2014-04-28 12:44:58 »

thats 1 incredibly helpful answer... U Rock Smiley Smiley Smiley

last thing,
my game is logic is as such :

player1(host)
player2(client)

-1st player1 choose game type and setting, and send details to server.
-server pu host & game detail in a list (available game)
-client(player2) pick game from list and request server for conection detail to host(player1)
-server send connection detail to both host and client so they can create a connection between them
-host and client initiate connection and start game

during the game the only data exchange is a small array
from what i just read, i think TCP and writing the server in java will be okay.

i would like to to get your opinion orogamo



How i understand it, you have:
  • A Server for giving out host informtion to clients
  • The host for hosting the game (obviously Tongue)
  • A client for having fun Smiley

For simplicity you can use Json (Google GSON), and create wrapper classes for the JSON parser.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
package com.something.cool
public class HostInformation {
    private String GameName; // Custom game name, like: "Orogamos amazing server with cats and meow"
    private String HostAddress; // Host IP or Hostname, like: "192.168.0.1", "localhost" or what ever...
    private int HostPort; // Port to connect to host

    public void setGameName(String name) {GameName = name;}
    public String getGameName() {return GameName;}
    ...
    //Add more fields if necessary
}

And the Game State
1  
2  
3  
4  
5  
6  
7  
8  
9  
package com.something.cool
public class GameState{
    private ArrayList<Integer, Integer> positions;

    public void setPositions(ArrayList<Integer, Integer> pos) {positions = pos;}
    public ArrayList<Integer, Integer> getPositions() {return positions;}
    ...
    //Add more fields if necessary
}


You can fill out the classes convert them to json:
1  
2  
3  
4  
5  
6  
7  
HostInformation hostInfo = new HostInformation();
hostInfo.setGameName("Orogamos amazing server with cats and meow");
...
Gson gson = new Gson();
String json = gson.toJson(hostInfo);

sendToServer(json); //Example function


EDIT START
And parse them back:
1  
2  
3  
String json = receiveHostInformationFromServer(); //Example function
Gson gson = new Gson();
HostInformation hostInfo = gson.fromJson(json, HostInformation.class);


You can use these classes as a guide:
MojangAuthentication.java // the json sender, receiver and parser. Ignore saveCredentials and loadCredentials those are local filesystem Smiley
Authentication.java // Wrapper classes for json
This method uses HTTP Post, but can easily be converted to Java Sockets using byte[] and Streams
EDIT END

I hope this helps you, if you have any questions feel free to ask Smiley
Offline dav0012

Senior Newbie





« Reply #4 - Posted 2014-04-28 15:13:52 »

oooooook, json kinda new to me
and am still trying to process your tips
thanks Smiley
processing in progress.... Wink
Online Longarmx
« Reply #5 - Posted 2014-04-28 20:57:31 »

Well it depends on the Game.
If it's a simplistic game, you could use TCP. (I think minecraft still uses TCP, correct me if I'm wrong)
But for more advanced games you should use UDP. (Like games that are network heavy, f.ex Call Of Duty, Battlefield etc.)

TCP keeps sending the packet until it is received. It should be used for important information or information that can't be lost (ex. in-game shop) Think of it like a two way conversation between the server and client.

UDP just sends the packet and doesn't care if it is received or not. Use it for information that is okay to be lost (ex. player coordinates) This is like a one-way communication from the client to the server or vice versa.

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.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!