Android连接服务器端的Socket的实例代码
作者:interestOrMoney 时间:2023-03-11 06:06:47
废话不多说了,直接给大家贴代码了,具体代码如下所述:
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的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://www.cnblogs.com/wjll/archive/2017/05/29/6918512.html
标签:android,服务器端,socket
0
投稿
猜你喜欢
解决Spring或SpringBoot开启事务以后无法返回自增主键的问题
2023-09-12 18:51:45
Flutter实现顶部导航栏功能
2023-03-10 17:13:48
dotNet中的反射用法入门教程
2023-12-01 01:08:28
基于C#实现端口扫描器(单线程和多线程)
2021-10-16 20:32:23
解决@Autowired注入static接口的问题
2022-01-02 03:16:29
C# SkinEngine控件 给窗体添加皮肤的方法
2021-10-21 23:32:08
详解使用Spring AOP和自定义注解进行参数检查
2021-11-27 00:06:49
树莓派.GPRS.短信接收器
2021-11-01 08:34:31
JAVA中的基本数据类型
2023-07-29 07:38:34
浅谈idea live template高级知识_进阶(给方法,类,js方法添加注释)
2023-12-07 22:50:28
JAVA中的字符串常量池使用操作代码
2021-11-24 02:00:57
通过java.util.TreeMap源码加强红黑树的理解
2021-07-27 08:45:59
C#中DateTime日期类型格式化显示方法汇总
2021-07-20 15:52:30
Springboot 整合 RabbitMQ 消息队列 详情
2021-07-17 18:49:42
Android Bitmap和Drawable的对比
2021-11-16 06:03:41
Java中List转Map List实现的几种姿势
2022-10-23 19:19:57
c#中SAPI使用总结——SpVoice的使用方法
2022-02-20 09:50:07
SpringCloud实战之Feign声明式服务调用
2022-07-02 08:25:30
使用mybatis-plus-generator进行代码自动生成的方法
2021-09-03 00:50:18
Spring AOP对嵌套方法不起作用的解决
2022-01-08 16:55:03