Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5401614
  • 博文数量: 1272
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 15161
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1272)

文章存档

2020年(55)

2019年(193)

2018年(81)

2017年(80)

2016年(70)

2015年(52)

2014年(41)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2014-08-28 10:51:36

Android与服务器通信的方法之一-->TCP

http://www.cnblogs.com/liushengjie/archive/2012/09/16/2687828.html



点击(此处)折叠或打开

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.io.PrintWriter;
  6. import java.net.InetAddress;
  7. import java.net.Socket;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;

  16. public class Activity01 extends Activity
  17. {
  18. private final String DEBUG_TAG = "Activity01";

  19. private TextView mTextView=null;
  20. private EditText mEditText=null;
  21. private Button mButton=null;
  22.  
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);

  28. mButton = (Button)findViewById(R.id.Button01);
  29. mTextView=(TextView)findViewById(R.id.TextView01);
  30. mEditText=(EditText)findViewById(R.id.EditText01);

  31. //登陆
  32. mButton.setOnClickListener(new OnClickListener()
  33. {
  34. public void onClick(View v)
  35. {
  36. Socket socket = null;
  37. String message = mEditText.getText().toString() + "\r\n";
  38. try
  39. {
  40. //创建Socket
  41. socket = new Socket("116.29.27.138",5554); //查看本机IP,每次开机都不同
  42. //socket=new Socket("192.168.1.110",50000);
  43. //向服务器发送消息
  44. PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
  45. out.println(message);

  46. //接收来自服务器的消息
  47. BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  48. String msg = br.readLine();

  49. if ( msg != null )
  50. {
  51. mTextView.setText(msg);
  52. }
  53. else
  54. {
  55. mTextView.setText("数据错误!");
  56. }
  57. //关闭流
  58. out.close();
  59. br.close();
  60. //关闭Socket
  61. socket.close();
  62. }
  63. catch (Exception e)
  64. {
  65. // TODO: handle exception
  66. Log.e(DEBUG_TAG, e.toString());
  67. }
  68. }
  69. });
  70. }
  71. }

  72. 服务器端代码:
  73. Java代码
  74. package com.yarin.android.Examples_08_04;
  75.   
  76. import java.io.BufferedReader;
  77. import java.io.BufferedWriter;
  78. import java.io.InputStreamReader;
  79. import java.io.OutputStreamWriter;
  80. import java.io.PrintWriter;
  81. import java.net.ServerSocket;
  82. import java.net.Socket;
  83.   
  84. public class Server implements Runnable
  85. {
  86.     public void run()
  87.     {
  88.         try
  89.         {
  90.             //创建ServerSocket
  91.             ServerSocket serverSocket = new ServerSocket(5554);
  92.             while (true)
  93.             {
  94.                 //接受客户端请求
  95.                 Socket client = serverSocket.accept();
  96.                 System.out.println("accept");
  97.                 try
  98.                 {
  99.                     //接收客户端消息
  100.                     BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  101.                     String str = in.readLine();
  102.                     System.out.println("read:" + str);
  103.                     //向服务器发送消息
  104.                     PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true);
  105.                     out.println("server message");
  106.                     //关闭流
  107.                     out.close();
  108.                     in.close();
  109.                 }
  110.                 catch (Exception e)
  111.                 {
  112.                     System.out.println(e.getMessage());
  113.                     e.printStackTrace();
  114.                 }
  115.                 finally
  116.                 {
  117.                     //关闭
  118.                     client.close();
  119.                     System.out.println("close");
  120.                 }
  121.             }
  122.         }
  123.         catch (Exception e)
  124.         {
  125.             System.out.println(e.getMessage());
  126.         }
  127.     }
  128.     //main函数,开启服务器
  129.     public static void main(String a[])
  130.     {
  131.         Thread desktopServerThread = new Thread(new Server());
  132.         desktopServerThread.start();
  133.     }
  134. }

阅读(1842) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~