Giter Club home page Giter Club logo

androidserver's Introduction

AndroidServer

  1. 基于Apache MINA 开源框架

  2. 实现Android OS 作为服务器、客户端,进行Socket通讯

  3. 结合HotWiFi项目,手机架设热点进行通讯

How to

  • Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
	repositories {
          ...
	  maven { url 'https://jitpack.io' }
	}
}
  • Step 2. Add the dependency
dependencies {
	 compile 'com.github.cuihp:AndroidServer:v1.0.0'
}
  • Step 3.初始化服务器端
          //建造者模式进行相关配置
          ServerConfig serverConfig = new ServerConfig.Builder().setPort(8888).build();
          MinaServer minaServer = new MinaServer(serverConfig);
          //服务器状态进行监听
          minaServer.setServerStateListener(new MinaServer.ServerStateListener() {
                 @Override
                 public void sessionCreated() {
                     Log.d(TAG, "server sessionCreated ");
                 }

                 @Override
                 public void sessionOpened() {
                     Log.d(TAG, "server sessionOpened ");
                 }

                 @Override
                 public void sessionClosed() {
                     Log.d(TAG, "server sessionClosed ");
                 }

                 @Override
                 public void messageReceived(String message) {
                     Log.d(TAG, "server messageReceived ");
                 }

                 @Override
                 public void messageSent(String message) {
                     Log.d(TAG, "server messageSent "+message);

                 }
             });
         }

  • Step 4.初始化客户端
     //建造者模式进行相关配置
     ClientConfig clientConfig = new ClientConfig.Builder().setIp("127.0.0.1").setPort(8888).build();
     MinaClient  minaClient = new MinaClient(clientConfig);
     //状态进行监听
     minaClient.setClientStateListener(new MinaClient.ClientStateListener() {
            @Override
            public void sessionCreated() {
                Log.d(TAG, "client sessionCreated ");
            }

            @Override
            public void sessionOpened() {
                Log.d(TAG, "client sessionOpened ");
            }

            @Override
            public void sessionClosed() {
                Log.d(TAG, "client sessionClosed ");
            }

            @Override
            public void messageReceived(String message) {
                Log.d(TAG, "client messageReceived "+message.toString());
            }

            @Override
            public void messageSent(String message) {
                Log.d(TAG, "client messageSent "+message);

            }
     });

  • Step 5.消息发送
 minaClient.sendMessage("hello server "+System.currentTimeMillis());

 minaServer.sendMessage("hello client "+System.currentTimeMillis());

androidserver's People

Contributors

cuihp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

androidserver's Issues

服务端重启出错

手机作为服务端,退出重启后报错:bind failed: EADDRINUSE (Address already in use),您知道该怎么解决吗?

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.