本文主要是介绍Android与服务器通信的方法之一(TCP)效率高安全性完善,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android与服务器通信的方法之一(TCP)效率高安全性完善
客户端代码:
Java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | package com.yarin.android.Examples_08_04; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Activity01 extends Activity { private final String DEBUG_TAG = "Activity01" ; private TextView mTextView= null ; private EditText mEditText= null ; private Button mButton= null ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button)findViewById(R.id.Button01); mTextView=(TextView)findViewById(R.id.TextView01); mEditText=(EditText)findViewById(R.id.EditText01); //登陆 mButton.setOnClickListener( new OnClickListener() { public void onClick(View v) { Socket socket = null ; String message = mEditText.getText().toString() + "\r\n" ; try { //创建Socket socket = new Socket( "116.29.27.138" , 5554 ); //查看本机IP,每次开机都不同 //socket=new Socket("192.168.1.110",50000); //向服务器发送消息 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true ); out.println(message); //接收来自服务器的消息 BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); if ( msg != null ) { mTextView.setText(msg); } else { mTextView.setText( "数据错误!" ); } //关闭流 out.close(); br.close(); //关闭Socket socket.close(); } catch (Exception e) { // TODO: handle exception Log.e(DEBUG_TAG, e.toString()); } } }); } } import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Activity01 extends Activity { private final String DEBUG_TAG = "Activity01" ; private TextView mTextView= null ; private EditText mEditText= null ; private Button mButton= null ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button)findViewById(R.id.Button01); mTextView=(TextView)findViewById(R.id.TextView01); mEditText=(EditText)findViewById(R.id.EditText01); //登陆 mButton.setOnClickListener( new OnClickListener() { public void onClick(View v) { Socket socket = null ; String message = mEditText.getText().toString() + "\r\n" ; try { //创建Socket socket = new Socket( "116.29.27.138" , 5554 ); //查看本机IP,每次开机都不同 //socket=new Socket("192.168.1.110",50000); //向服务器发送消息 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true ); out.println(message); //接收来自服务器的消息 BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); if ( msg != null ) { mTextView.setText(msg); } else { mTextView.setText( "数据错误!" ); } //关闭流 out.close(); br.close(); //关闭Socket socket.close(); } catch (Exception e) { // TODO: handle exception Log.e(DEBUG_TAG, e.toString()); } } }); } } 服务器端代码: Java代码 package com.yarin.android.Examples_08_04; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server implements Runnable { public void run() { try { //创建ServerSocket ServerSocket serverSocket = new ServerSocket( 5554 ); while ( true ) { //接受客户端请求 Socket client = serverSocket.accept(); System.out.println( "accept" ); try { //接收客户端消息 BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); String str = in.readLine(); System.out.println( "read:" + str); //向服务器发送消息 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())), true ); out.println( "server message" ); //关闭流 out.close(); in.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { //关闭 client.close(); System.out.println( "close" ); } } } catch (Exception e) { System.out.println(e.getMessage()); } } //main函数,开启服务器 public static void main(String a[]) { Thread desktopServerThread = new Thread( new Server()); desktopServerThread.start(); } } |
这篇关于Android与服务器通信的方法之一(TCP)效率高安全性完善的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!