《利用Socket与服务器端交互的简单Android范例代码.docx》由会员分享,可在线阅读,更多相关《利用Socket与服务器端交互的简单Android范例代码.docx(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、利用Socket与服务器端交互的简单Android范例代码在Android中可以直接利用java中的Socket与ServerSocket构建socket通 信。代码的运行环境:pc端:普通pc ,作为服务器,在5648端口进行监听。代码运行一切正常,客户端发送的文字将在服务器端接收并显示,服务器每接收 到客户端的一行文字,就会返回一个从0开始递增的整数,此整数将在客户端显 示出来。pc端代码:package test;import java io.BufferedReader;import java.io.lOException;import java.io.Inputstream;impo
2、rt java io.InputStreamReader;import java.io.Outputstream;import java,io.Printwriter;import .ServerSocket;import .Socket;public class Test implements Runnable/* param argspublic void run() try (String str;while (str=br.readLine()!= null )final String s=str;handler.post(new Runnable()public void run (
3、) Toast .makeText(MainActivity .this, Toast.LENGTH_LONG).show(););) catch (lOException e) / TODO Auto-generated catch blocke.printStackTrace();*/int max=10;/最大开启线程数int i=0;/回复数字int temp;ServerSocket serversocket;Socket socket;public Test ()try serverSocket=new ServerSocket( 5648);/在 5648 端口进行侦听 catc
4、h (lOException e) / TODO Auto-generated catch blocke.printStackTrace();System.out .println( cant imitate ServerSocket!11);return ;)socket=new Socketmax;System.out .println( waiting for connect);try while (socketi=serverSocket.accept()!=null )temp=i;i+;new Thread( this ).start。;每侦听到一个客户端的连接,就会开启一个工作线
5、程) catch (lOException e) / TODO Auto-generated catch blocke.printStackTrace();)public static void main (String args) new Test();Overridepublic void run() Socket sk=sockettemp;System, out .println( accept: +sk.getlnetAddress().getHostAddres s();Inputstream is=null;Outputstream os=null;BufferedReader
6、br=null;Printwriter pw=null;try (is=sk.getlnputstream();os=sk.getOutputStream();br=new BufferedReader( new InputStreamReader( is);pw=new PrintWriter(os); catch (lOException e) / TODO Auto-generated catch blocke.printStackTrace();try (sk.close(); catch (lOException el) / TODO Auto-generated catch blo
7、ckel.printStackTrace();return ;)String str;try (int m=0;while (str=br.readLine()!= null )System.out .println(str);pw.println(m);pw.flush();m+;) catch (lOException e) / TODO Auto-generated catch block e.printStackTrace();)android设备客户端代码:MainActivity. java;代码:import java.io.BufferedReader;import java.
8、io.lOException;import java.io.InputStreamReader;import java.io.Printwriter;import .Socket;import .UnknownHostException;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnC
9、lickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener,Runnableprivate Button button;private EditText editText;private Socket socket;private Printwriter pw;private BufferedReader br;pr
10、ivate Handler handler;Overridepublic void onCreate(Bundle savedlnstanceState) super .onCreate(savedlnstanceState);setContentView(R.layout.activity_main);handler=new Handler();button=(Button)findViewById(R.id.button);button.setOnClickListener(this);editText=(EditText)findViewById(R.id.edittext);try s
11、ocket=new Socket( ,5648);连接到 tobacco5648. xicp. net 的 5648 端口 catch (UnknownHostException e) / TODO Auto-generated catch blocke, printStackTrace();Log.e(socketunknown host); catch (lOException e) / TODO Auto-generated catch blocke.printStackTrace();Log.e(socket/io execption);if(socket=null)Log.e(soc
12、ket/null);)elsetry pw=new PrintWriter(socket.getOutputStream();br=new BufferedReader(new InputStreamReader(socket.getlnputstream();if(pw!=null&br!=null)new Thread(this).start(); catch (lOException e) / TODO Auto-generated catch blocke, printStackTrace();Overridepublic boolean onCreateOptionsMenu (Menu menu) menu);getMenuInflater()inflate(R.menuactivity_main return true ;)public void onClick(View view) if(view=button)String str;str=editText.getText()toString();pw.println(str);pw.flush();