安卓给pc发送信息,实例一已经正式成功

来源:互联网 发布:动画片 知乎 编辑:程序博客网 时间:2024/06/13 07:24

注意点:注册访问的网络权限;Android中UI线程不能有访问网络的操作,否则会报android.os.NetworkOnMainThreadException的异常

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <uses-permission   
  2.     android:name="android.permission.INTERNET"/>  

      Android开发联盟③ 433233634

      实例一

      客户端


      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.xiong.simplesocket;  
      2.   
      3. import java.io.BufferedReader;  
      4. import java.io.IOException;  
      5. import java.io.InputStreamReader;  
      6. import java.io.OutputStream;  
      7. import java.net.InetSocketAddress;  
      8. import java.net.Socket;  
      9. import java.net.SocketTimeoutException;  
      10.   
      11. import android.app.Activity;  
      12. import android.os.Bundle;  
      13. import android.os.Handler;  
      14. import android.os.Message;  
      15. import android.view.Menu;  
      16. import android.view.View;  
      17. import android.view.View.OnClickListener;  
      18. import android.widget.Button;  
      19. import android.widget.EditText;  
      20. import android.widget.TextView;  
      21.   
      22. public class MainActivity extends Activity {  
      23.     Socket socket = null;  
      24.     String buffer = "";  
      25.     TextView txt1;  
      26.     Button send;  
      27.     EditText ed1;  
      28.     String geted1;  
      29.     public Handler myHandler = new Handler() {  
      30.         @Override  
      31.         public void handleMessage(Message msg) {  
      32.             if (msg.what == 0x11) {  
      33.                 Bundle bundle = msg.getData();  
      34.                 txt1.append("server:"+bundle.getString("msg")+"\n");  
      35.             }  
      36.         }  
      37.   
      38.     };  
      39.   
      40.     @Override  
      41.     protected void onCreate(Bundle savedInstanceState) {  
      42.         super.onCreate(savedInstanceState);  
      43.         setContentView(R.layout.activity_main);  
      44.         txt1 = (TextView) findViewById(R.id.txt1);  
      45.         send = (Button) findViewById(R.id.send);  
      46.         ed1 = (EditText) findViewById(R.id.ed1);  
      47.         send.setOnClickListener(new OnClickListener() {  
      48.   
      49.             @Override  
      50.             public void onClick(View v) {  
      51.                 geted1 = ed1.getText().toString();  
      52.                 txt1.append("client:"+geted1+"\n");  
      53.                 //启动线程 向服务器发送和接收信息  
      54.                 new MyThread(geted1).start();  
      55.             }  
      56.         });  
      57.   
      58.     }  
      59.   
      60.     class MyThread extends Thread {  
      61.   
      62.         public String txt1;  
      63.   
      64.         public MyThread(String str) {  
      65.             txt1 = str;  
      66.         }  
      67.   
      68.         @Override  
      69.         public void run() {  
      70.             //定义消息  
      71.             Message msg = new Message();  
      72.             msg.what = 0x11;  
      73.             Bundle bundle = new Bundle();  
      74.             bundle.clear();  
      75.             try {  
      76.                 //连接服务器 并设置连接超时为5秒  
      77.                 socket = new Socket();  
      78.                 socket.connect(new InetSocketAddress("1.1.9.30"30000), 5000);  
      79.                 //获取输入输出流  
      80.                 OutputStream ou = socket.getOutputStream();  
      81.                 BufferedReader bff = new BufferedReader(new InputStreamReader(  
      82.                         socket.getInputStream()));  
      83.                 //读取发来服务器信息  
      84.                 String line = null;  
      85.                 buffer="";  
      86.                 while ((line = bff.readLine()) != null) {  
      87.                     buffer = line + buffer;  
      88.                 }  
      89.                   
      90.                 //向服务器发送信息  
      91.                 ou.write("android 客户端".getBytes("gbk"));  
      92.                 ou.flush();  
      93.                 bundle.putString("msg", buffer.toString());  
      94.                 msg.setData(bundle);  
      95.                 //发送消息 修改UI线程中的组件  
      96.                 myHandler.sendMessage(msg);  
      97.                 //关闭各种输入输出流  
      98.                 bff.close();  
      99.                 ou.close();  
      100.                 socket.close();  
      101.             } catch (SocketTimeoutException aa) {  
      102.                 //连接超时 在UI界面显示消息  
      103.                 bundle.putString("msg""服务器连接失败!请检查网络是否打开");  
      104.                 msg.setData(bundle);  
      105.                 //发送消息 修改UI线程中的组件  
      106.                 myHandler.sendMessage(msg);  
      107.             } catch (IOException e) {  
      108.                 e.printStackTrace();  
      109.             }  
      110.         }  
      111.     }  
      112.   
      113.     @Override  
      114.     public boolean onCreateOptionsMenu(Menu menu) {  
      115.         // Inflate the menu; this adds items to the action bar if it is present.  
      116.         getMenuInflater().inflate(R.menu.main, menu);  
      117.         return true;  
      118.     }  
      119.   
      120. }  

      [html] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     xmlns:tools="http://schemas.android.com/tools"  
      3.     android:layout_width="match_parent"  
      4.     android:layout_height="match_parent"  
      5.     android:paddingBottom="@dimen/activity_vertical_margin"  
      6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
      7.     android:paddingRight="@dimen/activity_horizontal_margin"  
      8.     android:paddingTop="@dimen/activity_vertical_margin"  
      9.     tools:context=".MainActivity" >  
      10.   
      11.     <EditText   
      12.         android:id="@+id/ed1"  
      13.         android:layout_width="match_parent"  
      14.         android:layout_height="wrap_content"  
      15.         android:hint="给服务器发送信息"/>  
      16.     <Button   
      17.         android:id="@+id/send"  
      18.         android:layout_width="match_parent"  
      19.         android:layout_height="wrap_content"  
      20.         android:layout_below="@id/ed1"  
      21.         android:text="发送"/>  
      22.     <TextView   
      23.         android:id="@+id/txt1"  
      24.         android:layout_width="match_parent"  
      25.         android:layout_height="wrap_content"  
      26.         android:layout_below="@id/send"/>  
      27.       
      28.       
      29.   
      30.   
      31. </RelativeLayout>  

      服务端

      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.net;  
      2.   
      3. import java.io.IOException;  
      4. import java.net.ServerSocket;  
      5. import java.net.Socket;  
      6. import java.util.ArrayList;  
      7. import java.util.List;  
      8.   
      9. public class AndroidService {  
      10.   
      11.   
      12.     public static void main(String[] args) throws IOException {  
      13.         ServerSocket serivce = new ServerSocket(30000);  
      14.         while (true) {  
      15.             //等待客户端连接  
      16.             Socket socket = serivce.accept();  
      17.             new Thread(new AndroidRunable(socket)).start();  
      18.         }  
      19.     }  
      20.   
      21. }  

      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.net;  
      2.   
      3. import java.io.BufferedReader;  
      4. import java.io.BufferedWriter;  
      5. import java.io.IOException;  
      6. import java.io.InputStream;  
      7. import java.io.InputStreamReader;  
      8. import java.io.OutputStream;  
      9. import java.io.OutputStreamWriter;  
      10. import java.io.PrintWriter;  
      11. import java.net.Socket;  
      12.   
      13. public class AndroidRunable implements Runnable {  
      14.   
      15.     Socket socket = null;  
      16.   
      17.     public AndroidRunable(Socket socket) {  
      18.         this.socket = socket;  
      19.     }  
      20.   
      21.     @Override  
      22.     public void run() {  
      23.         // 向android客户端输出hello worild  
      24.         String line = null;  
      25.         InputStream input;  
      26.         OutputStream output;  
      27.         String str = "hello world!";  
      28.         try {  
      29.             //向客户端发送信息  
      30.             output = socket.getOutputStream();  
      31.             input = socket.getInputStream();  
      32.             BufferedReader bff = new BufferedReader(  
      33.                     new InputStreamReader(input));  
      34.             output.write(str.getBytes("gbk"));  
      35.             output.flush();  
      36.             //半关闭socket    
      37.             socket.shutdownOutput();  
      38.             //获取客户端的信息  
      39.             while ((line = bff.readLine()) != null) {  
      40.                 System.out.print(line);  
      41.             }  
      42.             //关闭输入输出流  
      43.             output.close();  
      44.             bff.close();  
      45.             input.close();  
      46.             socket.close();  
      47.   
      48.         } catch (IOException e) {  
      49.             e.printStackTrace();  
      50.         }  
      51.   
      52.     }  
      53. }  
      =======================================以上是经过证实成功的代码==========

      实例二

      客户端

      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.xiong.sockettwotest;  
      2.   
      3. import android.app.Activity;  
      4. import android.os.Bundle;  
      5. import android.os.Handler;  
      6. import android.os.Message;  
      7. import android.view.Menu;  
      8. import android.view.View;  
      9. import android.view.View.OnClickListener;  
      10. import android.widget.Button;  
      11. import android.widget.EditText;  
      12. import android.widget.TextView;  
      13.   
      14. public class MainActivity extends Activity {  
      15.   
      16.     // 定义界面上的两个文本框  
      17.     EditText input;  
      18.     TextView show;  
      19.     // 定义界面上的一个按钮  
      20.     Button send;  
      21.     Handler handler;  
      22.     // 定义与服务器通信的子线程  
      23.     ClientThread clientThread;  
      24.   
      25.     @Override  
      26.     protected void onCreate(Bundle savedInstanceState) {  
      27.         super.onCreate(savedInstanceState);  
      28.         setContentView(R.layout.activity_main);  
      29.         input = (EditText) findViewById(R.id.input);  
      30.         show = (TextView) findViewById(R.id.show);  
      31.         send = (Button) findViewById(R.id.send);  
      32.         handler = new Handler() {  
      33.   
      34.             @Override  
      35.             public void handleMessage(Message msg) {  
      36.                 // 如果消息来自子线程  
      37.                 if (msg.what == 0x123) {  
      38.                     // 将读取的内容追加显示在文本框中  
      39.                     show.append("\n" + msg.obj.toString());  
      40.                 }  
      41.             }  
      42.         };  
      43.         clientThread = new ClientThread(handler);  
      44.         // 客户端启动ClientThread线程创建网络连接、读取来自服务器的数据  
      45.         new Thread(clientThread).start();  
      46.         send.setOnClickListener(new OnClickListener() {  
      47.   
      48.             @Override  
      49.             public void onClick(View v) {  
      50.                 try {  
      51.                     // 当用户按下按钮之后,将用户输入的数据封装成Message  
      52.                     // 然后发送给子线程Handler  
      53.                     Message msg = new Message();  
      54.                     msg.what = 0x345;  
      55.                     msg.obj = input.getText().toString();  
      56.                     clientThread.revHandler.sendMessage(msg);  
      57.                     input.setText("");  
      58.   
      59.                 } catch (Exception e) {  
      60.   
      61.                 }  
      62.             }  
      63.         });  
      64.     }  
      65.   
      66.     @Override  
      67.     public boolean onCreateOptionsMenu(Menu menu) {  
      68.         getMenuInflater().inflate(R.menu.main, menu);  
      69.         return true;  
      70.     }  
      71.   
      72. }  

      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.xiong.sockettwotest;  
      2.   
      3. import java.io.BufferedReader;  
      4. import java.io.IOException;  
      5. import java.io.InputStreamReader;  
      6. import java.io.OutputStream;  
      7. import java.net.InetSocketAddress;  
      8. import java.net.Socket;  
      9. import java.net.SocketTimeoutException;  
      10.   
      11. import android.os.Handler;  
      12. import android.os.Looper;  
      13. import android.os.Message;  
      14.   
      15. public class ClientThread implements Runnable {  
      16.     private Socket s;  
      17.     // 定义向UI线程发送消息的Handler对象  
      18.     Handler handler;  
      19.     // 定义接收UI线程的Handler对象  
      20.     Handler revHandler;  
      21.     // 该线程处理Socket所对用的输入输出流  
      22.     BufferedReader br = null;  
      23.     OutputStream os = null;  
      24.   
      25.     public ClientThread(Handler handler) {  
      26.         this.handler = handler;  
      27.     }  
      28.   
      29.     @Override  
      30.     public void run() {  
      31.         s = new Socket();  
      32.         try {  
      33.             s.connect(new InetSocketAddress("1.1.9.30"3000), 5000);  
      34.             br = new BufferedReader(new InputStreamReader(s.getInputStream()));  
      35.             os = s.getOutputStream();  
      36.             // 启动一条子线程来读取服务器相应的数据  
      37.             new Thread() {  
      38.   
      39.                 @Override  
      40.                 public void run() {  
      41.                     String content = null;  
      42.                     // 不断的读取Socket输入流的内容  
      43.                     try {  
      44.                         while ((content = br.readLine()) != null) {  
      45.                             // 每当读取到来自服务器的数据之后,发送的消息通知程序  
      46.                             // 界面显示该数据  
      47.                             Message msg = new Message();  
      48.                             msg.what = 0x123;  
      49.                             msg.obj = content;  
      50.                             handler.sendMessage(msg);  
      51.                         }  
      52.                     } catch (IOException io) {  
      53.                         io.printStackTrace();  
      54.                     }  
      55.                 }  
      56.   
      57.             }.start();  
      58.             // 为当前线程初始化Looper  
      59.             Looper.prepare();  
      60.             // 创建revHandler对象  
      61.             revHandler = new Handler() {  
      62.   
      63.                 @Override  
      64.                 public void handleMessage(Message msg) {  
      65.                     // 接收到UI线程的中用户输入的数据  
      66.                     if (msg.what == 0x345) {  
      67.                         // 将用户在文本框输入的内容写入网络  
      68.                         try {  
      69.                             os.write((msg.obj.toString() + "\r\n")  
      70.                                     .getBytes("gbk"));  
      71.                         } catch (Exception e) {  
      72.                             e.printStackTrace();  
      73.                         }  
      74.                     }  
      75.                 }  
      76.   
      77.             };   
      78.             // 启动Looper  
      79.             Looper.loop();  
      80.   
      81.         } catch (SocketTimeoutException e) {  
      82.             Message msg = new Message();  
      83.             msg.what = 0x123;  
      84.             msg.obj = "网络连接超时!";  
      85.             handler.sendMessage(msg);  
      86.         } catch (IOException io) {  
      87.             io.printStackTrace();  
      88.         }  
      89.   
      90.     }  
      91. }  

      [html] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     xmlns:tools="http://schemas.android.com/tools"  
      3.     android:layout_width="match_parent"  
      4.     android:layout_height="match_parent"  
      5.     android:paddingBottom="@dimen/activity_vertical_margin"  
      6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
      7.     android:paddingRight="@dimen/activity_horizontal_margin"  
      8.     android:paddingTop="@dimen/activity_vertical_margin"  
      9.     tools:context=".MainActivity" >  
      10.   
      11.     <EditText  
      12.         android:id="@+id/input"  
      13.         android:layout_width="match_parent"  
      14.         android:layout_height="wrap_content"  
      15.         android:hint="@string/input" />  
      16.     <Button   
      17.         android:id="@+id/send"  
      18.         android:layout_width="match_parent"  
      19.         android:layout_height="wrap_content"  
      20.         android:text="@string/send"  
      21.         android:layout_below="@id/input"/>  
      22.     <TextView   
      23.         android:id="@+id/show"  
      24.         android:layout_width="match_parent"  
      25.         android:layout_height="wrap_content"  
      26.         android:layout_below="@id/send"/>  
      27.   
      28. </RelativeLayout>  

      服务端


      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.net;  
      2.   
      3. import java.io.IOException;  
      4. import java.net.ServerSocket;  
      5. import java.net.Socket;  
      6. import java.util.ArrayList;  
      7. import java.util.List;  
      8.   
      9. public class MyService {  
      10.   
      11.     // 定义保存所有的Socket  
      12.     public static List<Socket> socketList = new ArrayList<Socket>();  
      13.   
      14.     public static void main(String[] args) throws IOException {  
      15.         ServerSocket server = new ServerSocket(3000);  
      16.         while(true){  
      17.             Socket s=server.accept();  
      18.             socketList.add(s);  
      19.             //每当客户端连接之后启动一条ServerThread线程为该客户端服务  
      20.             new Thread(new ServiceThreada(s)).start();  
      21.               
      22.         }  
      23.     }  
      24.   
      25. }  


      [java] view plain copy
       在CODE上查看代码片派生到我的代码片
      1. package com.android.net;  
      2.   
      3. import java.io.BufferedReader;  
      4. import java.io.IOException;  
      5. import java.io.InputStreamReader;  
      6. import java.io.OutputStream;  
      7. import java.net.Socket;  
      8.   
      9. public class ServiceThreada implements Runnable {  
      10.   
      11.     // 定义当前线程处理的Socket  
      12.     Socket s = null;  
      13.     // 该线程所处理的Socket所对应的输入流  
      14.     BufferedReader br = null;  
      15.   
      16.     public ServiceThreada(Socket s) {  
      17.         this.s = s;  
      18.         try {  
      19.             br = new BufferedReader(new InputStreamReader(s.getInputStream()));  
      20.         } catch (IOException e) {  
      21.             e.printStackTrace();  
      22.         }  
      23.     }  
      24.   
      25.     @Override  
      26.     public void run() {  
      27.   
      28.         String content = null;  
      29.         //采用循环不断的从Socket中读取客户端发送过来的数据  
      30.         while((content=readFromClient())!=null){  
      31.             //遍历socketList中的每个Socket  
      32.             //将读取到的内容每个向Socket发送一次  
      33.             for(Socket s:MyService.socketList){  
      34.                 OutputStream os;  
      35.                 try {  
      36.                     os = s.getOutputStream();  
      37.                     os.write((content+"\n").getBytes("gbk"));  
      38.                 } catch (IOException e) {  
      39.                     // TODO Auto-generated catch block  
      40.                     e.printStackTrace();  
      41.                 }  
      42.                   
      43.             }  
      44.         }  
      45.   
      46.     }  
      47.   
      48.     // 定义读取客户端的信息  
      49.     public String readFromClient() {  
      50.         try {  
      51.             return br.readLine();  
      52.         } catch (Exception e) {  
      53.             e.printStackTrace();  
      54.         }  
      55.         return null;  
      56.     }  
      57.   

0 0
原创粉丝点击