GithubHelp home page GithubHelp logo

somegituser123 / jraknet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from whirvis/jraknet

0.0 1.0 0.0 7.49 MB

A protocol implementation of the networking library RakNet

Home Page: https://jraknet.github.io

License: Other

Java 100.00%

jraknet's Introduction

JRakNet

JRakNet is a networking library for Java which implements the UDP based protocol RakNet. This library was meant to be used for Minecraft servers and clients, but can still be used to create game servers and clients for other video games with ease. You can also read the JavaDocs

Protocol Info Version
Current protocol 8
Supported server protocol 8
Supported client protocol 8

Note: Always use the newest version of JRakNet, including bug fix updates as they almost always fix major bugs, add new features, or have optimizations to make the API run faster.

Note 2: Since people are always creating issues on the repo about this, Minecraft clients do not work with JRakNet if you are on the same machine as the server due to how Minecraft is programmed. You will have to use another device or change the server port to anything but 19132 or 19133 (This will prevent the server from showing up in the LAN list, but will allow Minecraft to communicate with the server.) However, JRakNet clients work fine with Minecraft servers running on the same machine.

How to use with Maven

In order to add this project to your maven project, you will need to add the maven repository and then the actual dependency:

<repositories>
  <repository>
    <id>maven-repo</id>
    <url>https://raw.githubusercontent.com/JRakNet/MavenRepository/master</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>net.marfgamer</groupId>
    <artifactId>jraknet</artifactId>
    <version>2.8.4</version>
  </dependency>
</dependencies>

How to create a server

Creating a server in JRakNet is extremely easy, all it takes to create one can be seen right here

// Create server
RakNetServer server = new RakNetServer(19132, 10,
		new MinecraftIdentifier("JRakNet Example Server", 137, "1.2", 0, 10,
				new Random().nextLong() /* Server broadcast ID */, "New World", "Survival"));

// Add listener
server.addListener(new RakNetServerListener() {

	// Client connected
	@Override
	public void onClientConnect(RakNetClientSession session) {
		System.out.println("Client from address " + session.getAddress() + " has connected to the server");
	}

	// Client disconnected
	@Override
	public void onClientDisconnect(RakNetClientSession session, String reason) {
		System.out.println("Client from address " + session.getAddress()
				+ " has disconnected from the server for the reason \"" + reason + "\"");
	}

	// Packet received
	@Override
	public void handleMessage(RakNetClientSession session, RakNetPacket packet, int channel) {
		System.out.println("Client from address " + session.getAddress() + " sent packet with ID "
				+ RakNetUtils.toHexStringId(packet) + " on channel " + channel);
	}

});

// Start server
server.start();

This is a simple RakNet server that can be tested through Minecraft by going to the "Friends" tab where the server should show up. Once the server pops up, you should be able to click on it to trigger the connection and packet hooks.

How to create a client

Creating a client in JRakNet is also very easy. The code required to create a client can be seen here

// Server address and port
String SERVER_ADDRESS = "sg.lbsg.net";
int SERVER_PORT = 19132;

// Create client
RakNetClient client = new RakNetClient();
		
// Add listener
client.addListener(new RakNetClientListener() {

	// Server connected
	@Override
	public void onConnect(RakNetServerSession session) {
		System.out.println("Successfully connected to server with address " + session.getAddress());
		client.disconnect();
	}

	// Server disconnected
	@Override
	public void onDisconnect(RakNetServerSession session, String reason) {
		System.out.println("Successfully disconnected from server with address " + session.getAddress()
				+ " for the reason \"" + reason + "\"");
		client.shutdown();
	}

});

// Connect to server
client.connect(SERVER_ADDRESS, SERVER_PORT);

A simple RakNet client, this example attempts to connect to the main LBSG server. When it is connected, it closes the connection and shuts down.

Notes

  • Some DataPacket ID's are reserved by RakNet. Because of this, it is recommended that all game packets not relating to RakNet begin with their own special ID, Minecraft does this (It's header byte is currently 0xFE). It is also recommended that game servers and game clients do not use raw packets at all.
  • For anyone coding server softwares for Minecraft, be aware that the game reserves ports 19132 and 19133 on the machine for server discovery. If you are on the same machine as your server make sure to set the port to anything BUT 19132 or 19133.

jraknet's People

Contributors

whirvis avatar mrpowergamerbr avatar thestacktracewhisperer avatar

Watchers

Max avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.