1. 程式人生 > >android WIFI Socket 檔案傳輸——問題已經解決了,發帖只因高興--嘿嘿--並不徹底歡迎點評

android WIFI Socket 檔案傳輸——問題已經解決了,發帖只因高興--嘿嘿--並不徹底歡迎點評

</pre><pre name="code" class="java">我的這個Socket是想保持一個長連線,檔案可以迴圈傳輸
但是我的問題在於,不能抓住檔案傳輸完成的這個時機,導致異常

我的檔案傳送程式碼
[code=java]
public void sendFile(String filePath)
    {
        try
        {
            File file = new File(filePath);

            //傳送檔案
            this.fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[this.bufferSize];
            int total = 0;
            _Log.e(_Log.msg() + "我們正在傳送檔案先生,");
            while (true)
            {
                int length = this.fileInputStream.read(buffer);
                this.outputStream.write(buffer);
                this.outputStream.flush();
                if (length == -1)
                {
                    _Log.e(_Log.msg() + "這就是當-1的時候我們讀檔案結束了");
                    break;
                }
                total += length;
            }
            _Log.e(_Log.msg() + "我們按傳輸所計算的檔案大小為:" + total + "bit");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        _Log.e(_Log.msg() + "檔案傳送結束我們應該等待客戶端的確認,然後關閉等待對話方塊");
    }
[/code]

我的檔案接收程式碼
[code=java]
  //region 正在接受檔案
  this.fileOutputStream = new FileOutputStream(this.receiveFilePath);
  boolean first = true;
  _Log.e(_Log.msg() + "檔案正在被接受");
  while (true)
  {
      if (!first)
      {
          _Log.e(_Log.msg() + "正在等待接收嚇一跳");
          total = this.inputStream.read(buffer);
          _Log.e(_Log.msg() + "接收到的陣列長度為+" + total);
          if (new File(this.receiveFilePath).length() >= this.receiveFileSize)
          {
              _Log.e(_Log.msg() + "讀取檔案時因為到末尾時返回-1而結束的 = " + this
                      .receiveFileSize);
              break;
          }
      }
      else
      {
          first = false;
      }
      //我們暫且認為這裡FileOutputStream(path)會自動建立這個不存在的路徑,如果
      this.fileOutputStream.write(buffer, 0, total);
  }
  this.fileOutputStream.close();
  _Log.e(_Log.msg() + "傳輸完成了");
  //endregion傳輸檔案完成
  this.receiveFileState = false;
  _Log.e(_Log.msg() + "我們在1秒鐘之後告知客戶我們已經接受檔案完成的這件事");
  Thread.sleep(1000);
  this.sendString(this.transferFileOver);//傳送檔案接收完成標誌
  _Log.e(_Log.msg() + "我們已經說完了,就是不知道其受到沒有");
[/code]

關鍵點:(我想在檔案傳輸完成後從這個點跳出但是總不能把握檔案什麼時候傳輸完成)
[code=java]
//這個沒有成功
if (new File(this.receiveFilePath).length() >= this.receiveFileSize)
          {
              _Log.e(_Log.msg() + "讀取檔案時因為到末尾時返回-1而結束的 = " + this
                      .receiveFileSize);
              break;
          }
//這個也沒有成功
this.receiveFileSize -= total;
if (this.receiveFileSize <= 0)
{
    _Log.e(_Log.msg() + "讀取檔案時因為到末尾時返回-1而結束的 = " + this
            .receiveFileSize);
    break;
}
//還試過讓傳送方Socket傳送完成檔案後傳送一個字串命令,
//我讓傳送檔案後,使其sleep(1000),在傳送命令,依然會和檔案資料 混在一起
[/code]

分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割分割


沒想到的是問題還沒有問,不過是寫了些思路問題就有了解,真棒
程式碼如下:
SocketTool.java
[code=java]
package a114.yangming.Work.M_Socket;

import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import a114.yangming.Util.Global;
import a114.yangming.Util.Tool_GetStorage;
import a114.yangming.Util._Log;
import a114.yangming.myapplication.R;

/**
 楊銘 Created by kys_8 on 16/6/11,0011. <p>Email:
[email protected]
</p> <p>Mobile phone:15133350726</p> <p/> Client 和 Server同時需呀的是 <p/> 收發文字——包括命令,和通訊語句 <p/> 收發檔案 */ public class SocketTool { //region 流 包括 簡單流 和 檔案流 private OutputStream outputStream; private InputStream inputStream; /** 設定簡單流 @param outputStream @param inputStream */ public void setSimpleStream(OutputStream outputStream, InputStream inputStream) { this.inputStream = inputStream; this.outputStream = outputStream; } private FileOutputStream fileOutputStream; private FileInputStream fileInputStream; //endregion //region 通訊語句 /** 客戶端向伺服器打招呼的短語 */ public final String hello = "信工學院"; /** 伺服器的標準回覆 */ public final String hi = "北方學院"; /** 成功得到了檔案描述 */ public final String getFileIntroduceOK = "getIntroduceOk"; /** 傳輸檔案完成 */ public final String transferFileOver = "transferFileOver"; /** 通過Json傳來的檔名字——Key */ public final String jsonFileName = "fileName"; /** 通過Json出來的檔案大小——Key */ public final String jsonFileSize = "fileSize"; /** 預設緩衝區大小 */ private final int bufferSize = 10240; //endregion //region 標識 /** 用來判斷當前是否正處於傳送檔案階段,true,正在傳送檔案,false,當前沒有傳送檔案 */ private boolean sendFileState = false; /** 用來標記是否應該接受檔案流了,如果是應該接受檔案流。這裡置為true ——如果為False說明應該傳輸檔名字 */ private boolean receiveFileState = false; //endregion //region 傳輸過程中需要的一些檔案介紹資訊——我們的程式,收發不能同時進行,所以這裡公用相同的變數是沒有問題的 /** 正在傳輸的檔案大小——接收過程 */ private long receiveFileSize = -1; /** 正在傳輸的檔案路徑——接收過程 */ private String receiveFilePath = null; public List<String> transFilePaths = null; //endregion /** 僅用於處理客戶發來的打招呼訊息——這是服務端需要用的 <p/> 如果給的暗號是正確的,我們就回復他對應的暗號,如果是錯誤的就和他斷開連線等待下一個使用者 @return true代表,可以建立通訊,false,代表應該斷開這個Socket等待下一個連線的到來 */ public boolean receiveHello() { try { byte[] buffer = new byte[20]; _Log.e(_Log.msg() + "我是服務端我正在等待客戶的打招呼資訊"); int total = this.inputStream.read(buffer); if (total == 12) { String string = this.getStrFormBytes(buffer, total); if (string.equals(this.hello)) { _Log.e(_Log.msg() + "客戶端說:" + string); Thread.sleep(1000);//睡一小會再發就能發了 _Log.e(_Log.msg() + "看看這句話和上一句是否相聚1000mm"); this.sendString(this.hi); return true; } else { return false; } } else { return false; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } _Log.e(_Log.msg() + "能執行到這一步本身就是異常"); return false; } public boolean receiveHi() { try { byte[] buffer = new byte[20]; _Log.e(_Log.msg() + "我是客戶端,我已經和服務端打過招呼了,等待他的迴應"); int total = this.inputStream.read(buffer); if (total == 12) { String string = this.getStrFormBytes(buffer, total); if (string.equals(this.hi)) { _Log.e(_Log.msg() + "服務端回覆正常——應該關閉介面進行檔案傳輸了"); this.initSendUI(); return true; } else { return false; } } else { return false; } } catch (IOException e) { e.printStackTrace(); } _Log.e(_Log.msg() + "能執行到這一步本身就是異常"); return false; } /** 說白了就是關閉當前頁面啟動初始頁面準備進行檔案傳輸 */ public void initSendUI() { if (Global.handlerLoading != null) { Message message = Message.obtain(); message.what = Global.CloseActivity; Global.handlerLoading.sendMessage(message); } else { _Log.e(_Log.msg() + Global.mainActivity.getResources().getString(R.string.myExecption)); } } /** 接受資訊的邏輯 ——程式碼執行到這裡說明已經打過招呼了,我們可以進行後續判斷了 */ public void receive() { //我們在這裡等待接收,我們會等到什麼呢?有可能是檔案描述 //如果我們已經發送了一個檔案描述,那麼我們回得到一個檔案描述已經處理完成的確認資訊 //有可能是打招呼資訊 //有可能是檔案 try { byte[] buffer = new byte[1024]; while (true) { _Log.e(_Log.msg() + "這裡是正在等待一些什麼"); int total = this.inputStream.read(buffer); if (total == -1)//說明客戶端主動斷開連線了 { _Log.e(_Log.msg() + "正在等待處理的異常"); } if (this.receiveFileState)//正在處於檔案接收狀態——就是說已經接受過檔案介紹會執行這裡的 { _Log.e(_Log.msg() + "進入到接受檔案的地方了"); //region 正在接受檔案 this.fileOutputStream = new FileOutputStream(this.receiveFilePath); boolean first = true; _Log.e(_Log.msg() + "檔案正在被接受"); while (true) { if (!first) { _Log.e(_Log.msg() + "正在等待接收嚇一跳"); total = this.inputStream.read(buffer); _Log.e(_Log.msg() + "接收到的陣列長度為+" + total); if (this.receiveFileSize <= 0) { String string = this.getStrFormBytes(buffer, total).trim(); if (string.equals(this.transferFileOver)) { break; } } else { this.receiveFileSize -= total; } } else { first = false; } //我們暫且認為這裡FileOutputStream(path)會自動建立這個不存在的路徑,如果 this.fileOutputStream.write(buffer, 0, total); } this.fileOutputStream.close(); _Log.e(_Log.msg() + "傳輸完成了"); //endregion傳輸檔案完成 this.receiveFileState = false; _Log.e(_Log.msg() + "我們在1秒鐘之後告知客戶我們已經接受檔案完成的這件事"); Thread.sleep(1000); this.sendString(this.transferFileOver);//傳送檔案接收完成標誌 _Log.e(_Log.msg() + "我們已經說完了,就是不知道其受到沒有"); } else//沒有接受過檔案介紹的會執行這裡 { if (this.sendFileState)//這是正處於檔案傳送狀態的 { _Log.e(_Log.msg() + "當前本機是傳送狀態,到達接受使用者機器,對接受本機檔案後的反饋 的地方了"); //region 傳送檔案階段客戶接受檔案介紹成功 String string = this.getStrFormBytes(buffer, total); if (string.equals(this.getFileIntroduceOK))//客戶端說接受檔案介紹成功了 { _Log.e(_Log.msg() + "對方接受檔案資訊成功了,可以傳送檔案了——已經寫好了看看結果怎麼樣吧"); this.sendFile(this.receiveFilePath); } else if (string.equals(this.transferFileOver)) { this.sendFileState = false; _Log.e(_Log.msg() + "我們已經得到了transferFileOver的反饋,從而設定了this" + ".sendFileState = false;"); } else { _Log.e(_Log.msg() + "對方傳送的暗號不太對,重新等待暗號"); } continue; //endregion } else//這種情況下接受檔案介紹資訊——這個階段沒有檢錯,不能出錯 { //region 正在接受檔案資訊 _Log.e(_Log.msg() + "這是正在等待檔案介紹的位置呢"); //region 得到檔案路徑和檔案大小 String jsonStr = this.getStrFormBytes(buffer, total); _Log.e(_Log.msg() + "執行到這裡了嗎?111111111111111莫名奇妙這裡怎麼會得到內容呢?+" + jsonStr); //得到檔名和檔案大小. this.getFileIntroduceFormJson(jsonStr); _Log.e(_Log.msg() + "執行到這裡了嗎?22222222222222222222222"); //endregion String testFilePath = this.receiveFilePath; //region 為了防止檔案重名,造成對檔案的覆蓋,我們要確保這個檔名在這個目錄下獨一無二 ——完成了這一步才算真正得到了檔案路徑 int i = 1; while (true) { if (new File(testFilePath).exists()) { String suffix = this.getFileSuffix(receiveFilePath);//得到字尾 if (suffix == null) { testFilePath = receiveFilePath + "(" + i + ")"; } else { testFilePath = receiveFilePath.substring(0, receiveFilePath .length() - suffix.length() - 1); testFilePath = testFilePath + "(" + i + ")." + suffix; } i++; } else { this.receiveFilePath = testFilePath; break; } } //endregion //檔名字處理完成——搞好標緻位 this.receiveFileState = true; //告知客戶我們得到了檔案介紹——也就是說可以傳送檔案了 this.sendString(this.getFileIntroduceOK); //endregion } } } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** 從JSONStr中得到對檔案的描述,包括檔名和檔案大小 <p/> 這個方法在適當的時候執行完成後,成員變數 fileSize 和 filePath 將得到各自的值 <p/> 但是每次用完後最好是能釋放掉 @param jsonStr 得到的Json */ private void getFileIntroduceFormJson(String jsonStr) { try { JSONTokener jsonParser = new JSONTokener(jsonStr); JSONObject connect = (JSONObject) jsonParser.nextValue(); this.receiveFileSize = connect.getLong(this.jsonFileSize); String fileName = connect.getString(this.jsonFileName); fileName = fileName == null ? "未命名" : fileName;//防止傳過來的檔名為空 //從XML檔案中得到檔案預設儲存路徑。如果使用者還沒有設定,就用它手機中首個儲存卡的根目錄——注意是在根目錄建立“速傳資料夾” String fileDic = PreferenceManager.getDefaultSharedPreferences(Global.mainActivity) .getString(Global.keySaveStorage, new Tool_GetStorage(Global.mainActivity) .getStorageLocationStr()[0]); this.receiveFilePath = fileDic + "/" + Global.folderName; //我們會首先建立這個資料夾 File file = new File(this.receiveFilePath); if (!file.exists() && !new File(receiveFilePath).mkdirs()) {//先判斷資料夾是否存在,如果存在就沒事 //如果不存在就會建立,如果建立成功那麼也沒事, //如果建立失敗了就會報錯的 _Log.e(_Log.msg() + Global.mainActivity.getResources() .getString(R.string.myExecption)); } this.receiveFilePath += "/" + fileName;//最終得到檔案路徑 } catch (JSONException e) { e.printStackTrace(); } } //region 傳送檔案的相關工作 /** 傳送檔案介紹 */ public void sendFileIntraduce(String filePath) { try { this.receiveFilePath = filePath; File file = new File(filePath); JSONObject jsonObject = new JSONObject(); jsonObject.put(this.jsonFileName, file.getName()); jsonObject.put(this.jsonFileSize, this.receiveFileSize = file.length()); this.sendString(jsonObject.toString()); _Log.e(_Log.msg() + "我們傳送出去的JSON為:" + jsonObject.toString()); this.sendFileState = true; } catch (JSONException e) { e.printStackTrace(); } } /** 真正的傳送檔案 */ public void sendFile(String filePath) { try { File file = new File(filePath); //傳送檔案 this.fileInputStream = new FileInputStream(file); byte[] buffer = new byte[this.bufferSize]; int total = 0; _Log.e(_Log.msg() + "我們正在傳送檔案先生,"); while (true) { int length = this.fileInputStream.read(buffer); this.outputStream.write(buffer); this.outputStream.flush(); if (length == -1) { _Log.e(_Log.msg() + "這就是當-1的時候我們讀檔案結束了"); break; } total += length; } _Log.e(_Log.msg() + "我們按傳輸所計算的檔案大小為:" + total + "bit"); Thread.sleep(2000); this.sendString(this.transferFileOver); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } _Log.e(_Log.msg() + "檔案傳送結束我們應該等待客戶端的確認,然後關閉等待對話方塊"); } //endregion //===========================來自publicMethod /** 把 byte 陣列中的有用的部分轉換為String也就是說如果 bytes 肯定是不滿的,而 total < bytes 長度 @param bytes 包含資料的byte陣列 @param total 所包含的資料佔的陣列的長度 @return 返回所轉換的字串 */ public String getStrFormBytes(byte[] bytes, int total) { byte[] helloFromClient = new byte[total]; System.arraycopy(bytes, 0, helloFromClient, 0, total); String string = new String(helloFromClient);//把這12位byte轉化為字串 return string; } //region 傳送字串 /** 這裡把一個字串轉換為byte陣列 @param msg 這裡要注意msg的預設編碼也就是我們的 .java 檔案的預設編碼格式。getByte() 如果我們不指定編碼方式,就會按預設編碼來得到陣列。 我們這裡指定用`UTF-8`來搞。 <p/> String長度為4 UTF_8 得到的陣列長度為 12——GBK 得到的陣列長度為 8 */ public void sendString(String msg) { try { byte[] bytes = msg.getBytes("UTF-8"); this.sendBytes(bytes); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** 專一發送Bytes陣列的 ,處理好的Byte */ private void sendBytes(byte[] bytes) { try { outputStream.write(bytes); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } } //endregion /** 得到檔案字尾名 @param fileName 引數為String—是FileName @return 返回字尾名 */ public final String getFileSuffix(String fileName) { if (fileName == null) { Log.e(_Log.TAG, _Log.msg() + "當前功能不接受null"); return null; } //要分個那就要考慮情況 //點在開始 得到 “”+點後的內容 //點在中間 得到 以點為分割點的陣列 //======================== //點在末尾 得到 得到除分隔符外的串,類似於沒有點,但又不太相同 //沒有點 得到 原來串 if (!fileName.contains(".")) {//如果不包含"."或者最後一個是"."——那麼肯定是未知型別 return null; } if ((fileName.lastIndexOf(".") == fileName.length() - 1)) { return ""; } else { String[] splitDot = fileName.split("\\.");//被“.”分割 String strSuffix = splitDot[splitDot.length - 1];//得到字尾 return strSuffix; } } } [/code] SocketServer.java [code=java] package a114.yangming.Work.M_Socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import a114.yangming.Util.Global; import a114.yangming.Util.OurIP; import a114.yangming.Util._Log; /** 楊銘 Created by kys_8 on 16/6/3,0003. <p>Email:
[email protected]
</p> <p>Mobile phone:15133350726</p> <p/> 服務端判斷客戶端時候斷線的標誌就是接收到的資訊是否為空,如果不為空,說明沒有斷開啊,如果為空說明斷線了,那就要做一些操作了 — Client也一樣的斷開就會接收到null <p/> 在這裡我們必須解決端口占用問題“java.net.BindException: bind failed: EADDRINUSE (Address already in use)”如果使用者短時間內用軟體重複開啟熱點就會出現端口占用問題。 */ public class SocketServer extends Thread { //region 自己的中斷標誌設定 /** 直接用官方提供的方法,沒能解決線上程睡眠時候進行中斷操作,所以自己定義一箇中斷標誌進行嘗試——沒想到成功了,雖然好了,但還是要因對中斷的不瞭解做出檢討 */ private boolean myInterrupt = false; public boolean isMyInterrupt() { return myInterrupt; } public void setMyInterrupt(boolean myInterrupt) { this.myInterrupt = myInterrupt; }//endregion //region 其餘標誌們 /** 這個表示是用來標記當前資料通道是否處於連線狀態 */ public boolean connect = false; //endregion //region 基礎成員 /** 預設埠號 */ private final int port = 9999; /** 預設客戶端連線數 */ private final int backlog = 1; /** 通訊socket */ private Socket socket; private ServerSocket serverSocket; private OutputStream outputStream; private InputStream inputStream; //endregion public SocketTool socketTool; public SocketServer() { this.socketTool = new SocketTool(); } @Override public void run() { _Log.e(_Log.msg() + "服務端啟動了"); //獲取IP和初始化必要資料 this.getIpCreateServer(); //接下來應該迴圈等待使用者接入 while (true) { if (this.connect)//判斷當前是否處於有客戶連線的狀態—true代表有使用者連線 { this.socketTool.receive(); } else//如果處於未連線狀態 { this.waitClient(); //等待客戶端打招呼 if (this.socketTool.receiveHello())//和客戶端連線成功 { _Log.e(_Log.msg() + "連線成功了***********************************"); this.socketTool.initSendUI(); //現在應該關閉當前頁,開啟初始介面,就是說可以是開始檔案傳輸了 this.connect = true;//設定好連線標誌 continue;//等待真正的資料傳來 } else//這個客戶端不是自己人,應該斷開 { _Log.e(_Log.msg() + "暗號異常連線失敗#############################"); this.connect = false; //斷開當前Socket try { this.socket.close(); } catch (IOException e) { _Log.e(_Log.msg() + "this.socket.close();%%%%%%%%%%%%%%%%%% catch"); e.printStackTrace(); } } } } } /** 獲取IP,建立服務端。這裡是一次性操作的 <p/> 它的結果是ServerSocket準備好了 */ private void getIpCreateServer() { try { //region 獲取IP InetAddress inetAddress;//獲取IP地址 OurIP ourIP = new OurIP(); while (!Global.wifiReceiver.isGetApIp() || (inetAddress = ourIP.getApIp()) == null) { if (this.isMyInterrupt()) { return; } this.sleep(1000); } //endregion _Log.e(_Log.msg() + "服務端的地址為:" + inetAddress); //建立服務端 this.serverSocket = new ServerSocket(this.port, this.backlog, inetAddress); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** 阻塞等待使用者接入 <p/> 簡單流在這裡初始化 */ private void waitClient() { try { _Log.e(_Log.msg() + "正在等待客戶端的接入···········"); this.socket = serverSocket.accept();//阻塞執行緒等待客戶端的接入 this.outputStream = this.socket.getOutputStream(); this.inputStream = this.socket.getInputStream(); this.socketTool.setSimpleStream(outputStream, inputStream); } catch (IOException e) { e.printStackTrace(); } } public void stopMySelf() { try { this.setMyInterrupt(true);//設定中斷 if (this.socket != null) { this.socket.close(); } if (this.serverSocket != null) { this.serverSocket.close(); } } catch (IOException e) { e.printStackTrace(); } _Log.e(_Log.msg() + "SocketServer結束了"); } } [/code] SocketClient.java [code=java] package a114.yangming.Work.M_Socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import a114.yangming.Util._Log; /** 楊銘 Created by kys_8 on 16/6/3,0003. <p>Email:
[email protected]
</p> <p>Mobile phone:15133350726</p> <p/> 使用這個類一定是要首先設定好啟動它的動作是什麼,然後在真正啟動的時候設定好DHCP然後才能安全啟動 */ public class SocketClient extends Thread { //region 自己的中斷標誌設定 /** 直接用官方提供的方法,沒能解決線上程睡眠時候進行中斷操作,所以自己定義一箇中斷標誌進行嘗試——沒想到成功了,雖然好了,但還是要因對中斷的不瞭解做出檢討 */ private boolean myInterrupt = false; public boolean isMyInterrupt() { return myInterrupt; } public void setMyInterrupt(boolean myInterrupt) { this.myInterrupt = myInterrupt; }//endregion //region 其餘標誌們 public enum Mark { /** 從FragmentRadar中通過點選啟動本執行緒 */ /** 從FragmnetScanCoce中通過掃描啟動執行緒 */ markClickScan, /** 秋其他啟動本執行緒的動作 */ markNon } private Mark mark; public boolean connect = false; //endregion private String dhcp; private int port = 9999; private Socket socket; private InputStream inputStream; private OutputStream outputStream; public SocketTool socketTool; public SocketClient() { this.socketTool = new SocketTool(); } /** 啟動執行緒 */ @Override public synchronized void start() { switch (mark) { case markClickScan: _Log.e(_Log.msg() + "客戶端啟動成功"); super.start(); break; case markNon: _Log.e(_Log.msg() + "標示異常,客戶端未正常啟動"); } } @Override public void run() { try { this.sleep(1000); _Log.e(_Log.msg() + "當前得到的ip為:" + dhcp); InetAddress inetAddress = InetAddress.getByName(dhcp); this.socket = new Socket(inetAddress, port); this.outputStream = this.socket.getOutputStream(); this.inputStream = this.socket.getInputStream(); this.socketTool.setSimpleStream(outputStream, inputStream); }//region 一堆Catch catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }//endregion this.socketTool.sendString(this.socketTool.hello); if (!this.socketTool.receiveHi())//如果接受到錯誤的資訊就結束本執行緒 { this.stopMySelf(); return; } else { this.connect = true; } this.socketTool.receive(); } /** 首先標記這個動作,在啟動的時候只有有效的動作,才會啟動本執行緒 @param mark 啟動本執行緒的動作是什麼 */ public void setMark(Mark mark) { this.mark = mark; } /** 設定DHCP @param dhcp */ public void setDHCP(String dhcp) { if (dhcp == null) { _Log.e(_Log.msg() + "當前DHCP是null"); } this.dhcp = dhcp; } public void stopMySelf() { try { if (this.socket != null) { this.socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } [/code]


相關推薦

no