GNetLib V0.0.0.3 (A Simple Java Networking Library)

This looks like an easy-to-use, neat library.

When do you estimate to have UDP support done?

i cant get it to work tell me what am doing wrong here

Here i Create Sevrer

ArrayList names = new ArrayList();

public void CreateServer() {
	GNetServer server = new GNetServer("127.0.0.1", 8080);
	
	server.addEventListener(new ServerEventListener() 
	{
		@Override
		protected void connectionEvent(final ClientModel client, final ServerEvent event)
		{
			if (event.clientConnected) {
				Sys.SendSysMsg(names+"");
			}
		}

		@Override
		protected void packetReceived(ClientModel client, Packet packet) {
			 if (packet.getPacketName().equals("LoginPacket"))
			 {
				String name = (String) packet.getEntry("name");
				int id = (int) packet.getEntry("id");
				names.add(name);
				Sys.SendSysMsg(id+"");
			    return; // event handled.
			 }
		}
	});
	
	if (server.bind())
	{
	   server.start();
	}
}

Here is when i connect to server
public void ConnectToServer(String ip,int port) {

	GNetClient client = new GNetClient("127.0.0.1", 8080);
	
	client.addEventListener(new ClientEventListener()
	{

		@Override
		protected void connectionEvent(ServerModel server, ClientEvent event) {
			
			if(event.connected) {
				Packet loginPacket = new loginPacket("JIM", 1);
			    server.sendPacket(loginPacket);
			}

		}

		@Override
		protected void packetReceived(ServerModel serverModel, Packet packet) {
			
			 return; // event handled.
		}
		
	});
	
	if (client.bind())
	{
	   client.start();
	}
}

Login Packet

public class loginPacket extends Packet{

public loginPacket(String username, int ID) {
	super("LoginPacket", 2);
	addEntry("name", username);
	addEntry("id", ID);
}

}

it wont print out the name or add it to the list?!

You’re doing everything right :stuck_out_tongue:

You just need to move the client to its own file, to my knowledge you can’t connect a client in the same file / JVM you’ve opened the server on.

Example of what you’re trying to do I believe
:


public static void main(String[] args) {
    openServer();
    connectSomeClient();
}

(Sorry if I’m not using the proper terms/being descriptive enough as I just woke up, I’ll be more than happy to help you if you need it, just post back or PM me ^__^)

Notice how in the package overview picture, ClientTest and ServerTest are present? (2 different files)

Example of what you need to do: (2 different files, 1 for server, 1 for client, each has their own main method)


// ServerLauncher.java

public static void main(String[] args) {
    // Open your server normally as you've done.
}


// ClientLauncher.java

public static void main(String[] args) {
    // Connect to the server like you've done.
}

Thanks!

Hopefully within the next week :slight_smile:

it still wont let me print the players x and y
if (packet.getPacketName().equals(“LoginPacket”))
{
Integer x = (Integer) packet.getEntry(“xpos”);
Integer y = (Integer) packet.getEntry(“ypos”);
Sys.SendSysMsg(x+","+y);
return; // event handled.
}

btw can you add functions like this

if(client.HasConnected()){}
if(!client.HasConnected()){}

there is a bug that you can keep on pressing a button to connect same client 10000 times xD

I love it! It’s very user friendly :slight_smile: :slight_smile: 8)

and this metod does not seam to work btw it wont print or anything been trying for hours to print x and y

int x;
int y;
    g2.drawString("xpos: "+x+" ypos: "+y, 50, 180);

                    @Override
		protected void packetReceived(final ClientModel client, final Packet packet)
		{
			
			 if (packet.getPacketName().equals("LoginPacket"))
			 {
				  x = (int) packet.getEntry("xpos");
			      y = (int) packet.getEntry("ypos");
			      Sys.SendSysMsg("RECIVED!");
			      return; // event handled.
			 }
			 Sys.SendSysMsg("RECIVED!");
		}

Thank you! ;D ;D

@VIrtueeL

In the opening post I posted everything required to perform what you’re trying to do mate :stuck_out_tongue:

  • Sorry you’re having troubles.

What you need to do:
Open server in its own public static void main(String[] args) method.
Connect a client in its own public static void main(String[] args) method.

Once the clients connected to the server, send your packet: (Like you’re wanting to do)


 @Override
   protected void connectionEvent(final ServerModel server, final ClientEvent event)
   {
      if (event.connected)
      {
            // we've just connected to the server
            // proceed to send the CoordsPacket to the server.
            Packet coordPacket = new Packet("CoordsPacket", 2);
            coordPacket.addEntry("x", new Integer(50));
            coordPacket.addEntry("y", new Integer(100));
            return; // event handled.
      }
   }

Than server-sided you’ll receive the packet and handle the data as you wish:


@Override
protected void packetReceived(final ClientModel client, final Packet packet)
{
   if (packet.getPacketName().equals("CoordsPacket"))
   {
      Integer x= (Integer) packet.getEntry("x");
      Integer y= (Integer) packet.getEntry("y");
      // Do something with the data
      return; // event handled.
   }
}

Hope this is simple enough for you to get a trial working, are any other users having issues?

What do you mean by this?
This is a multi-threaded server meaning it’s not meant to handle 1 client only, it can handle multiple clients.

Are you saying that once you create your GNetClient you can spam the bind/start methods to continuously connect the same client?

yes!

what i mean by this

			@Override
			protected void packetReceived(final ClientModel client, final Packet packet) {
				 Sys.SendSysMsg("RECIVE!");
				 if (packet.getPacketName().equals("move")) {
					 double xp = (double) packet.getEntry("x");
					 double yp = (double) packet.getEntry("y");
					 Sys.SendSysMsg("RECIVE!");
				 }
				 Sys.SendSysMsg("RECIVE!");
				 return; // event handled.
			}

it wont print msg or anything i can send packets from server to client
and client sends the pack but server does not take it and i cant use its data i have been trying all night
and yes am starting them in main string args thing

here is concol here it connects and sends packet
GNetClient -> Obtaining target host address.
GNetClient -> Target host obtained, attempting to connect to it.
GNetClient -> We’ve been connected to the server.
GNetClient -> Sending packet: LoginPacket

but server does not say RECIVE
GNetServer -> Obtaining target host address.
GNetServer -> Target host obtained, attempting to bind to it.
GNetServer -> Server is now being broadcasted on given address.
GNetServer -> A client has been connected. (online: 1)
System: Client Has Connected!

or that packet has been sent

Thank you :slight_smile: Helped me fix a bug.

Turns out I didn’t initialize the clients ‘connected’ variable when/where I should of :stuck_out_tongue:

  • Fixed

Uploading new .jar and source files.

thanks =D
tell me when thay are uploaded =D

Updated links in OP for downloading fixed update and source :slight_smile:

i might do some tutorials on how to use this library once i get pretty good at it
it have learned me so much about networking in 1 day it’s really good for beginners =D

That would be awesome XD

Glad I could help :slight_smile: if you need any more help PM me, would like to make a more in-depth tutorial once I get familiar with github ;D

  • Overhauling the library -
    (8) Habit 8))

  • V0.0.0.3 will take some time before ready for release, sorry D:

Looks Awesome Greate Job!!!

Yes, a multi-threaded TCP server framework is included in V0.0.0.2.
(V0.0.0.3 will hopefully include a advanced multi-threaded TCP/UDP server framework)

At the moment there’s no ‘limit’ for the amount of clients you can connect although I would like to add one, along with a login queue system :slight_smile:

NOTE: I’m still working on the server/client (GNetLib) library overhaul with UDP implementation in mind, it will take some time as I’ve never tried any UDP related programming.

Thanks for replying.

  • TehJavaDev

On another note:
For those who have used this library, is there anything you’d like to see added?