标签:
一、Android Socket 通信简介
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信。两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据。而Socket通信则是在双方建立起连接后就可以直接进行数据的传输,在连接时可实现信息的主动推送,而不需要每次由客户端想服务器发送请求。 那么,什么是socket?Socket又称套接字,在程序内部提供了与外界通信的端口,即端口通信。通过建立socket连接,可为通信双方的数据传输传提供通道。socket的主要特点有数据丢失率低,使用简单且易于移植。
二、理论性东西就不多说了,直接让我们来看看实现代码!!!!1
客户端的实现。
1 //连接到服务器的接口 2 Runnable connectThread = new Runnable() { 3 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 //初始化Scoket,连接到服务器 8 try { 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 30 public void run() { 31 // TODO Auto-generated method stub 32 byte[] sendBuffer = null; 33 try { 34 sendBuffer = strMessage.getBytes("UTF-8"); 35 } catch (UnsupportedEncodingException e1) { 36 // TODO Auto-generated catch block 37 e1.printStackTrace(); 38 } 39 try { 40 outStream = socket.getOutputStream(); 41 } catch (IOException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 try { 46 outStream.write(sendBuffer); 47 } catch (IOException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 } 52 }; 53 54 //接收线程 55 private class ReceiveThread extends Thread{ 56 private InputStream inStream = null; 57 58 private byte[] buffer; 59 private String str = null; 60 61 ReceiveThread(Socket socket){ 62 try { 63 inStream = socket.getInputStream(); 64 } catch (IOException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 @Override 70 public void run(){ 71 while(isReceive){ 72 buffer = new byte[512]; 73 try { 74 inStream.read(buffer); 75 } catch (IOException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 try { 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 }
服务端实现。
1 //发送数据接口 2 public Runnable runnable=new Runnable() { 3 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 byte[] by=null; 8 try { 9 10 by=data.getBytes("GBK"); 11 ouStream=socket.getOutputStream(); 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 //接收数据线程 22 public class ThreadData extends Thread { 23 public InputStream inputStream; 24 25 26 @Override 27 public void run() { 28 // TODO Auto-generated method stub 29 super.run(); 30 31 try { 32 ServerSocket serverSocket = new ServerSocket(4567); 33 socket = serverSocket.accept(); 34 while (true) { 35 byte[] data = new byte[512]; 36 inputStream = socket.getInputStream(); 37 inputStream.read(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 }
标签:
原文地址:http://www.cnblogs.com/fhygl/p/5747566.html