Android简单Socket通信demo
⼀、Android Socket 通信简介
Android与服务器的通信⽅式主要有两种,⼀是Http通信,⼀是Socket通信。两者的最⼤差异在于,http连接使⽤的是“请求—响应⽅式”,即在请求时建⽴连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据。⽽Socket通信则是在双⽅建⽴起连接后就可以直接进⾏数据的传输,在连接时可实现信息的主动推送,⽽不需要每次由客户端想服务器发送请求。那么,什么是socket?Socket⼜称套接字,在程序内部提供了与外界通信的端⼝,即端⼝通信。通过建⽴socket连接,可为通信双⽅的数据传输传提供通道。socket的主要特点有数据丢失率低,使⽤简单且易于移植。
⼆、理论性东西就不多说了,直接让我们来看看实现代码1
客户端的实现。
1//连接到服务器的接⼝
2 Runnable connectThread = new Runnable() {
3
4 @Override
5public void run() {
6// TODO Auto-generated method stub
7//初始化Scoket,连接到服务器
8try {
9 socket = new Socket(ServerIP, ServerPort);
10 isConnect = true;
11//启动接收线程
12 isReceive = true;
13 receiveThread = new ReceiveThread(socket);
14 receiveThread.start();
15 System.out.println("----connected success----");
16 } catch (UnknownHostException e) {
17// TODO Auto-generated catch block
18 e.printStackTrace();
19 } catch (IOException e) {
20// TODO Auto-generated catch block
21 e.printStackTrace();
22 }
23
24 }
25 };
26//发送消息的接⼝
27 Runnable sendThread = new Runnable() {
28
29 @Override
30public void run() {
31// TODO Auto-generated method stub
32byte[] sendBuffer = null;
33try {
34 sendBuffer = Bytes("UTF-8");
35 } catch (UnsupportedEncodingException e1) {
36// TODO Auto-generated catch block
socket通信为什么要指定端口37 e1.printStackTrace();
38 }
39try {
40 outStream = OutputStream();
41 } catch (IOException e) {
42// TODO Auto-generated catch block
43 e.printStackTrace();
44 }
45try {
46 outStream.write(sendBuffer);
47 } catch (IOException e) {
48// TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 }
52 };
53
54//接收线程
55private class ReceiveThread extends Thread{
56private InputStream inStream = null;
57
58private byte[] buffer;
59private String str = null;
60
61 ReceiveThread(Socket socket){
62try {
63 inStream = InputStream();
64 } catch (IOException e) {
65// TODO Auto-generated catch block
66 e.printStackTrace();
67 }
68 }
69 @Override
70public void run(){
71while(isReceive){
72 buffer = new byte[512];
73try {
74 ad(buffer);
75 } catch (IOException e) {
76// TODO Auto-generated catch block
77 e.printStackTrace();
78 }
79try {
80 str = new String(buffer,"UTF-8").trim();
81 } catch (UnsupportedEncodingException e) {
82// TODO Auto-generated catch block
83 e.printStackTrace();
84 }
85 Message msg = new Message();
86 msg.obj = str;
87 myHandler.sendMessage(msg);
88 }
89 }
View Code
服务端实现。
1//发送数据接⼝
2public Runnable runnable=new Runnable() {
3
4 @Override
5public void run() {
6// TODO Auto-generated method stub
7byte[] by=null;
8try {
9
10 Bytes("GBK");
11 OutputStream();
12 ouStream.write(by);
13 ouStream.flush();
14 } catch (IOException e) {
15// TODO Auto-generated catch block
16 e.printStackTrace();
17 }
18 }
19 };
20
21//接收数据线程
22public class ThreadData extends Thread {
23public InputStream inputStream;
24
25
26 @Override
27public void run() {
28// TODO Auto-generated method stub
29super.run();
30
31try {
32 ServerSocket serverSocket = new ServerSocket(4567);
33 socket = serverSocket.accept();
34while (true) {
35byte[] data = new byte[512];
36 inputStream = InputStream();
37 ad(data);
38 String strdata = new String(data, "GBK").trim();
39
40 Log.e("连接成功", strdata + "");
41 Message msg = handler.obtainMessage();
42 msg.obj=strdata;
43 handler.sendMessage(msg);
44 }
45 } catch (IOException e) {
46 Log.e("ttttttttttt", e.getMessage());
47 e.printStackTrace();
48 }
49
50
51 }
52 }
View Code
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论