Thread: GNetLib V0.0.0.3 (A Simple Java Networking Library)

Results 1 to 7 of 7
  1. #1 GNetLib V0.0.0.3 (A Simple Java Networking Library) 
    Banned
    Join Date
    Jul 2015
    Posts
    126
    Thanks given
    21
    Thanks received
    9
    Rep Power
    0
    Found this and have been using this in a few applications I made. Useful library, its a year old however its open source as well.

    Here's the original link.

    GNetLib.jar
    GNetLib V0.0.0.3 (A Simple Java Networking Library) - Java-Gaming.org

    Source
    http://www.mediafire.com/download/1c...Lib+Source.zip

    Extremely easy to use. Here's its usage.

    Server
    Code:
    package com.main;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.gnet.packet.Packet;
    import org.gnet.server.ClientModel;
    import org.gnet.server.GNetServer;
    import org.gnet.server.ServerEventListener;
    
    
    public class Server {
    	
    	private static final Logger logger = Logger.getLogger(Server.class.getName());
    
    	public static void main(String[] args) {
    		
    		final String host = "localhost";
    		final int port = 43594;
    		
    		final GNetServer netserver = new GNetServer(host, port);
    		
    		netserver.addEventListener(new ServerEventListener() {
    
    			@Override
    			protected void clientConnected(ClientModel e) {
    				System.out.println("Client has connected " + e.getUuid());
    				
    			}
    
    			@Override
    			protected void clientDisconnected(ClientModel arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void debugMessage(String arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void errorMessage(String arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void packetReceived(ClientModel client, Packet packet) {
    				
    				/**
    				 * This is just a sample packet I created to show it works.
    				 */
    				
    				if(packet.getPacketName().equals("LoginPacket")) {
    					String name = (String) packet.getEntry("playerName");
    					String password = (String) packet.getEntry("playerPassword");
    					System.out.println(name + " " + password);
    				}
    				
    			}		
    		});
    		
    		netserver.enableServerMonitor();
    		netserver.bind();
    		netserver.start();
    		logger.log(Level.INFO, "Server Online on port" + port);
    	}
    }
    Code:
    package com.main;
    
    import org.gnet.packet.Packet;
    
    public class LoginPacket extends Packet {
    
    	public LoginPacket(String name, String password) {
    		super("LoginPacket", 2);
    		addEntry("playerName", name);
    		addEntry("playerPassword", password);
    	}
    
    }
    Client
    Code:
    package com.main;
    import javax.swing.JOptionPane;
    
    import org.gnet.client.ClientEventListener;
    import org.gnet.client.GNetClient;
    import org.gnet.client.ServerModel;
    import org.gnet.packet.Packet;
    
    
    public class Client {
    	
    	public static void main(String[] args) {
    		
    		final String host = "localhost";
    		final int port = 43594;
    		
    		GNetClient netclient = new GNetClient(host, port);
    		
    		netclient.addEventListener(new ClientEventListener() {
    
    			@Override
    			protected void clientConnected(ServerModel server) {
    				String name = JOptionPane.showInputDialog("What is your name?");
    				String password = JOptionPane.showInputDialog("What do you want your password to be?");
    				
    				System.out.println("Sending login packet to server");
    				
    				
    				LoginPacket login = new LoginPacket(name, password);
    				server.sendPacket(login);
    							
    			}
    
    			@Override
    			protected void clientDisconnected(ServerModel arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void debugMessage(String arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void errorMessage(String arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			protected void packetReceived(ServerModel arg0, Packet arg1) {
    				// TODO Auto-generated method stub
    				
    			}			
    		});
    		netclient.bind();
    		netclient.start();
    	}
    }
    Code:
    package com.main;
    import org.gnet.packet.Packet;
    
    
    public class LoginPacket extends Packet {
    
    	public LoginPacket(String name, String password) {
    		super("LoginPacket", 2);
    		addEntry("playerName", name);
    		addEntry("playerPassword", password);
    	}
    
    }
    Credits: TehJavaDev
    Reply With Quote  
     

  2. #2  
    Developer


    Join Date
    Aug 2012
    Posts
    2,493
    Thanks given
    180
    Thanks received
    1,732
    Rep Power
    2487
    didn't read the actual code yet but i'm quite shocked to at the following:

    Code:
    @Override
    			protected void packetReceived(ClientModel client, Packet packet) {
    				if(packet.getPacketName().equals("LoginPacket")) {
    					String name = (String) packet.getEntry("playerName");
    					String password = (String) packet.getEntry("playerPassword");
    					System.out.println(name + " " + password);
    				}
    				
    			}
    you realizee you're going to send A LOT of redundant bytes
    Reply With Quote  
     

  3. #3  
    Banned
    Join Date
    Jul 2015
    Posts
    126
    Thanks given
    21
    Thanks received
    9
    Rep Power
    0
    Quote Originally Posted by sirtom gf View Post
    didn't read the actual code yet but i'm quite shocked to at the following:

    Code:
    @Override
    			protected void packetReceived(ClientModel client, Packet packet) {
    				if(packet.getPacketName().equals("LoginPacket")) {
    					String name = (String) packet.getEntry("playerName");
    					String password = (String) packet.getEntry("playerPassword");
    					System.out.println(name + " " + password);
    				}
    				
    			}
    you realizee you're going to send A LOT of redundant bytes
    I do realize that, that was just a simple packet I made to show it works.
    Reply With Quote  
     

  4. #4  
    Banned
    Join Date
    Aug 2014
    Age
    33
    Posts
    134
    Thanks given
    53
    Thanks received
    19
    Rep Power
    0
    cool find.
    Reply With Quote  
     

  5. #5  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    30
    Posts
    1,237
    Thanks given
    275
    Thanks received
    957
    Rep Power
    3304
    wtf is this using javas built in serialization for transporting messages

    edit: ugh

    Code:
    			oos = new ObjectOutputStream(clientSocket.getOutputStream());
    it does. protip: don't.

    you're also creating a new thread per connection. read up on asynchronous io.
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
    Reply With Quote  
     

  6. #6  
    Banned
    Join Date
    Jul 2015
    Posts
    126
    Thanks given
    21
    Thanks received
    9
    Rep Power
    0
    Quote Originally Posted by saifix View Post
    wtf is this using javas built in serialization for transporting messages

    edit: ugh

    Code:
    			oos = new ObjectOutputStream(clientSocket.getOutputStream());
    it does. protip: don't.

    you're also creating a new thread per connection. read up on asynchronous io.
    Well I would like to continue to improve this because its very easy to use for people who are interesting in networking. If you have any other tips I'll see what I can do. As I'm learning networking myself.
    Reply With Quote  
     

  7. #7  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    30
    Posts
    1,237
    Thanks given
    275
    Thanks received
    957
    Rep Power
    3304
    Quote Originally Posted by Rust View Post
    Well I would like to continue to improve this because its very easy to use for people who are interesting in networking. If you have any other tips I'll see what I can do. As I'm learning networking myself.
    then stop using java serialization. it performs ridiculously bad in this scenario. you want to be (de)serializing your data as a byte stream instead of traversing the entire object graph of a packet.
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
    Reply With Quote  
     

  8. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Simple JAVA swing tutorial
    By Karma in forum Application Development
    Replies: 10
    Last Post: 11-04-2009, 08:39 PM
  2. Simple java web server
    By Colby in forum Application Development
    Replies: 0
    Last Post: 06-17-2009, 09:19 AM
  3. Simple Java Errors... I'm Dumb :(
    By Deviants in forum Help
    Replies: 7
    Last Post: 02-16-2009, 08:08 AM
  4. CrasherCrisis - simple java doser
    By Zahhak in forum Downloads
    Replies: 16
    Last Post: 01-02-2009, 02:31 AM
  5. Scu11's Simple Java: Inversing Booleans
    By Scu11 in forum Tutorials
    Replies: 17
    Last Post: 04-13-2008, 01:03 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •