Skip to content

Creating a Server

Ben edited this page Sep 22, 2021 · 3 revisions

Following this guide you will learn how to create your first Server. We assume you already imported the library to your project so we won't cover that here.

Lets first look at the code snippet:

package dev.bitbite.networking.example;

import dev.bitbite.networking.Server;

public class ExampleServer extends Server{

	public ExampleServer(int port) {
		super(port);
	}

	@Override
	protected void processReceivedData(String clientAddress, byte[] raw) {
		String data = new String(raw);
		System.out.println(clientAddress + ": " + data);
		send(clientAddress, data.getBytes());
	}

}

Yes. Thats all. Instantiate the ExampleServer class using ExampleServer server = new ExampleServer(1337); and call server.start(); on the created instance and the server will start listening on port 1337. You can of course change the port as you wish. Also you can pass the port directly to the super-constructor instead of as an argument of the ExampleServer constructor, but thats up to you. Incoming data will now be passed to the processReceivedData(String, byte[]) method with the first argument being the Remote Socket Address of the client who sent the data, and the second argument being the data itself, represented as a String. Here, the data is being posted to console and then sent back to the client.

Clone this wiki locally