1. 程式人生 > >Android之Socket通訊

Android之Socket通訊

Android裡面使用Socket與伺服器之間進行通訊:

首先建立一個SocThread類


 
package com.junto.sockettest;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by WangJinyong on 2018/6/30.
 */

public class SocThread extends Thread {

    private String ip = "114.116.64.184";
    private int port = 9667;
    private String TAG = "socket thread";
    private int timeout = 10000;

    public Socket client = null;
    PrintWriter out;
    BufferedReader in;
    public boolean isRun = true;
    Handler inHandler;
    Handler outHandler;
    Context ctx;
    private String TAG1 = "===Send===";
    SharedPreferences sp;

    public SocThread(Handler handlerin, Handler handlerout, Context context) {
        inHandler = handlerin;
        outHandler = handlerout;
        ctx = context;
        Log.i(TAG, "建立執行緒socket");
    }

    /**
     * 連線socket伺服器
     */
    public void conn() {

        try {
            initdate();
            Log.i(TAG, "連線中……");
            client = new Socket(ip, port);
            client.setSoTimeout(timeout);// 設定阻塞時間
            Log.i(TAG, "連線成功");
            in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    client.getOutputStream())), true);
            Log.i(TAG, "輸入輸出流獲取成功");
        } catch (UnknownHostException e) {
            Log.i(TAG, "連線錯誤UnknownHostException 重新獲取");
            e.printStackTrace();
            conn();
        } catch (IOException e) {
            Log.i(TAG, "連線伺服器io錯誤");
            e.printStackTrace();
        } catch (Exception e) {
            Log.i(TAG, "連線伺服器錯誤Exception" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void initdate() {
        sp = ctx.getSharedPreferences("SP", ctx.MODE_PRIVATE);
        ip = sp.getString("ipstr", ip);
        port = Integer.parseInt(sp.getString("port", String.valueOf(port)));
        Log.i(TAG, "獲取到ip埠:" + ip + ";" + port);
    }

    /**
     * 實時接受資料
     */
    @Override
    public void run() {
        Log.i(TAG, "執行緒socket開始執行");
        conn();
        Log.i(TAG, "1.run開始");
        String line = "";
        while (isRun) {
            try {
                if (client != null) {
                    Log.i(TAG, "2.檢測資料");
                    while ((line = in.readLine()) != null) {
                        Log.i(TAG, "3.getdata" + line + " len=" + line.length());
                        Log.i(TAG, "4.start set Message");
                        Message msg = inHandler.obtainMessage();
                        msg.obj = line;
                        inHandler.sendMessage(msg);// 結果返回給UI處理
                        Log.i(TAG1, "5.send to handler");
                    }

                } else {
                    Log.i(TAG, "沒有可用連線");
                    conn();
                }
            } catch (Exception e) {
                Log.i(TAG, "資料接收錯誤" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 傳送資料
     *
     * @param mess
     */
    public void Send(String mess) {
        try {
            if (client != null) {
                Log.i(TAG1, "傳送" + mess + "至"
                        + client.getInetAddress().getHostAddress() + ":"
                        + String.valueOf(client.getPort()));
                out.println(mess);
                out.flush();
                Log.i(TAG1, "傳送成功");
                Message msg = outHandler.obtainMessage();
                msg.obj = mess;
                msg.what = 1;
                outHandler.sendMessage(msg);// 結果返回給UI處理
            } else {
                Log.i(TAG, "client 不存在");
                Message msg = outHandler.obtainMessage();
                msg.obj = mess;
                msg.what = 0;
                outHandler.sendMessage(msg);// 結果返回給UI處理
                Log.i(TAG, "連線不存在重新連線");
                conn();
            }

        } catch (Exception e) {
            Log.i(TAG1, "send error");
            e.printStackTrace();
        } finally {
            Log.i(TAG1, "傳送完畢");

        }
    }

    /**
     * 關閉連線
     */
    public void close() {
        try {
            if (client != null) {
                Log.i(TAG, "close in");
                in.close();
                Log.i(TAG, "close out");
                out.close();
                Log.i(TAG, "close client");
                client.close();
            }
        } catch (Exception e) {
            Log.i(TAG, "close err");
            e.printStackTrace();
        }

    }
}

 

通訊的Activity類Client

package com.junto.sockettest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by WangJinyong on 2018/6/30.
 */

public class Client extends Activity {

    private String TAG = "===Client===";
    private TextView tv1 = null;
    Handler mhandler;
    Handler mhandlerSend;
    EditText edtsendms;
    Button btnsend;
    private String sendstr = "";
    private Context ctx;
    SocThread socketThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);
        tv1 = (TextView) findViewById(R.id.tv1);
        btnsend = (Button) findViewById(R.id.button1);
        ctx = Client.this;
        edtsendms = (EditText) findViewById(R.id.editText1);
        mhandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    Log.i(TAG, "mhandler接收到msg=" + msg.what);
                    if (msg.obj != null) {
//                        byte[] objStr2 = (byte[]) msg.obj;
//                        Log.e("tag","objStr2="+objStr2);
                        String s = msg.obj.toString();
                        if (s.trim().length() > 0) {
                            Log.i(TAG, "mhandler接收到obj=" + s);
                            Log.i(TAG, "開始更新UI");
                            tv1.append("Server:" + s);
                            Log.i(TAG, "更新UI完畢");
                        } else {
                            Log.i(TAG, "沒有資料返回不更新");
                        }
                    }
                } catch (Exception ee) {
                    Log.i(TAG, "載入過程出現異常");
                    ee.printStackTrace();
                }
            }
        };
        mhandlerSend = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    Log.i(TAG, "mhandlerSend接收到msg.what=" + msg.what);
                    String s = msg.obj.toString();
                    if (msg.what == 1) {
                        tv1.append("\n ME: " + s + "      傳送成功");
                    } else {
                        tv1.append("\n ME: " + s + "     傳送失敗");
                    }
                } catch (Exception ee) {
                    Log.i(TAG, "載入過程出現異常");
                    ee.printStackTrace();
                }
            }
        };
        startSocket();
        btnsend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 傳送資料
                Log.i(TAG, "準備傳送資料");
                sendstr = edtsendms.getText().toString().trim();
                socketThread.Send(sendstr);

            }
        });
    }

    public void startSocket() {
        socketThread = new SocThread(mhandler, mhandlerSend, ctx);
        socketThread.start();
    }

    private void stopSocket() {
        socketThread.isRun = false;
        socketThread.close();
        socketThread = null;
        Log.i(TAG, "Socket已終止");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "start onStart~~~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "start onRestart~~~");
        startSocket();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "start onResume~~~");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "start onPause~~~");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "start onStop~~~");
        stopSocket();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "start onDestroy~~~");

    }

}
client的XML佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="接收到的資訊" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_weight="0.25"
        android:text="" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="請輸入傳送內容" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10">

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="傳送" />

    </LinearLayout>

</LinearLayout>
在AndroidManifest.xml新增網路許可權:
<uses-permission android:name="android.permission.INTERNET"/>