废话不多说了,直接给大家贴代码了,具体代码如下所述:
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的实例代码全部内容,希望文章能够帮你解决Android连接服务器端的Socket的实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)