Code Monkey home page Code Monkey logo

socketchannel's Introduction

A Java NIO Connection Library

Author: CubeSky

An easy used Java NIO Library. Use callback automaticlly notify user to data arrived.

Install

You can use Cube Repo at https://cubesky-mvn.github.io/

API

TCP

First, you need a listener for TCPServer or TCPClient

TCPSocket.SCTCPCallback scTCPCallback = new TCPSocket.SCTCPCallback(){
    void onDataArrived(long id, byte[] obj) {
        //Calling when data arrived.
        //When this is a Server Callback, id is a connection identifier.
        //When this is a Client Callback, id always 0.
        //obj is data byte array.
    }
    void onManagedConnectState(long id, ConnectState connectState) {
        //Calling when a managed connection state changed.
        //When this is a Server Callback, id is a connection identifier.
        //When this is a Client Callback, id always 0.
        //connectState is an Enum.
    }
    void onUnmangedCreated(long id, String tag, Socket socket){
        //Calling when an unmanaged socket is created.
        //When this is a Server Callback, id is a connection identifier.
        //When this is a Client Callback, id always 0.
        //tag is the tag when create this socket
        //socket is your raw Socket instance
    }
    void onHeartbeat(long id) {
        //Calling when heartbeat arrived.
        //When this is a Server Callback, id is a connection identifier.
        //When this is a Client Callback, id always 0.
    }
}

Server

Now, you can create a TCPServer in two lines.

TCPServer tcpServer = new TCPServer(new InetSocketAddress("0.0.0.0", 20000), scTCPCallback); //20000 is your TCPServer Port
tcpServer.start();
Fully Management Mode

This library will add a byte as command code before your data, if you don't want to use createUnmanagedSocket and Auto Heartbeat feature, you can use Manually Mode to disable this feature. If you do this, createUnmanagedSocket will immediately return without do anything. You need create TCPClient as Manually Mode too.

TCPServer tcpServer = new TCPServer(new InetSocketAddress("0.0.0.0", 20000), scTCPCallback, true);

You can send data by using sendMessage(long,byte[])

tcpServer.sendMessage(15L, "Hello Client".getBytes()); //15L is client id

You can also send Boardcast to all connected client by using sendBoardcast(byte[])

tcpServer.sendBoardcast("Hello All Client".getBytes());

If you want to know if two id is the same host, you can simplify call isSameHost(long, long)

tcpServer.isSameHost(15L, 20L); //15L and 20L is two client id, return a boolean

If you want to create a raw Socket, you can use createUnmanagedSocket(long) or createUnmanagedSocket(long, String)

tcpServer.createUnmanagedSocket(15L); //Tell id 15 client to connect a new Socket without tag
tcpServer.createUnmanagedSocket(15L, "Tag"); //Tell id 15 client to connect a new Socket with tag

And you can stop is by calling

tcpServer.stop();

or

tcpServer.close();

If you want to filter some connection during new connection accept. You can use setConnAuthCallback

tcpServer.setConnAuthCallback(new TCPSocket.SCAuthCallback(){
    boolean onNewSocketAuth(Socket socket) {
      //Your code
      return true; //true to Accept, false to Deny
    }
})

Becareful: This callback is sync, so it will block the main loop, use it carefully or you will get a poor performance.

you can also use try..resource statement

try(TCPServer tcpServer = new TCPServer(new InetSocketAddress("0.0.0.0", 20000), scTCPCallback)){ //20000 is your TCPServer Port
    tcpServer.start();
} //TCPServer will automatic close

Client

Client is more easy.

TCPClient tcpClient = new TCPClient(new InetSocketAddress("127.0.0.1", 20000), scTCPCallback); //127.0.0.1 is your server ip and 20000 is your server port
tcpClient.start();
Fully Management Mode

This library will add a byte as command code before your data, if you don't want to use createUnmanagedSocket and Auto Heartbeat feature, you can use this to disable this feature. If you do this, createUnmanagedSocket will immediately return without do anything. You need create TCPSocket as Manually Mode too.

TCPClient tcpClient = new TCPClient(new InetSocketAddress("127.0.0.1", 20000), scTCPCallback, true);

Send data to your server

tcpClient.sendMessage("Hello Server".getBytes());

If you want to create a raw Socket, you can use createUnmanagedSocket() or createUnmanagedSocket(String)

tcpClient.createUnmanagedSocket(); //Tell server to create a new raw Socket without tag
tcpClient.createUnmanagedSocket("Tag"); //Tell server to create a new raw Socket with tag

And you can stop is by calling

tcpClient.stop();

or

tcpClient.close();

you can also use try..resource statement

try(TCPClient tcpClient = new TCPClient(new InetSocketAddress("127.0.0.1", 20000), scTCPCallback)){ //127.0.0.1 is your server ip and 20000 is your server port
    tcpClient.start();
} //TCPClient will automatic close

ConnectState Enum

  • CONNECT: when a connection is created and ready to use
  • DISCONNECT: when a connection is broken.
  • CLOSED: TCPServer or TCPClient is closed, it can not used for new connection, connection which is connected will lose.

UDP

UDP is easy too.

SCUDPCallback scUDPCallback = new SCUDPCallback() {
  void onDataArrived(String ip, int port, byte[] obj) {
    //Calling when data arrived
  }
}
UDPServer udpServer = new UDPServer(20000, scUDPCallback); //UDP Server will listen on port 20000
udpServer.start();

Send your data by calling sendMessage(string, int, byte[])

udpServer.sendMessage("127.0.0.1", 20000, "Hello Peer".getBytes());

Close it

udpServer.stop();

or

udpServer.close();

You can also use try...resource statement

try (UDPServer udpServer = new UDPServer(20000, scUDPCallback)) {
    udpServer.start();
} //UDPSocket will automatic close

Shortcut

Server and Client

StringTCPClient StringTCPServer StringUDPServer is comming after SocketChannel 7.0.
They will automatically wrap byte array to String UTF-8.
FileTCPClient FileTCPServer is comming after SocketChannel 8.0.
They provide method to send file directly.
FileStringTCPClient FileStringTCPServer is comming after SocketChannel 8.0.
They provide method to send file directly and extend from StringTCPClient and StringTCPServer.

Callback

DefaultTCPCallback and DefaultUDPCallback is an empty SCTCPCallback and SCUDPCallback implementation. You can extends and override these to decrease your code.
EchoTCPCallback and EchoUDPCallback will offer an echo server handler automatically.
StringTCPCallback and StringUDPCallback will automatically transform byte array to String UTF-8.
FileDefaultTCPCallback and FileStringTCPCallback extends DefaultTCPCallback and StringTCPCallback with file transport support.

TransportHelper

Transport Helper will help to transport non-standard data.
Now, you can use FileTransportHelper to transport files between socket channel instance.

All Transport Helper can not run with Manually Mode instance!

Heartbeat

If you use fully management mode, SocketChannel library will automatically send and reply heartbeat packet every 2 minutes using command code 2.
In Manually Mode, this feature will be disabled.

Dependency

Name Description Package Name
Guava Google Core Libraries for Java com.google.guava

License

This library is under GPLv3.

socketchannel's People

Contributors

cubesky avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.