Android實(shí)現(xiàn)TCP客戶端支持讀寫(xiě)操作
本篇我們便來(lái)學(xué)習(xí)如何通過(guò)socket讀寫(xiě)TCP.
需要注意的是socket必須寫(xiě)在子線程中,不能在ui主線程中直接使用,所以我們這里創(chuàng)建了兩個(gè)class:
MainActivity(主界面)、TcpThread(獲取socket接收的數(shù)據(jù))
由于代碼有注釋了,所以就不解釋了.
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="請(qǐng)?zhí)钊胍l(fā)送的內(nèi)容" /> <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="發(fā)送" /> <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="接收的內(nèi)容" /> </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; //要發(fā)送的內(nèi)容 Button btn_send; //定義一個(gè)handler public Handler mHandler = new Handler() { public void handleMessage(Message msg) { //打印服務(wù)器端發(fā)來(lái)的消息 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); //設(shè)置handler mt.setIp("10.10.10.104"); //設(shè)置服務(wù)器地址 mt.start(); //啟動(dòng)線程 btn_send = (Button)findViewById(R.id.btn_send); btn_send.setOnClickListener(new OnClickListener() { //向服務(wù)器端發(fā)送數(shù)據(jù) public void onClick(View v) { if(!mt.write(et_text.getText().toString())) { Toast.makeText(getApplicationContext(), "發(fā)送失敗", 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; //接收流 //獲取另一個(gè)線程的Handler public void setHandler( Handler handler){ mHandler = handler; } //設(shè)置服務(wù)器IP public void setIp(String ip){ this.ip = ip; } public void run(){ try { socket = new Socket(ip, 8080); //訪問(wèn)指定的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) //讀取服務(wù)器端發(fā)送來(lái)的數(shù)據(jù) { final byte[] buffer = new byte[1024];//創(chuàng)建接收緩沖區(qū) inputStream = socket.getInputStream(); final int len = inputStream.read(buffer);//數(shù)據(jù)讀出來(lái),并且返回?cái)?shù)據(jù)的長(zhǎng)度 if(len>0) { Message msg = mHandler.obtainMessage(); //設(shè)置發(fā)送的內(nèi)容 msg.obj = new String(buffer,0,len); mHandler.sendMessage(msg); } } } catch (IOException e) { } } //向服務(wù)器端寫(xiě)入數(shù)據(jù) public boolean write(String text){ boolean ret = true; try { outputStream.write(text.toString().getBytes()); } catch (IOException e) { ret = false; e.printStackTrace(); } return ret; } }
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)TCP客戶端支持讀寫(xiě)操作,希望對(duì)大家有所幫助!
相關(guān)文章
Android如何給Textview添加菜單項(xiàng)詳解(Java)
TextView是android里面用的最多的控件,TextView類似一般UI中的Label,TextBlock等控件,只是為了單純的顯示一行或多行文本,下面這篇文章主要給大家介紹了關(guān)于Android如何給Textview添加菜單項(xiàng)的相關(guān)資料,需要的朋友可以參考下2022-01-01Android6.0 動(dòng)態(tài)權(quán)限機(jī)制深入講解
這篇文章主要給大家介紹了關(guān)于Android6.0 動(dòng)態(tài)權(quán)限機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android編程中Perferences的用法實(shí)例分析
這篇文章主要介紹了Android編程中Perferences的用法,以實(shí)例形式較為詳細(xì)的分析了配置文件preferences.xml的功能、定義及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android 使用Vitamio打造自己的萬(wàn)能播放器(8)——細(xì)節(jié)優(yōu)化
本文主要介紹Android Vitamio開(kāi)發(fā)播放器,這里給大家提供了一些小的細(xì)節(jié)優(yōu)化,更加完善播放器的功能,希望能幫助有需要的小伙伴2016-07-07利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法
這篇文章主要介紹了利用源碼編譯Android系統(tǒng)的APK和可執(zhí)行命令的方法,示例在Linux系統(tǒng)環(huán)境上進(jìn)行構(gòu)建,需要的朋友可以參考下2016-02-02Android遞歸方式刪除某文件夾下的所有文件(.mp3文件等等)
以刪除為例,當(dāng)然,對(duì)于遍歷某文件夾下的所有文件均可用這個(gè)方法。如搜索.mp3文件等,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Android實(shí)現(xiàn)底部狀態(tài)欄切換的兩種方式
這篇文章主要介紹了Android實(shí)現(xiàn)底部狀態(tài)欄切換功能,在文中給大家提到了兩種實(shí)現(xiàn)方式,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06