Android与服务器通信的方法之一(TCP)效率高安全性完善

2024-06-07 00:48

本文主要是介绍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)效率高安全性完善的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1037708

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安