UDP安卓服務器端接收和顯示(含代碼demo,16進制接收顯示)

UDP基本知識

UDP屬於傳輸層協議,屬於TCP/IP協議中的一部分,是一種無連接的傳輸協議,UDP是不具有可靠性的數據報協議,因此可以根據需要設定重傳機制,適合高速傳輸和實時性有較高要求的網絡。UDP包頭佔8個字節,每2個字節組成一個域,由四個域組成,分別是存儲着源端口號、目的端口號、數據包長度和檢驗值。
作用:將數據流量變成一中包格式轉發出去。
缺點:包不是按序到達,該協議不會對包進行排序,分組和統計,如果需要這些功能可以在應用層再對包進行處理。
優點:時延小,速度快。適合需要高速數據傳輸的場景。

在java中UDP編程(socket)

在使用TCP/UDP時,會廣泛使用到套接字(socket)的API,應用程序利用套接字,可以設置對端的IP地址,端口號,實現數據的發送與接收。
JAVA中實現UDP通信是由兩個類完成的:

  1. DatagramPacket:將數據字節填充到UDP包中,得到數據報。
  2. DatagramSocket:用來收發UDP數據包。
    在這裏插入圖片描述

UDP服務器端(server)

接收客服端(client)發送的信息,只需設置開啓某個端口接收信息即可。
服務器端使用DatagramPacket類構造方法:

public DatagramPacket(byte[] buf, int length) { 
        throw new RuntimeException("Stub!");
    }

傳入參數:緩存數組(buffer[ ]),緩存數組長度( buffer[].length() )。收到的數據報將直接放入buffer[]中。

服務器端使用DatagramSocket類構造方法:

public DatagramSocket(SocketAddress bindaddr) throws SocketException {
        throw new RuntimeException("Stub!");
    }

傳入參數:SocketAddress類,這個類構造方法如下

public InetSocketAddress(int port) {
        throw new RuntimeException("Stub!");
    }

所以在這裏只需要輸入服務器端口號(port)即可,服務器端對IP地址沒有要求,只需要把這個端口打開就可以接收到其他客戶端發送的數據。
一個最簡單的UDP服務器基本上使用上述類就可以實現,一般會有的程序會加上對UDP生命線程因子和設置超時等操作,這裏就先不做討論,下面先打開AndroidStudio新建一個工程,然後開始完成這個demo。

AndroidStudio工程

頁面佈局(activity_main.xml)代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="8"
    tools:context="com.example.hh.udpserverdemo.MainActivity">

    <EditText
        android:id="@+id/portserver"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/openServer"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="開啓服務器"/>
        <Button
            android:id="@+id/closeServer"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="match_parent"
            android:text="關閉服務器"/>
        <Button
            android:id="@+id/cleantxt"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="match_parent"
            android:text="清空接收區"
            />
        <Button
            android:id="@+id/tohex"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="Hex"/>

        <Button
            android:id="@+id/tostr"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:text="字符串"/>

    </LinearLayout>

    <EditText
        android:id="@+id/receive"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:gravity="top"
        android:hint="接收區"
        android:background="#0EDBD6"/>

</LinearLayout>

頁面佈局預覽圖:

在這裏插入圖片描述
MainActivity.java代碼:

package com.example.hh.udpserverdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class MainActivity extends AppCompatActivity {
    /*UDP相關配置和所使用類對象創建*/
    private int port = 0; //服務器接收端口
    private InetSocketAddress inetSocketAddress = null;
    private DatagramPacket dpReceive = null;
    private DatagramSocket dsReceive = null;
    private byte[] msgReceive = new byte[1024]; //接收緩衝區大小
    private boolean hexString = false;
    private boolean udpServer = false;

    /*UI界面相關控件對象創建*/
    private EditText editport;
    private Button openserver,closeserver,hex,strshow,cleanrec;
    private EditText receive;

    /*其他使用類的創建*/
    private ButtonClick buttonClick = new ButtonClick(); //按鍵事件處理
    private MyHandle myHandle = new MyHandle(this);



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editport = (EditText)findViewById(R.id.portserver); //開始時佈局文件id=port,程序報錯。更改
        openserver = (Button)findViewById(R.id.openServer);
        closeserver = (Button)findViewById(R.id.closeServer);
        hex = (Button)findViewById(R.id.tohex);
        strshow = (Button)findViewById(R.id.tostr);
        cleanrec = (Button)findViewById(R.id.cleantxt);
        receive = (EditText)findViewById(R.id.receive);

        openserver.setOnClickListener(buttonClick);
        closeserver.setOnClickListener(buttonClick);
        cleanrec.setOnClickListener(buttonClick);
        hex.setOnClickListener(buttonClick);
    }



    private class ButtonClick implements Button.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.openServer:
                    udpServer = true;
                    port = Integer.parseInt(editport.getText().toString());
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            inetSocketAddress = new InetSocketAddress(port);
                            while (udpServer){
                                try {
                                    dsReceive = new DatagramSocket(inetSocketAddress);
                                    Log.i("服務器", "開啓服務器:");
                                }catch (SocketException e){
                                    e.printStackTrace();
                                }

                                dpReceive = new DatagramPacket(msgReceive,msgReceive.length);
                                try {
                                    dsReceive.receive(dpReceive);
                                    Log.i("服務器", "接收到數據包長:"+dpReceive.getLength());
                                    if (hexString){
                                        String rec = byte2hex(dpReceive.getData(),dpReceive.getLength());
                                        Message message = new Message();
                                        message.what = 1;
                                        message.obj = rec;
                                        myHandle.sendMessage(message);
                                    }else {
                                        String rec = new String(dpReceive.getData(),dpReceive.getOffset(),
                                                dpReceive.getLength());
                                        Message message = new Message();
                                        message.what = 1;
                                        message.obj = rec;
                                        myHandle.sendMessage(message);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            dsReceive.close();
                        }
                    });
                    thread.start();
                    break;

                case R.id.closeServer:
                    udpServer = false;
                    break;

                case R.id.cleantxt:
                    receive.setText("");
                    break;

                case R.id.tohex:
                    hexString = true;
                    break;

                case R.id.tostr:
                    hexString = false;
                    break;
            }
        }
    }

    private class MyHandle extends Handler{
        private final WeakReference<MainActivity> mActivity;
        public MyHandle(MainActivity activity){
            mActivity = new WeakReference<MainActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = mActivity.get();
            if (activity != null){
                switch (msg.what){
                    case 1:
                        String str  = msg.obj.toString();
                        receive.append(str);
                        Log.i("UI", "接收到信息並顯示(字符形式)");
                        break;
                }
            }
        }
    }

    /**
     * 字節數組轉換爲十六進制字符串
     *
     * @param b
     *            byte[] 需要轉換的字節數組
     * @return String 十六進制字符串
     */
    public static  String byte2hex(byte b[],int length) {
        if (b == null) {
            throw new IllegalArgumentException(
                    "Argument b ( byte array ) is null! ");
        }
        String hs = "";
        String stmp = "";
        for (int n = 0; n < length; n++) {
            stmp = Integer.toHexString(b[n] & 0xff);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
}

最後在AndroidManifest.xml中添加權限:

<uses-permission android:name="android.permission.INTERNET"/>

在這裏插入圖片描述
上述就是整個工程中需要的代碼。
可能存在的問題:
我的android studio工具有問題,所以創建的工程不能直接make build,這樣會報錯,直接在Build下

	Error:Execution failed for task ':app:preDebugAndroidTestBuild'.

Conflict with dependency ‘com.android.support:support-annotations’ in project ‘:app’. Resolved versions for app (26.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

選擇rebulid project即可,如果報同樣的錯誤這樣處理就可以了。
在這裏插入圖片描述

demo使用

在自己的設備上安裝這個APK:

  1. 在一行輸入打開的設備(服務器)端口號,序號儘量大點,因爲序號小的端口號可能被佔用。
  2. 點擊開啓服務器,默認顯示區域顯示的是“字符串”。
  3. 在PC端打開調試助手,設置服務器的地址和端口號(我選的廣播地址,端口號爲6001),輸入要發送的消息,點擊發送。
  4. 服務器接收到消息並顯示在接收區(藍色區域),默認把數據以字符串的形式顯示,發送端選擇以16進制發送時,想要正確顯示Hex字符的話,點擊HEX按鈕即可,如果後面又想轉化成字符串就點擊字符串按鈕。
    在這裏插入圖片描述
    PC端調試助手設置:
    在這裏插入圖片描述

demo演示

PC客戶端:
在這裏插入圖片描述

安卓服務器端:
在這裏插入圖片描述
最後附上整個項目和PC調試工具的下載地址:百度網盤鏈接,提取碼:6suk

寫在最後:
本人水平有限,所以這個demo儘量往簡單的在做,也是一個學習的過程,如果大家在使用時如果出現問題歡迎指出,一起學習,一起進步,謝謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章