android 用UDP做的心跳连接 小示例

 

推荐几个不错的帖子: 

基于Android平台的城市天气预报源码

http://www.eoeandroid.com/thread-193468-1-1.html

Android 用opengl 篮球游戏源码

http://www.eoeandroid.com/thread-192486-1-1.html

 

基于android北京地铁小助手有导航功能
http://www.eoeandroid.com/thread-193758-1-1.html

 

设备:客户端,服务端
机制:UDP
心跳思路:
1.自检,检测自身是否连接网络
2.检测对方是否发来信息,自身是否接收到

 

public class ServerHeartService extends Service {

    private WifiManager wifiManager=null;  //用来检测自身网络是否连接

    private boolean isConnected;

    private static int errorNum = 0; //用于一次连接不成功后记录,并且,记录到4次后再对程序进行提示

 

    private static final String TAG = "ServerHeartService";

    private String getMsg = "";

 

    private int _getPort = 7894;

    private int _sendPort = 5489;

    private String _partnerIP;

    private static  boolean   ServerFlag = false;

    private int num = 1;

    private Thread thread;

    private DatagramSocket socket;

    private boolean passFlag = false;

 

    private int length = 1;

 

    @Override

    public IBinder onBind(Intent intent) {

        return null;

    }

 

    @Override

    public void onCreate() {

        Log.v(TAG, "In Server onCreate");

    }

 

    @Override

    public void onDestroy() {

        System.out.println("DestoryJoinnerUdp");

        thread.stop();

        super.onDestroy();

    }

 

    @Override

    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);

        thread = new Thread(new Runnable() {

        @Override

          public void run() {

                while(true){

                    try {

                       //1.自测wifi是否正常

                        isConnected = isWifiConnect();

                        if(isConnected == false){

                            if(length == 1){

                                //不能直接在Service上通知,必须经过Handler去提示

                                Message heartMessage = ServerTestingActivity.heartHandler.obtainMessage();

                                heartMessage.arg1 = 1;

                                ServerTestingActivity .heartHandler.sendMessage(heartMessage);

                                //因为是多线程,防止多次发送Handler

                                length ++;

                            }

                            break;

                        }

 

                        SendMsg();

                        GetMsg();

                        //用于调试,可删除

                        String msg = "";

                        if( ServerFlag == false){

                            msg = "false";

                        }else{

                            msg = "true";

                        }

                        Log.d("ServerFlag", msg);

                        //记录未接收到客户端发来的消息 +1

                        if( ServerFlag == false){

                            errorNum ++;

                        }else{

                            errorNum = 0;

                        }

 

                        if(errorNum == 4){

                            ServerHeartMsg("客户端网络故障!");

                            break;

                        }

                    }catch(Exception e){

                        e.printStackTrace();

                    }

                }

            }

        });

        thread.start();

    }

    private boolean isWifiConnect(){

        try{

            ConnectivityManager conManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

            NetworkInfo mWifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            return mWifi.isConnected();

        }catch(Exception e){

            return false;

        }

}

 

// 将控制信息已广播的形式发送到Activity.

    private void ServerHeartMsg(String str) {

        final Intent intent = new Intent();

        intent.setAction(Constants.Controller.UDP_BORADCAST);

        intent.putExtra(UDPData.UDP_HEART_ERROR, str);

        sendBroadcast(intent);

    }

 

    public void GetMsg() throws IOException, InterruptedException{

        try {

            socket = new DatagramSocket(_getPort);

            Log.d(TAG, "Server连接到端口");

            byte data[] = new byte[256];

            DatagramPacket packet = new DatagramPacket(data, data.length);

        if(passFlag == false){

            socket.setSoTimeout(100000);

        }else{

            socket.setSoTimeout(3000);

        }

        socket.receive(packet);

        controlFlag = true;

        passFlag = true;

        Log.d(TAG, "Server接收到信息");

        getMsg = new String(packet.getData(), packet.getOffset(), packet.getLength());

        socket.close();

    } catch (SocketException e) {

        Log.d(TAG, "Server未找到服务器");

        socket.close();

        controlFlag = false;

        e.printStackTrace();

    } catch (UnknownHostException e) {

        Log.d(TAG, "Server未连接到服务器");

        socket.close();

        controlFlag = false;

        e.printStackTrace();

    } catch (IOException e) {

        Log.d(TAG, "Server消息未接收成功");

        socket.close();

        controlFlag = false;

        e.printStackTrace();

    }

}

 

    public void SendMsg() throws IOException{

        try {

            Thread.sleep(1000);

            socket = new DatagramSocket();

            InetAddress serverAddress = InetAddress.getByName(客户端IP);

            String str = "服务端网络故障!";

            byte data[] = str.getBytes();

            DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, _sendPort);

            socket.send(packet);

            Log.d(TAG, "Server消息发送成功");

            socket.close();

        } catch (SocketException e) {

            Log.d(TAG, "Server未找到服务器");

            socket.close();

            e.printStackTrace();

        } catch (UnknownHostException e) {

            Log.d(TAG, "Server未连接到服务器");

            socket.close();

            e.printStackTrace();

        } catch (IOException e) {

            Log.d(TAG, "Server消息未发送成功");

            socket.close();

            e.printStackTrace();

        } catch (InterruptedException e) {

            Log.d(TAG, "Sleep线程");

            socket.close();

            e.printStackTrace();

        }

    }

}

 

 

Server端接收广播: -----> ServerTestActivity

 

//定义心跳广播

    private BroadcastReceiver udpHeartReceiver = new BroadcastReceiver() {

        @Override

         public void onReceive(Context context, Intent intent) {

                //因为只有连接失败才会发送广播,所以 getStringExtra 一定有值,所以不需要去 getStringExtra == null去做判断

                String udpHeart = intent.getStringExtra(UDPData.UDP_HEART_ERROR);

                //msgHeart = udpHeart;

                Toast.makeText(getApplicationContext(), udpHeart,Toast.LENGTH_SHORT).show();

            }

    };

 

 

 

 

//注册心跳

    IntentFilter filter2 = new IntentFilter();

    filter2.addAction(Constants.Controller.UDP_BORADCAST);

    registerReceiver(udpHeartReceiver, filter2);

 

    //启动心跳

    intentService = new Intent();

    intentService.setClass(this, ControllerHeartService.class);

    startService(intentService);

 

    //停止心跳

    stopService(intentService);

 

 

 

客户端同理,可自已写下,在这里我就不贴代码了

 

你可能感兴趣的:(android)