Android实现TCP客户端
Android實現TCP客戶端
1.添加相關權限,使得android app可以訪問網絡
在AndroidManiffest.xml中添加
<uses-permission android:name="android.permission.INTERNET"/>2.socket的連接
由于android平臺的限制,與網絡相關的操作只能在子線程中進行,所以這里我們單獨建立一個線程用于socket的連接
//子線程中進行網絡相關操作class connectthread extends Thread {OutputStream outputStream=null;InputStream inputStream=null;@Overridepublic void run() {//連接try {socket=new Socket(a, b);runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接成功",Toast.LENGTH_SHORT).show();}});} catch (UnknownHostException e) {// TODO Auto-generated catch blockrunOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接失敗",Toast.LENGTH_SHORT).show();}});e.printStackTrace();}catch (IOException e) {e.printStackTrace();runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接失敗",Toast.LENGTH_SHORT).show();}});} }socket=new socket(a,b)這一方法可以建立一個tcp連接,其中,a為ip地址,b為端口號
如果連接成功,我們及通過Toast在屏幕中顯示“連接成功”,若連接失敗,則會轉到異常中,我們通過Toast顯示“連接失敗”。
3.通過輸出流發送消息
在子線程中添加以下代碼,獲取socket的輸出流對象
并通過輸出流對象的write()方法向服務器發送“123”
以上就是最簡單的消息發送,下面我們通過edittext獲取輸入的內容,并將輸入的內容發送給服務器
//發送send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//子線程中進行網絡操作new Thread(new Runnable() {@Overridepublic void run() {if(socket!=null){try {String text=out.getText().toString();lianjie.outputStream.write(text.getBytes());} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e) {e.printStackTrace();}}else{runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"請先建立連接",Toast.LENGTH_SHORT).show();}});}}}).start();}});需要注意的是,發送的操作需要在子線程中進行,所以這里我又建立了一個線程來進行發送的操作,在線程中獲取socket的輸出流對象即可進行內容的發送。
在這里我加了一個提示信息,如果socket為空的話,則提示“請先建立連接”。
4.通過輸入流獲取消息
在子線程中建立一個死循環,時刻監聽輸入流,讀取服務器發送來的消息
try{while (true){final byte[] buffer = new byte[1024];//創建接收緩沖區inputStream = socket.getInputStream();final int len = inputStream.read(buffer);//數據讀出來,并且返回數據的長度runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubreceive.append(new String(buffer,0,len)+"\r\n");}});}}catch (IOException e) {}5.測試結果
客戶端
服務器
經過測試,服務器和客戶端之間可以正常的發送和接收信息。
6.源代碼
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:id="@+id/ip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="ip"/><EditTextandroid:id="@+id/port"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="port"/><EditTextandroid:id="@+id/out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="請輸入要發送的內容"/><Buttonandroid:id="@+id/connect"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="連接"/><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="發送"/><TextViewandroid:id="@+id/receive"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>Mainactivity
public class MainActivity extends AppCompatActivity {String a;int b;connectthread lianjie;TextView receive;Socket socket=null;Button connect;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);EditText ip=findViewById(R.id.ip);EditText port=findViewById(R.id.port);EditText out=findViewById(R.id.out);receive=findViewById(R.id.receive);connect=findViewById(R.id.connect);Button send=findViewById(R.id.send);connect.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {a=ip.getText().toString();String c=port.getText().toString();if("".equals(a)||"".equals(c)){Toast.makeText(MainActivity.this,"請輸入ip和端口號",Toast.LENGTH_SHORT).show();}else{b=Integer.valueOf(c);lianjie=new connectthread();lianjie.start();}}});//發送send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//子線程中進行網絡操作new Thread(new Runnable() {@Overridepublic void run() {if(socket!=null){try {String text=out.getText().toString();lianjie.outputStream.write(text.getBytes());} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e) {e.printStackTrace();}}else{runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"請先建立連接",Toast.LENGTH_SHORT).show();}});}}}).start();}});}//子線程中進行網絡相關操作class connectthread extends Thread {OutputStream outputStream=null;InputStream inputStream=null;@Overridepublic void run() {//連接try {socket=new Socket(a, b);runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接成功",Toast.LENGTH_SHORT).show();}});} catch (UnknownHostException e) {// TODO Auto-generated catch blockrunOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接失敗",Toast.LENGTH_SHORT).show();}});e.printStackTrace();}catch (IOException e) {e.printStackTrace();runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubToast.makeText(MainActivity.this,"連接失敗",Toast.LENGTH_SHORT).show();}});}if(socket!=null){//獲取輸出流對象try {outputStream=socket.getOutputStream();outputStream.write(123);} catch (IOException e) {e.printStackTrace();}try{while (true){final byte[] buffer = new byte[1024];//創建接收緩沖區inputStream = socket.getInputStream();final int len = inputStream.read(buffer);//數據讀出來,并且返回數據的長度runOnUiThread(new Runnable()//不允許其他線程直接操作組件,用提供的此方法可以{public void run(){// TODO Auto-generated method stubreceive.append(new String(buffer,0,len)+"\r\n");}});}}catch (IOException e) {}}}; }}7.參考文章
Android 一步步實現TCP客戶端
Android網絡編程之–Socket編程
android 之TCP客戶端編程
Android Studio TCP客戶端實現
Android的SocketTCP客戶端發送信息
總結
以上是生活随笔為你收集整理的Android实现TCP客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取系统信息1——linux系统中的时间
- 下一篇: android sina oauth2.