因为项目上需要实现一个TCP Client 端;在网上找好多例子基本上都是阻塞方式完成;
我的实现例子:由Activity 及sever 来实现,在sever 创建一个线程来监听接受数据。收到数据,通过广播发送给Activity;
服务端我没有去实现,你可以下载TCP Socket 调试工具v2.2;创建个9005端口;客户端:访问的IP为10.0.2.2
AnetTest.java
1
/*
*
2 * Copyright 2010 archfree
3 *
4 */
5 package com . archfree . demo;
6
7 import android . app . Activity;
8 import android . content . BroadcastReceiver;
9 import android . content . ComponentName;
10 import android . content . Context;
11 import android . content . Intent;
12 import android . content . IntentFilter;
13 import android . content . ServiceConnection;
14
15 import android . os . Bundle;
16 import android . os . IBinder;
17 import android . util . Log;
18 import android . view . View;
19 import android . view . View . OnClickListener;
20 import android . widget . Button;
21 import android . widget . EditText;
22 import android . widget . TextView;
23
24 public class AnetTest extends Activity {
25
26 /* *
27 * 通过ServiceConnection的内部类实现来连接Service和Activity
28 *
29 */
30 public static final String TAG = " AnetTest " ;
31 private static final boolean DEBUG = true ; // false
32 private String msg = " " ;
33 private UpdateReceiver mReceiver;
34 private Context mContext;
35 private ReceiveMessage mReceiveMessage;
36
37 // 实现一个 BroadcastReceiver,用于接收指定的 Broadcast
38 public class UpdateReceiver extends BroadcastReceiver {
39
40 @Override
41 public void onReceive(Context context, Intent intent) {
42 if (DEBUG)
43 Log . d(TAG, " onReceive: " + intent);
44 msg = intent . getStringExtra( " msg " );
45 System . out . println( " recv: " + msg);
46 // System.out.println();
47 ((EditText) findViewById(R . id . tv_recv)) . append(msg + " /n " );
48
49 }
50
51 }
52
53 private ServiceConnection serviceConnection = new ServiceConnection() {
54
55 @Override
56 public void onServiceConnected(ComponentName name, IBinder service) {
57
58 mReceiveMessage = ((ReceiveMessage . LocalBinder) service)
59 . getService();
60 if (DEBUG)
61 Log . d(TAG, " on serivce connected " );
62
63 }
64
65 @Override
66 public void onServiceDisconnected(ComponentName name) {
67 mReceiveMessage = null ;
68 }
69 } ;
70
71 /* * Called when the activity is first created. */
72 @Override
73 public void onCreate(Bundle savedInstanceState) {
74 super . onCreate(savedInstanceState);
75 setContentView(R . layout . main);
76
77 // 实例化自定义的 BroadcastReceiver
78 mReceiver = new UpdateReceiver();
79 IntentFilter filter = new IntentFilter();
80 // 为 BroadcastReceiver 指定 action ,使之用于接收同 action 的广播
81 filter . addAction( " com.archfree.demo.msg " );
82
83 // 以编程方式注册 BroadcastReceiver 。配置方式注册 BroadcastReceiver 的例子见
84 // AndroidManifest.xml 文件
85 // 一般在 OnStart 时注册,在 OnStop
2 * Copyright 2010 archfree
3 *
4 */
5 package com . archfree . demo;
6
7 import android . app . Activity;
8 import android . content . BroadcastReceiver;
9 import android . content . ComponentName;
10 import android . content . Context;
11 import android . content . Intent;
12 import android . content . IntentFilter;
13 import android . content . ServiceConnection;
14
15 import android . os . Bundle;
16 import android . os . IBinder;
17 import android . util . Log;
18 import android . view . View;
19 import android . view . View . OnClickListener;
20 import android . widget . Button;
21 import android . widget . EditText;
22 import android . widget . TextView;
23
24 public class AnetTest extends Activity {
25
26 /* *
27 * 通过ServiceConnection的内部类实现来连接Service和Activity
28 *
29 */
30 public static final String TAG = " AnetTest " ;
31 private static final boolean DEBUG = true ; // false
32 private String msg = " " ;
33 private UpdateReceiver mReceiver;
34 private Context mContext;
35 private ReceiveMessage mReceiveMessage;
36
37 // 实现一个 BroadcastReceiver,用于接收指定的 Broadcast
38 public class UpdateReceiver extends BroadcastReceiver {
39
40 @Override
41 public void onReceive(Context context, Intent intent) {
42 if (DEBUG)
43 Log . d(TAG, " onReceive: " + intent);
44 msg = intent . getStringExtra( " msg " );
45 System . out . println( " recv: " + msg);
46 // System.out.println();
47 ((EditText) findViewById(R . id . tv_recv)) . append(msg + " /n " );
48
49 }
50
51 }
52
53 private ServiceConnection serviceConnection = new ServiceConnection() {
54
55 @Override
56 public void onServiceConnected(ComponentName name, IBinder service) {
57
58 mReceiveMessage = ((ReceiveMessage . LocalBinder) service)
59 . getService();
60 if (DEBUG)
61 Log . d(TAG, " on serivce connected " );
62
63 }
64
65 @Override
66 public void onServiceDisconnected(ComponentName name) {
67 mReceiveMessage = null ;
68 }
69 } ;
70
71 /* * Called when the activity is first created. */
72 @Override
73 public void onCreate(Bundle savedInstanceState) {
74 super . onCreate(savedInstanceState);
75 setContentView(R . layout . main);
76
77 // 实例化自定义的 BroadcastReceiver
78 mReceiver = new UpdateReceiver();
79 IntentFilter filter = new IntentFilter();
80 // 为 BroadcastReceiver 指定 action ,使之用于接收同 action 的广播
81 filter . addAction( " com.archfree.demo.msg " );
82
83 // 以编程方式注册 BroadcastReceiver 。配置方式注册 BroadcastReceiver 的例子见
84 // AndroidManifest.xml 文件
85 // 一般在 OnStart 时注册,在 OnStop