GNetLib V0.0.0.3 (A Simple Java Networking Library)

Hey JGO, I’d like to present to you GNetLib, a simple yet effective java networking library that I’ve been working on.
NOTE: A list of features can be seen below.
This library is in a very early stage of development, so if there’s a feature you’d like to see added that would prove useful please post it and I will do my best to implement it in future updates :slight_smile:

Library details:
Name: GNetLib
Version: 0.0.0.3
Project creation date: 10/27/2014 (Re-written from scratch no c&p)

Developer notes:
-> Working on a multiplayer demo to showoff the features of GNetLib.
-> Server capacity tested on 127.0.0.1, reached around 4,136 online clients.
–> (Ran all clients on 1 JVM while server was ran on a separate JVM :D)

  • UDP feature
  • Customization of serialization
  • I will invest time and post back when I’ve found more features GNetLib has that kryonet doesn’t :slight_smile:

How to get started using this library:
You’ll need to download the .jar below, and add it to your projects workspace/build path.
GNetLib download -> GNetLib.jar (24.2kb)
GNetLib source download link -> GNetLib Source.zip (13.9kb)
(Source code note: If you use this library for a project posted online, please include my name in the credits ;D)

V0.0.0.3 Bugs:
Fixed a few client-sided bugs relating to the new logging system - 10/28/2014 ✓
Fixed a few server-sided bugs relating to the new event system/logging system - 10/28/2014 ✓
Fixed a client-sided bug where the client would crash when the server randomly shut down - 10/28/2014 ✓
Fixed a server-sided issue where the online player count would be wrong upon connecting (during event) - 10/28/2014 ✓

Development status:
Green = finished.
Red = not yet started.
Blue = undergoing development.
-> Server now has a multi-threaded TCP framework
-> Server now has a ServerMonitor GUI (Options to show/dispose of statistics interface)
-> Server can now send/receive TCP objects
-> Server/Client can now send/receive custom packets (org.gnet.packet.Packet)
-> New and improved Packet system, 1 class vs 3 (Now hash-mapped)
-> Server now has the ability to choose which client to forcefully disconnect (org.gnet.packet.ClientShutdownPacket)
-> Server/Client has a new EventListener that handles connection related events along with incoming packet events
-> Server now has a list that contains all online clients (ClientModel instances)
-> Server now generates a unique identification number for each client that connects (UID)
-> Server/Client can now report all error/debug messages in the EventListener
-> Server now has the ability to send mass messages to all online clients (With ability to single out a client)
-> ServerMonitor can now be enabled/disabled on the fly (No longer enabled prior to server.start())
-> Working on giving the server a option to set the maximum number of clients online at once
-> Working on giving the server a login queue system (Encase too many connection requests are performed at the same time)
-> Working on giving the client a internal ping system
-> Working on giving the client a option to request the # of clients connected to the server (Will be a internal packet)
-> Working on a multiplayer demo :slight_smile:
-> Add a peer to peer feature for this library
-> Add the option to allow sending of UDP packets along with the current TCP method of sending packets
-> Add the option for a client to re-connect if a connection attempt failed (# connect attempts & delay between attempts)

Code to create/start a server:


// Host to use for the server:
final String host = "127.0.0.1";

// Port # to broadcast the host on:
final int port = 43594;

// Setup our server.
final GNetServer networkedServer = new GNetServer(host, port);

 // Disable debugging, (enabled by default) as we'll manually handle it via our EventListener.
networkedServer.setDebugging(false);

// Add our event listener to manage events.
networkedServer.addEventListener(new ServerEventListener() {

	@Override
	protected void clientConnected(ClientModel client) {	
	}

	@Override
	protected void clientDisconnected(ClientModel client) {
	}

	@Override
	protected void packetReceived(ClientModel client, Packet packet) {
	}

	@Override
	protected void debugMessage(String msg) {
	}

	@Override
	protected void errorMessage(String msg) {
	}
});
		 
// Attempt to bind the server.
networkedServer.bind();

// Once binded, finally start our server.
networkedServer.start();

Code to create/connect a client to the server:


// Host to connect to:
final String host = "127.0.0.1";

// Port # to connect to the host on:
final int port = 43594;

// Setup our client.
final GNetClient networkedClient = new GNetClient(host, port);

 // Disable debugging, (enabled by default) as we'll manually handle it via our EventListener.
networkedClient.setDebugging(false);

// Add our event listener to manage events.
networkedClient.addEventListener(new ClientEventListener() {

	@Override
	protected void clientConnected(ServerModel server) {
	}

	@Override
	protected void clientDisconnected(ServerModel server) {
	}

	@Override
	protected void packetReceived(ServerModel server, Packet packet) {
	}

	@Override
	protected void debugMessage(String msg) {
	}

	@Override
	protected void errorMessage(String msg) {
	}
});

// Attempt to bind the client.
networkedClient.bind();

// Once binded, finally start our client.
networkedClient.start();

How to use the new ServerMonitor:
(Picture of it can be seen here: link)


// To enable/show it:
server.enableServerMonitor();

// To dispose of it:
server.disableServerMonitor();

How to create your own custom packets:
This library sends “packets” (data) across the network to/from clients via org.gnet.packet.Packet class, you can create a class that extends this class or you can create the Packet on the fly.
A demonstration of both methods can be seen below.

NOTE: The Packet class constructor takes 2 parameters.
First parameter is a String that represents the packets name. (This will be used on the receiving end to detect the packets name)
Second parameter is a Integer that represents how many data slots to reserve for this packet. (Can not exceed this amount)

How to create your own custom Packet on the fly:


Packet loginPacket = new Packet("LoginPacket", 2); // 2 slots reserved
loginPacket.addEntry("username", new String("jimmy")); // 1st slot (name/value)
loginPacket.addEntry("pinCode", new Integer(5600)); // 2nd slot (name/value)

How to create your own custom Packet via extending Packet:


public class LoginPacket extends Packet {
	public LoginPacket(String user, int pinCode) {
		super("LoginPacket", 2); // super must be envoked

		// You can add your data here
		// or add it to the packet once created.
		addEntry("username", user);
		addEntry("pinCode", pinCode);
	}
}

How to send your own custom packet:
At the moment you must use a instance of “ClientModel” or “ServerModel” to send a packet, future updates will include a method within the packet to send to a destination address.
ClientModel = Server-sided instance for a client.
ServerModel = Client-sided instance for the server.

In this example we will be sending the server a mock “LoginPacket” once we’ve been connected.


@Override
protected void clientConnected(ServerModel server) {
	// Client has just connected to the server, here we'll send our packet.
	Packet loginPacket = new Packet("MockLoginPacket", 2);
	loginPacket.addEntry("username", new String("Jimmy"));
	loginPacket.addEntry("pinCode", new Integer(5600));
	server.sendPacket(loginPacket);
	return; // event handled.
}

How to receive your own custom packet:
In the example below we’ll be receiving the packet server-sided we sent from the client earlier.


@Override
protected void packetReceived(ClientModel client, Packet packet) {
	// Make sure the incoming packet has the title we wish to process data from.
	if (packet.getPacketName().equals("MockLoginPacket")) {
		String user = (String) packet.getEntry("username");
		Integer pinCode = (Integer) packet.getEntry("pinCode");
		// Do something with the data...
		return; // event handled.
	}
}

Project source overview:

Thanks for reading JGO, I’ve provided everything required to get started using this library so hopefully some users/browsers will find this library useful and use it in a project of theirs. :slight_smile:

i’m confused, where are the .java files ?

EDIT: Source files have been uploaded, you can download the .zip in the OP.

thanks :slight_smile:

Your welcome, if you browse the source code and notice anything out of place or anything that looks like it’s done in a poor manner please post back :slight_smile:

Hi

Which features not already implemented in Kryonet does your library provide?

Thanks for replying mate, I’m not too familiar with Kryonet and the implemented features of it, so I’ll invest some time browsing Kryonet’s features than compare it to my available features and I’ll post back for ya mate. :slight_smile:

On another note:
This library’s basically for users who are somewhat new to networking in Java or for users who are experienced in the java networking field but are tired of writing their own networking system/not being able to.

I’m aiming to make this library mainly easy to use and adaptable to users needs.

At the moment this library is capable of sending custom packets that are composed of custom data values, however I know that a game using this library/user using this library will require more functionality than what I’ve implemented, so if you’ve used / tested this library in a sample game or whatnot, please post back on what you’d like to see implemented.

NOTE: I’d like to write a test case for this library, it will be a chat client/server. (client-to-client chat system, chat will relay through the server)

Decided to overhaul the server and client :slight_smile:

Just about finished and let me say I’m happy with the outcome XD

It’s 4am so I’m going to call it a night, I’ll upload the new .jar tomorrow.

Here’s a sneak peek at the overhaul and how server/client instantiation will work:
JGO Pastebin link: New overhaul

Not meaning to hijack, but FWIW in KryoNet when you register classes they are given an ordinal which is sent over the network to identify the class. To send class names for unregistered classes, you can do:


endpoint.getKryo().setRegistrationRequired(false);

KryoNet is a simple networking API based on NIO (eg connect, send objects, receive objects, disconnect). How objects are serialized across the wire can be customized. The KryoSerialization class (used by default) uses Kryo for serialization. JsonSerialization uses JsonBeans (nice for debugging since the data is human readable).

If you preferred your networking API, you could still use Kryo or JsonBeans for serialization. Might save you some effort and allow you to focus more on the networking API. Eg, KryoNetty uses Netty for the networking and Kryo for the serialization.

No thank you mate :slight_smile:

This library is in a very early stage of development and I plan to add a few other methods of serialization/deserialization further down the road once I shine up the core framework and get some more essential features working.

I’d prefer not to involve another library written by someone else :-* at this point as this is a project of my own, I’d like to keep this networking library small (Relating to library size & code required to instantiate) and self contained.

I have no desire to use ‘Kryo serialization’ at this point :point:
Thank you for your FWIW though, now I’ll have serialization as a priority in my next update spree :slight_smile:

Version 0.0.0.2 .jar and .zip source files have been uploaded!

Check out the OP for links and new finished features :slight_smile:

Seriously take a look at Kryo & Kryonet, please :slight_smile:

The only way to get attention is to make a peer to peer solution, which KryoNet doesnt offer

Will definitely look into that today when I get back from shopping :slight_smile:

I have browsed kryonet’s source code and it’s pretty big compared to mine, only noticable features in my eyes I’m missing compared to kryonet at the moment are deep serilization and UDP followed by peer to peer networking.

Yet again I only browsed a couple files so obviously :stuck_out_tongue: I need to invest more time :slight_smile:

  • Thanks for replying guys.

Development for GNetLib has been resumed!

  • I’ve dropped my networked monopoly clone and posted it in the WIP board as a free unfinished project.

NOTE: Dropping my monopoly clone had nothing to do with this library. (For those who are curious)

Please don’t tell me that your library is better because it is smaller. As long as a library is modular, becoming big isn’t a concern.

this looks awesome and easy to learn thank you :smiley:
please do some video tutorials on this =D
i might do when i learn it please just keep on updating it so it does not turn in to an inactive project!

badass

http://s17.postimg.org/3rtt4jrcf/img.png

do movement tutorials as well
for ex

how to make a small mutliplayer game with just 2 rectangles moving on both clients

I never said this project’s better than theirs due to code size ::), I said what I noticed while browsing the source code was that kryonet has alot more source code than my project XD

I’ll keep developing this library :slight_smile: feedback from testers would be nice to help me further devlopment.

This library provides all the essential features needed to start a multiplayer game like the one you’ve mentioned.

Hope I don’t seem rude :stuck_out_tongue:
I’m rather busy in life although I do plan on making a client-client chat server demo soon ^___^

Opening post shows how to:
Open a server.
Connect client.
Handle disconnecting / connecting events.
Create/send/receive packets (data) that contain what you want (IE Player coords etc)

  • Which is all that’s basically required to make this multiplayer game you’ve suggested, the rest is up to you :slight_smile:

of times read 1337