Android连接服务器端的Socket的实例代码
程序员文章站
2023-12-02 18:23:10
废话不多说了,直接给大家贴代码了,具体代码如下所述:
package com.exa
mple.esp8266;
import java.io.ioexcep...
废话不多说了,直接给大家贴代码了,具体代码如下所述:
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的实例代码,希望对大家有所帮助