https://github.com/therolffr/miniserver
Simplified Java TCP server. Can be used for multiplayer purposes, like games
https://github.com/therolffr/miniserver
beginner-friendly java multiplayer multithreaded-server multithreading tcp tcp-client tcp-server tcp-socket
Last synced: 8 months ago
JSON representation
Simplified Java TCP server. Can be used for multiplayer purposes, like games
- Host: GitHub
- URL: https://github.com/therolffr/miniserver
- Owner: TheRolfFR
- Created: 2019-11-30T11:33:58.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-27T08:14:16.000Z (almost 5 years ago)
- Last Synced: 2025-01-28T12:43:45.885Z (about 1 year ago)
- Topics: beginner-friendly, java, multiplayer, multithreaded-server, multithreading, tcp, tcp-client, tcp-server, tcp-socket
- Language: Java
- Homepage:
- Size: 179 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# miniServer
Simplified Java TCP server. Can be used for multiplayer purposes in games
## Setup guide
You can copy folders and files from the ``src`` folder directory directly inside your project.
If you don't want the examples, you can delete the ``example`` subfolder.
## How to use it
First of all don't forget to import it
```java
import com.therolf.miniServer.Client;
import com.therolf.miniServer.Message;
import com.therolf.miniServer.Server;
```
## Client side
-
You need to connect to your server so what you do is that you create a client:
```java
String ip = "127.0.0.1"; // match with server
int port = 3000; // match with server
Client client = new Client(ip, port);
```
-
What you need next is a username to be recognized ans send messages to everyone :
```java
String login = "John Wick";
client.send(login);
```
- Then when you are authed, the
isAuthed() method will return true.
- You can then finally send strings with the
send(String s) method.
- Alternatively, you can listen to incoming messages with the
isReady and read messages:
```java
// receive messages
while(client.isRunning()) {
if(client.isReady()) {
Message m = client.read();
System.out.println(m.getPseudo() + ": " + m.getMessage());
}
}
```
(Don't forget to wrap it with try catch for error handling)
Go checkout [ClientExample.java](./src/example/ClientExample.java) for a real example.
## Server side
The server side is also simple!
-
You create a server object:
```java
String port = 3000; // match with client
String serverName = "ServerExample";
String ip = "127.0.0.1"; // match with server
Server miniServer = new Server(port);
Server miniServer = new Server(port, serverName);
Server miniServer = new Server(port, serverName, ip);
```
-
You listen for messages and respond to them if you want:
```java
miniServer.setMessageListener((fromPseudo, message) -> {
switch (message) {
case "hello":
miniServer.sendToEveryoneElse(fromPseudo, fromPseudo + " says hello");
break;
case "list":
miniServer.sendFromTo(miniServer.getServerName(), fromPseudo, miniServer.getAllPseudos());
break;
case "ping":
miniServer.sendFromTo(miniServer.getServerName(), fromPseudo, "pong");
break;
default:
miniServer.sendToEveryone(fromPseudo, message);
break;
}
});
```
-
You run it and it does its life on its own:
```java
miniServer.run();
```
Go checkout [ServerExample.java](./src/example/ServerExample.java) for a real example.