server for a java game

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 ???
my server should be developped in java itself right?
using socket programming ???

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) :slight_smile:

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

EDIT:

[quote]my server should be developped in java itself right?
[/quote]
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 :slight_smile:

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 :slight_smile:
[/quote]
thats 1 incredibly helpful answer… U Rock :slight_smile: :slight_smile: :slight_smile:

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 :P)
  • A client for having fun :slight_smile:

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


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


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:


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:


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 :slight_smile:
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 :slight_smile:

oooooook, json kinda new to me
and am still trying to process your tips
thanks :slight_smile:
processing in progress… :wink:

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.