Android连接服务器端的Socket的实例代码

发布时间:2020-10-02 07:46:13 作者:interestOrMoney
来源:脚本之家 阅读:200

废话不多说了,直接给大家贴代码了,具体代码如下所述:

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;
    }
   }
  });
  // 发送
  btnSend.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    // 启动发送线程
    new Thread(SendThread).start();
   }
  });
  myHandler = new Handler() {// UI主线程消息处理函数
   public void handleMessage(Message msg) {
    Bundle bundle = msg.getData();
    String string = bundle.toString();
    edReceive.setText(string);
   }
  };
 }
 // 连接到服务器的接口
 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();
    // 初始化发送线程
    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);
   }
  }
 };
 // 发送线程
 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连接服务器端的Socket的实例代码

以上所述是小编给大家介绍的Android连接服务器端的Socket的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

推荐阅读:
  1. Android Socket 发送广播包的那些坑
  2. Android中基于Socket的网络通信

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 服务器端 socket

上一篇:子类化QMainWindows

下一篇:C#实现定时关机小应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》