Android實現TCP客戶端支援讀寫操作
阿新 • • 發佈:2020-02-15
本篇我們便來學習如何通過socket讀寫TCP.
需要注意的是socket必須寫在子執行緒中,不能在ui主執行緒中直接使用,所以我們這裡建立了兩個class:
MainActivity(主介面)、TcpThread(獲取socket接收的資料)
由於程式碼有註釋了,所以就不解釋了.
1.gif效果如下
2.activity_main.xml如下所示:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:hint="請填入要傳送的內容" /> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/et_text" android:text="傳送" /> <TextView android:id="@+id/tv_recv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_send" android:minLines="20" android:hint="接收的內容" /> </RelativeLayout>
3.MainActivity.java如下所示
package com.example.tcpdemo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TcpThread mt; TextView tv_recv; EditText et_text; //要傳送的內容 Button btn_send; //定義一個handler public Handler mHandler = new Handler() { public void handleMessage(Message msg) { //列印伺服器端發來的訊息 System.out.println("read:"+msg.obj.toString()); tv_recv.append(msg.obj.toString()+"\r\n"); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_recv = (TextView)findViewById(R.id.tv_recv); et_text = (EditText)findViewById(R.id.et_text); mt = new TcpThread(); mt.setHandler(mHandler); //設定handler mt.setIp("10.10.10.104"); //設定伺服器地址 mt.start(); //啟動執行緒 btn_send = (Button)findViewById(R.id.btn_send); btn_send.setOnClickListener(new OnClickListener() { //向伺服器端傳送資料 public void onClick(View v) { if(!mt.write(et_text.getText().toString())) { Toast.makeText(getApplicationContext(),"傳送失敗",Toast.LENGTH_SHORT).show(); } } }); } }
4.TcpThread.java如下所示
package com.example.tcpdemo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import android.os.Handler; import android.os.Message; public class TcpThread extends Thread { Handler mHandler=null; Socket socket = null; String ip = null; OutputStream outputStream = null; //輸出流 InputStream inputStream=null; //接收流 //獲取另一個執行緒的Handler public void setHandler( Handler handler){ mHandler = handler; } //設定伺服器IP public void setIp(String ip){ this.ip = ip; } public void run(){ try { socket = new Socket(ip,8080); //訪問指定的ip地址:8080 } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //獲取輸出流 try { outputStream = socket.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ while (true) //讀取伺服器端傳送來的資料 { final byte[] buffer = new byte[1024];//建立接收緩衝區 inputStream = socket.getInputStream(); final int len = inputStream.read(buffer);//資料讀出來,並且返回資料的長度 if(len>0) { Message msg = mHandler.obtainMessage(); //設定傳送的內容 msg.obj = new String(buffer,len); mHandler.sendMessage(msg); } } } catch (IOException e) { } } //向伺服器端寫入資料 public boolean write(String text){ boolean ret = true; try { outputStream.write(text.toString().getBytes()); } catch (IOException e) { ret = false; e.printStackTrace(); } return ret; } }
總結
以上所述是小編給大家介紹的Android實現TCP客戶端支援讀寫操作,希望對大家有所幫助!