Hi, I am Gleade. I decided to start learning Java over the last few months and am quite interested in developing online games.
So I decided to start working on a networking library that will allow me to send raw data over a network. I have tried several networking libraries
already and could only find libraries that sent serialized objects over the network which provided a bit too much of a header for my simple tasks.
SFNet (Simple Fast Networking)
A simple Java networking library for TCP (no UDP yet) server-client connections.
The main idea behind this library is to allow the user to create fast networked games.
I have been influenced by a number of libraries already available, but I couldn’t find one that allowed you to send raw data over a network.
Currently supports:
- server-client connections
- easy-to-use packet container
- sending / receiving packets
- SQL database connections with an easy to column class
Repository:
Example:
Main:
public static void main(String args[])
{
System.out.println("Server.");
ServerStarter.listen(1337);
ServerStarter.addListener(new ExampleServerListener());
ClientStarter.connect("127.0.0.1", 1337);
ClientStarter.addListener(new ExampleClientListener());
Packet testPacket = new Packet();
testPacket.writebyte((byte)1);
testPacket.writestring("Hello World!");
ClientStarter.sendPacket(testPacket);
}
Client Listener
public class ExampleClientListener implements SocketListener
{
@Override
public void received(Packet packet)
{
// Get the message ID of the packet
int messageId = packet.readbyte();
switch(messageId)
{
// Receive a string based message from a player
case 1:
String message = packet.readstring();
System.out.println("Client: " + message);
break;
// Receive a positional based messaage from the player
case 2:
int x = packet.readbyte();
int y = packet.readbyte();
System.out.println("Player position update - x: " + x + " y: " + y);
break;
}
}
}
Server Listener
public class ExampleServerListener implements SocketListener
{
@Override
public void received(Packet packet)
{
int messageId = packet.readbyte();
switch(messageId)
{
// Receive a string based message from a player
case 1:
String message = packet.readstring();
System.out.println("Server: " + message);
ServerStarter.sendGlobalPacket(packet);
break;
// Receive a positional based messaage from the player
case 2:
int x = packet.readbyte();
int y = packet.readbyte();
System.out.println(" Server Player position update - x: " + x + " y: " + y);
break;
}
}
}