[Tut] Converting To A Custom Socket

Joined
May 17, 2007
Messages
2,468
Reaction score
681
Hey,

For about... 2 - 3 Weeks now, I've been taking a part-time java cource at a community college, and I'm more experienced at java sockets, ( basically java overall ). So i've decided to test out what i could do and see if i can make a better socket connection from scratch and see if i can make it better than the default one. So far, it's alright.. the only problem is it uses some-what more CPU ( stays in the 15 - 20% range mark 24/7 ), But seriously, it's overall better than the current one that is used in 503's and 508's.

PHP:
IntBuffer buffer = IntBuffer.allocate(10);
  for (int i=0; i < buffer.capacity(); i++) {
  buffer.put(i);
}

PHP:
buffer.position(0);
while (buffer.hasRemaining()) {
  int i = buffer.get();
  System.out.println("i="+i);
}

Those are what basically buffers via client to client.

So basically, my whole point is im trying to waste less connection speed, and less RAM.

Introducing less convected coding helps rather than making it read via package.

TheAJ - [Tut] Converting To A Custom Socket - RaGEZONE Forums



PHP:
create SocketChannel;
create Selector
associate the SocketChannel to the Selector
for(;;) {
  waiting events from the Selector;
  event arrived; create keys;
  for each key created by Selector {
    check the type of request;
    isAcceptable:
      get the client SocketChannel;
      associate that SocketChannel  to the Selector;
      record it for read/write operations
      continue;
   isReadable:
      get the client SocketChannel;
      read from the socket;
      continue;
   isWriteable:
      get the client SocketChannel;
      write on the socket;
      continue;
  } 
}

Reading/Writing to your client ( btw, these are just codes i'm currently devoloping on, there not confirmed yet )

PHP:
InputStream in = conn.getInputStream();
InputStreamReader rdr = new InputStreamReader(in);
LineNumberReader lnr = new LineNumberReader(rdr);
Request req = new Request();
while (!req.isComplete() )
{
   String s = lnr.readLine();
   req.addLine(s);
}

Using a accept() instead of request function saves time on login.


PHP:
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia, port );
serverChannel.socket().bind(isa);

All tcp clients listen correctly

PHP:
SocketChannel socket;
if (key.isAcceptable()) {
    System.out.println("Acceptable Key");
    ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
    socket = (SocketChannel) ssc.accept();
    socket.configureBlocking(false);
    SelectionKey another = 
      socket.register(sel,SelectionKey.OP_READ|SelectionKey.OP_WRITE);
}
if (key.isReadable()) {
    System.out.println("Readable Key");
    String ret = readMessage(key);
    if (ret.length() > 0) {
      writeMessage(socket,ret);
    }
		    
}
if (key.isWritable()) {
    System.out.println("Writable Key");
    String ret = readMessage(key);
    socket = (SocketChannel)key.channel();   
    if (result.length() > 0 ) {
      writeMessage(socket,ret);
    }
    }

Listening to the GSC keys and update keys via client class


PHP:
SelectionKey acceptKey = serverChannel.register(sel, SelectionKey.OP_ACCEPT);

   while (acceptKey.selector().select() > 0 ){

Allows clients registering via a stamp table.



I Probably won't be releasing this to public, instead i will release to friends.

Mostly because when i release things people don't learn from it.. they just use it without knowledge.
 
Re: Converting To A Custom Socket?

um... not really sure why your playing around with the sockets, to me they are quiete useless, it doesnt quiet matter if it takes 2 seconds or 4 seconds to login.

but btw correct me if im wrong but isnt this 503
PHP:
IntBuffer buffer = IntBuffer.allocate(10);
  for (int i=0; i < buffer.capacity(); i++) {
  buffer.put(i);
}

and this 508?
PHP:
buffer.position(0);
while (buffer.hasRemaining()) {
  int i = buffer.get();
  System.out.println("i="+i);
}

looks to me like 503 runs much better than 508...
("i="+i); <------ ????

it should be... *untested*
PHP:
System.out.println("i"=i++);
 
Re: Converting To A Custom Socket?

Doesn't matter what the state syntax is, but thanks

I'll fix it up when i have some time.

And for the fact, im a speed freak :P I like things fast, even if its a couple seconds faster

seems that all of you toronto pepole are... people from bc like things sloow lol...
 
Re: Converting To A Custom Socket?

Na,

It's mainly because i don't have much time on my hands, when i do, i try to use it up :P

Never on weekdays, usually at school ( detention for something stupid ), then i'm probably shooting some b's with my boys then im home eating food watching tv then i fall asleep out ot randomness and finally i get bored and go online to see ragezone
 
Re: Converting To A Custom Socket?

Sounds good, AJ. Now I would have optimized similar, however my optimization of the clientManager involves separating it completely from the server. The best way to reduce stress from the server is to have an individual client for world, and a client for managing the users properly. Good part of having an individual loginserver is that you can have 1 client manager and have it direct clients to multiple server paths
 
Re: Converting To A Custom Socket?

i still say there is no point but oh well :P that crazy frenchy wants things faster lol
 
Back