# Server and Client on one instance of a game

**URL:** <https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403>\
**Category:** Coding Questions\
**Created:** [August 6, 2018, 2:55am UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403 "2018-08-06T02:55:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [August 6, 2018, 2:55am UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/1 "2018-08-06T02:55:08Z")

</div>

Hello,  
this topic might be more appropriate to be put in the library category, but this is not suposed to be a question about any library.

In Minecraft in some very early update, offline singleplayer worlds were given multiplayer features. If you’re playing on a singleplayer world, you basically have a server and a client connected to the server without an internet connection in the same instance of the game and you’re controlling that client.

My question is how to do this exact thing.  
I am working on a sequel to my game DubTank, which I didn’t have a chance to release in time, because like a week before my planned release a game called [Diep.io](http://Diep.io) came out, which is pretty much DubTank with a different look.  
Right now I want to have a working LAN-based server; I don’t care about a global server right now as me and my friends are going to test it and we won’t have a 24/7 online server to connect to.  
Also, I’d like to have 2 or more offline clients connected to the server (for splitscreen);

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 6, 2018, 4:36am UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/2 "2018-08-06T04:36:59Z")

</div>

From a programming perspective, there isn’t a huge difference between a local server and a global server. The only difference is that you run a local server on your own computer, and you run a global server on a service like AWS or Google Cloud.

(You can also set your computer up as a global server, but that’s not really what you’re asking about.)

Start looking into server-side development, and start with a local server. That’s how you should start whether you’re planning on eventually implementing a global server or not.

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 6, 2018, 6:53pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/3 "2018-08-06T18:53:33Z")

</div>

What you can do is have two separate projects together, one is the server, one is the client, the server handles the logic and the client draws whatever’s supposed to be drawn. When you start a new singleplayer session on the client, start the server and connect to localhost:9001 (or whatever your port is). This way you can have a singleplayer session backed by the server.  
The main advantage of such a session is that you can now allow other players on your LAN to connect to your computer and play locally.

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [August 9, 2018, 7:04pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/4 "2018-08-09T19:04:54Z")

</div>

So basically, I have to make:  
A server, that handles calculations, data and incoming input.  
Client, that handles drawing, interactions with mouse, screen, keyboard etc. and sending user input.

Then I can make multi-platform game, that will differ in controls and have the same features. This is useful, because I have to make the game code only for the server.

Also, question:  
Can I have a client connected to a server on the same device?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 9, 2018, 8:21pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/5 "2018-08-09T20:21:56Z")

</div>

> [@FurryNightShade](#):
>
> Can I have a client connected to a server on the same device?

Yes, you can. You can have even one server and multiple clients in the same machine. The code from [this](https://discourse.processing.org/t/server-client-reply/1877/11) should be a good starting points. You can check the library documentation and see if it suits your needs.

Kf

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 9, 2018, 8:22pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/6 "2018-08-09T20:22:43Z")

</div>

Of course you can! Just connect to `localhost` under your game server’s port (for example, if the server’s port is `1234` you connect to the IP address `localhost:1234`. To do so, you use the `Client` class, with the constructor `new Client(this, "localhost", 1234)`).

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [August 9, 2018, 9:23pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/7 "2018-08-09T21:23:19Z")

</div>

If I ran the server and the client separatelly on Android one of them wouldn’t work on older versions. Can I do that from a single sketch?

I tried having the “client” interact with the server by calling methods. That is difficult and I still need to implement network clients, so I have two different things that will tell the server to do one thing two different ways.

Also, I haven’t touched the networking since the start of the topic. I am implenenting kind of command-based interaction system.  
Basically I send the server a string “shoot” and the server processes it and calls shoot(int playerID). Is it a good approach?

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 10, 2018, 9:21am UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/8 "2018-08-10T09:21:24Z")

</div>

What do you mean by “it wouldn’t run on older versions”?

That approach where you send the server a string like ‘shoot’ is pretty OK, until you start having problems with either a) performance, b) string lengths.  
If I were you, I’d probably just send the server a byte signifying a player just shot. I’d do this, because bytes have a constant size (1 byte), and I don’t have to worry about lengths. If I have to pass some arguments with my bytes, I send that argument to the server in bytes aswell, eg. an int = 4 bytes, and then the server would receive those based on the first one of the sequence. Creating the data to send could be done with the `java.nio.ByteBuffer`. `ByteBuffer` has functions to deal with converting bytes to arrays, you can use it like so:

```java
// we allocate 5 bytes, because our instruction is (byte, int) for (command, someIntegerArgument), adding the byte numbers of byte (1) and int (4) gives us 5
// then we put a byte of value 2, and an int of value 32
// you can read more about the ByteBuffer here:
// https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
ByteBuffer.allocate(5).put(2).putInt(32).array();

```

This will create an array with a byte 2 and your int converted to bytes. Remember that:

- byte = 1 byte (duh) (8-bit)
- short = 2 bytes (16-bit)
- char = 2 bytes (16-bit)
- int = 4 bytes (32-bit)
- float = 4 bytes (32-bit)
- long = 8 bytes (64-bit)
- double = 8 bytes (64-bit)  
Good luck!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 10, 2018, 9:37pm UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/9 "2018-08-10T21:37:02Z")

</div>

I suggest you create a small version testing a server-client setup and convince yourself how it works. You can use the net library in Processing. Not sure if it will work on Android though. Another alternative is [oscP5](http://www.sojamo.de/libraries/oscP5/) which works on supported mobile devices.

How many clients will your server manage?

Kf

---

<div class="post-metadata">

**Author:** ![FurryNightShade](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@FurryNightShade](https://discourse.processing.org/u/FurryNightShade)\
**Post date:** [August 12, 2018, 2:09am UTC](https://discourse.processing.org/t/server-and-client-on-one-instance-of-a-game/2403/10 "2018-08-12T02:09:38Z")

</div>

@kfrajer I can’t use any libraries in APDE, because of a bug I’ve reported. I can still use Android version in desktop PDE. Though I wish the APDE libraries were fixed. I write code mostly on my phone. I am currently testing it on a game of Pong (because it’s easy to write). I want to have a working network on something simple before I implement it to DubTank 2. With friends we’ve been able to run 6 clients at once (4 being spectators) without any lag. Not sure, how DubTank 2 will handle things. It’s way more complicated game with more data to send to clients.

@Iqdev I thought that might be the way to go, but I wasn’t sure. It might be more useful to use bits.  
In Pong I use only these commands:

> **Pong Command List**
>
> _Bots don’t have clients. They’re there just so that the game works. However bots use commands as well, because why make things complicated?_  
> _By default all players can use commands (like admins), because the game is for testing only._
> 
> - **goto int:playerID float:position** _set a position to move towards_  
> _(If a player doesn’t control a paddle, nothing happens)_
> - **restartround** _restarts current round (duh)_
> - **restartgame** _restarts whole game (duuuuh)_
> - **spectator** _become a spectator_
> - **paddle int:playerid int:paddleID** _Gain control of a paddle_  
> _(If no player has control of a paddle, a bot takes over)_  
> _(If a paddle is taken, nothing happens)_
> - **swappaddles** _Swap paddles between players (or bots)_
> - \*\*disconnect int:playerID _disconnects a player_
> - \*\*set String:property float:value _Set a value to something in game (i.e. set paddle0size 200)_
> - **timescale float:timescale** _Slows down a game or speeds it up. (Mainly so I can )_  
> _For bot movement:_
> - **paddlegoto int:paddleID float:position** _sets a position to move towards for a paddle_

That works just fine. There’s not much delay, because it is a simple game running on LAN.  
On Android, I still haven’t decided between

- Tap to target and move
- Use a controler to move and the rest of the screen to aim and shoot  
Depends on how efective the nework will be…  
Maybe I’ll do both, toggle in settings in server creation.

> **DubTank 2 Command List**
>
> - **move int:playerID int:direction**
> - **shoot int:playerID int:direction**
> - **changeweapon int:playerID int:weaponID**
> - **buyweapon int:playerID int:weaponID**
> - **upgrade int:playerID int:weaponID**
> - **drop int:playerID int:slotID**
> - **equip int:playerID**
> - **swapweapon int:playerID byte:slotFrom byte:slotTo**
> - **chat int:playerID String:content** _Also used for commands_
> - **changeteam int:playerID int:team**
> - **spectator int:playerID**
> - **player int:playerID**
> - **admin int:playerID**
> - **disconnect int:playerID**
> - **ban int:playerID**
> - **create bot int:playerID**
> - **teleport int:playerID int:x int:y**
> - **destroyserver**
> - **restartround**
> - **suicide**

An admin (also the creator of the server) can control other palyers like this. Also I plan to use those commands to control the players character by sending them from their client. I can even control a bot by sending commands.  
However this aproach has a big flaw. It can be used for cheating.

So I need 1 byte for command type and the rest as parameters. Chat is fine, because unless someone spams messages, there will be at most 1 message per second. And even if that’s the case, I can limit the amount of messages sent.

Well, I’ll have to learn to work with just bytes.
