码迷,mamicode.com
首页 > 其他好文 > 详细

qpp的客户端

时间:2016-01-15 22:44:11      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.example.server;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.PrintWriter;
  7 import java.net.Socket;
  8 import java.net.UnknownHostException;
  9 
 10 import android.os.Bundle;
 11 import android.os.Handler;
 12 import android.app.Activity;
 13 import android.view.Menu;
 14 import android.view.View;
 15 import android.view.View.OnClickListener;
 16 import android.widget.Button;
 17 import android.widget.EditText;
 18 import android.widget.TextView;
 19 
 20 public class MainActivity extends Activity {
 21 
 22     private TextView text;
 23     private Button button;
 24     private EditText ed;
 25     private BufferedReader b;
 26     private Socket socket;
 27     private PrintWriter pw;
 28     private String s;
 29 
 30     Handler hand = new Handler() {
 31         public void handleMessage(android.os.Message msg) {
 32             // 获取服务器发送的东西
 33             String ss = (String) msg.obj;
 34             System.out.println(ss + "ssd的值");
 35             // 把服务器发送的东西显示在textview上
 36             text.setText(text.getText().toString() + "\t\n" + ss);
 37 
 38         };
 39     };
 40 
 41     @Override
 42     protected void onCreate(Bundle savedInstanceState) {
 43         super.onCreate(savedInstanceState);
 44         setContentView(R.layout.activity_main);
 45 
 46         text = (TextView) findViewById(R.id.text1);
 47         button = (Button) findViewById(R.id.button);
 48         ed = (EditText) findViewById(R.id.ed);
 49         // 开启线程
 50         new Thread() {
 51             public void run() {
 52 
 53                 try {
 54                     // 创建对象要链接的服务器的ip端口
 55                     socket = new Socket("10.1.3.96", 8888);
 56                     // 读取文件并设置编码
 57                     BufferedReader bf = new BufferedReader(
 58                             new InputStreamReader(socket.getInputStream(),
 59                                     "gbk"));
 60 
 61                     while (true) {
 62                         // 读取到的值
 63                         String str = bf.readLine();
 64                         System.out.println(str);
 65                         // 线程
 66                         hand.sendMessage(hand.obtainMessage(1, str));
 67                     }
 68 
 69                 } catch (UnknownHostException e) {
 70                     // TODO Auto-generated catch block
 71                     e.printStackTrace();
 72                 } catch (IOException e) {
 73                     // TODO Auto-generated catch block
 74                     e.printStackTrace();
 75                 }
 76 
 77             };
 78         }.start();
 79         // 点击按钮把输入框中的值发送出去
 80         button.setOnClickListener(new OnClickListener() {
 81 
 82             @Override
 83             public void onClick(View v) {
 84                 // TODO Auto-generated method stub
 85 
 86                 s = ed.getText().toString();
 87                 new Thread() {
 88                     public void run() {
 89 
 90                         try {
 91                             // 输出流
 92                             PrintWriter pw = new PrintWriter(socket
 93                                     .getOutputStream());
 94                             System.out.println("1");
 95                             // 把输入框中的值发送出去
 96                             pw.println(s);
 97                             pw.flush();
 98                         } catch (IOException e) {
 99                             // TODO Auto-generated catch block
100                             e.printStackTrace();
101                         }
102 
103                     };
104                 }.start();
105 
106             }
107         });
108     }
109 }

 

qpp的客户端

标签:

原文地址:http://www.cnblogs.com/zhang117/p/5134398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!