Android与服务器通信的方法之一(TCP)效率高安全性完善
Android与服务器通信的方法之一(TCP)效率高安全性完善
客户端代码:
Java代码
001 |
package com.yarin.android.Examples_08_04; |
002 |
|
003 |
import java.io.BufferedReader; |
004 |
import java.io.BufferedWriter; |
005 |
import java.io.InputStreamReader; |
006 |
import java.io.OutputStreamWriter; |
007 |
import java.io.PrintWriter; |
008 |
import java.net.InetAddress; |
009 |
import java.net.Socket; |
010 |
import android.app.Activity; |
011 |
import android.os.Bundle; |
012 |
import android.util.Log; |
013 |
import android.view.View; |
014 |
import android.view.View.OnClickListener; |
015 |
import android.widget.Button; |
016 |
import android.widget.EditText; |
017 |
import android.widget.TextView; |
018 |
|
019 |
public class Activity01 extends Activity |
020 |
{ |
021 |
private final String DEBUG_TAG = "Activity01" ; |
022 |
|
023 |
private TextView mTextView= null ; |
024 |
private EditText mEditText= null ; |
025 |
private Button mButton= null ; |
026 |
|
027 |
@Override |
028 |
public void onCreate(Bundle savedInstanceState) |
029 |
{ |
030 |
super .onCreate(savedInstanceState); |
031 |
setContentView(R.layout.main); |
032 |
|
033 |
mButton = (Button)findViewById(R.id.Button01); |
034 |
mTextView=(TextView)findViewById(R.id.TextView01); |
035 |
mEditText=(EditText)findViewById(R.id.EditText01); |
036 |
|
037 |
//登陆 |
038 |
mButton.setOnClickListener( new OnClickListener() |
039 |
{ |
040 |
public void onClick(View v) |
041 |
{ |
042 |
Socket socket = null ; |
043 |
String message = mEditText.getText().toString() + "\r\n" ; |
044 |
try |
045 |
{ |
046 |
//创建Socket |
047 |
socket = new Socket( "116.29.27.138" , 5554 ); //查看本机IP,每次开机都不同 |
048 |
//socket=new Socket("192.168.1.110",50000); |
049 |
//向服务器发送消息 |
050 |
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true ); |
051 |
out.println(message); |
052 |
|
053 |
//接收来自服务器的消息 |
054 |
BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream())); |
055 |
String msg = br.readLine(); |
056 |
|
057 |
if ( msg != null ) |
058 |
{ |
059 |
mTextView.setText(msg); |
060 |
} |
061 |
else |
062 |
{ |
063 |
mTextView.setText( "数据错误!" ); |
064 |
} |
065 |
//关闭流 |
066 |
out.close(); |
067 |
br.close(); |
068 |
//关闭Socket |
069 |
socket.close(); |
070 |
} |
071 |
catch (Exception e) |
072 |
{ |
073 |
// TODO: handle exception |
074 |
Log.e(DEBUG_TAG, e.toString()); |
075 |
} |
076 |
} |
077 |
}); |
078 |
} |
079 |
} |
080 |
081 |
import java.io.BufferedReader; |
082 |
import java.io.BufferedWriter; |
083 |
import java.io.InputStreamReader; |
084 |
import java.io.OutputStreamWriter; |
085 |
import java.io.PrintWriter; |
086 |
import java.net.InetAddress; |
087 |
import java.net.Socket; |
088 |
import android.app.Activity; |
089 |
import android.os.Bundle; |
090 |
import android.util.Log; |
091 |
import android.view.View; |
092 |
import android.view.View.OnClickListener; |
093 |
import android.widget.Button; |
094 |
import android.widget.EditText; |
095 |
import android.widget.TextView; |
096 |
097 |
public class Activity01 extends Activity |
098 |
{ |
099 |
private final String DEBUG_TAG = "Activity01" ; |
100 |
101 |
private TextView mTextView= null ; |
102 |
private EditText mEditText= null ; |
103 |
private Button mButton= null ; |
104 |
|
105 |
@Override |
106 |
public void onCreate(Bundle savedInstanceState) |
107 |
{ |
108 |
super .onCreate(savedInstanceState); |
109 |
setContentView(R.layout.main); |
110 |
111 |
mButton = (Button)findViewById(R.id.Button01); |
112 |
mTextView=(TextView)findViewById(R.id.TextView01); |
113 |
mEditText=(EditText)findViewById(R.id.EditText01); |
114 |
115 |
//登陆 |
116 |
mButton.setOnClickListener( new OnClickListener() |
117 |
{ |
118 |
public void onClick(View v) |
119 |
{ |
120 |
Socket socket = null ; |
121 |
String message = mEditText.getText().toString() + "\r\n" ; |
122 |
try |
123 |
{ |
124 |
//创建Socket |
125 |
socket = new Socket( "116.29.27.138" , 5554 ); //查看本机IP,每次开机都不同 |
126 |
//socket=new Socket("192.168.1.110",50000); |
127 |
//向服务器发送消息 |
128 |
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true ); |
129 |
out.println(message); |
130 |
131 |
//接收来自服务器的消息 |
132 |
BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream())); |
133 |
String msg = br.readLine(); |
134 |
135 |
if ( msg != null ) |
136 |
{ |
137 |
mTextView.setText(msg); |
138 |
} |
139 |
else |
140 |
{ |
141 |
mTextView.setText( "数据错误!" ); |
142 |
} |
143 |
//关闭流 |
144 |
out.close(); |
145 |
br.close(); |
146 |
//关闭Socket |
147 |
socket.close(); |
148 |
} |
149 |
catch (Exception e) |
150 |
{ |
151 |
// TODO: handle exception |
152 |
Log.e(DEBUG_TAG, e.toString()); |
153 |
} |
154 |
} |
155 |
}); |
156 |
} |
157 |
} |
158 |
159 |
服务器端代码: |
160 |
Java代码 |
161 |
package com.yarin.android.Examples_08_04; |
162 |
|
163 |
import java.io.BufferedReader; |
164 |
import java.io.BufferedWriter; |
165 |
import java.io.InputStreamReader; |
166 |
import java.io.OutputStreamWriter; |
167 |
import java.io.PrintWriter; |
168 |
import java.net.ServerSocket; |
169 |
import java.net.Socket; |
170 |
|
171 |
public class Server implements Runnable |
172 |
{ |
173 |
public void run() |
174 |
{ |
175 |
try |
176 |
{ |
177 |
//创建ServerSocket |
178 |
ServerSocket serverSocket = new ServerSocket( 5554 ); |
179 |
while ( true ) |
180 |
{ |
181 |
//接受客户端请求 |
182 |
Socket client = serverSocket.accept(); |
183 |
System.out.println( "accept" ); |
184 |
try |
185 |
{ |
186 |
//接收客户端消息 |
187 |
BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); |
188 |
String str = in.readLine(); |
189 |
System.out.println( "read:" + str); |
190 |
//向服务器发送消息 |
191 |
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())), true ); |
192 |
out.println( "server message" ); |
193 |
//关闭流 |
194 |
out.close(); |
195 |
in.close(); |
196 |
} |
197 |
catch (Exception e) |
198 |
{ |
199 |
System.out.println(e.getMessage()); |
200 |
e.printStackTrace(); |
201 |
} |
202 |
finally |
203 |
{ |
204 |
//关闭 |
205 |
client.close(); |
206 |
System.out.println( "close" ); |
207 |
} |
208 |
} |
209 |
} |
210 |
catch (Exception e) |
211 |
{ |
212 |
System.out.println(e.getMessage()); |
213 |
} |
214 |
} |
215 |
//main函数,开启服务器 |
216 |
public static void main(String a[]) |
217 |
{ |
218 |
Thread desktopServerThread = new Thread( new Server()); |
219 |
desktopServerThread.start(); |
220 |
} |
221 |
} |
屌丝一直很屌