Android連接服務(wù)器端的Socket的實例代碼

廢話不多說了,直接給大家貼代碼了,具體代碼如下所述:

創(chuàng)新互聯(lián)公司提供高防物理服務(wù)器租用、云服務(wù)器、香港服務(wù)器、中國電信成都樞紐中心

package com.exa

mple.esp8266;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
 private EditText edSend, edReceive;
 private Button btnConnect, btnSend;
 private Handler myHandler;
 private SendThread SendThread;
 private boolean isReceive = false;
 private boolean isConnect = false;
 private static final String HOST = "192.168.4.1";
 private static final int PORT = 333;
 String strMessage;
 Socket socket = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  edSend = (EditText) findViewById(R.id.edSend);
  edReceive = (EditText) findViewById(R.id.edReceive);
  btnConnect = (Button) findViewById(R.id.btConnect);
  btnSend = (Button) findViewById(R.id.btSend);
  // 連接
  btnConnect.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if (!isConnect) {
     new Thread(connectThread).start();
     isConnect = true;
    }
   }
  });
  // 發(fā)送
  btnSend.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    // 啟動發(fā)送線程
    new Thread(SendThread).start();
   }
  });
  myHandler = new Handler() {// UI主線程消息處理函數(shù)
   public void handleMessage(Message msg) {
    Bundle bundle = msg.getData();
    String string = bundle.toString();
    edReceive.setText(string);
   }
  };
 }
 // 連接到服務(wù)器的接口
 Runnable connectThread = new Runnable() {
  public void run() {
   // TODO Auto-generated method stub
   try {
    socket = new Socket(HOST, PORT);
    if (socket != null)
     Toast.makeText(getApplicationContext(), "連接成功",
       Toast.LENGTH_LONG).show();
    else
     Toast.makeText(getApplicationContext(), "連接失敗",
       Toast.LENGTH_LONG).show();
    // 初始化發(fā)送線程
    SendThread = new SendThread(socket);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 };
 // 接收消息的接口
 Runnable Receive = new Runnable() {
  InputStream inStream;
  private byte[] buffer;
  private String str = null;
  public void run() {
   // TODO Auto-generated method stub
   while (!isReceive) {
    buffer = new byte[512];
    try {
     inStream = socket.getInputStream();
     inStream.read(buffer);
    } catch (IOException e) {
     e.printStackTrace();
    }
    str = new String(buffer);
    Bundle bundle = new Bundle();
    bundle.get(str);
    Message message = new Message();
    message.setData(bundle);
    myHandler.sendMessage(message);
   }
  }
 };
 // 發(fā)送線程
 private class SendThread extends Thread {
  private OutputStream outStream = null;
  private String str = null;
  SendThread(Socket socket) {
   try {
    outStream = socket.getOutputStream();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  public void run() {
   // while(true){
   str = edSend.getText().toString().trim();
   PrintStream pt = new PrintStream(outStream);
   pt.print(str);
   new Thread(Receive).start();
   // }
  }
 }
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  if (Receive != null) {
   isReceive = false;
   ((Thread) Receive).interrupt();
  }
 }
}

Android連接服務(wù)器端的Socket的實例代碼

以上所述是小編給大家介紹的Android連接服務(wù)器端的Socket的實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

網(wǎng)頁名稱:Android連接服務(wù)器端的Socket的實例代碼
轉(zhuǎn)載來于:http://muchs.cn/article40/gjsgeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)做網(wǎng)站、微信小程序、網(wǎng)站維護響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司