Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3534448
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: 嵌入式

2012-07-05 11:51:25

通过本文只是想来简单介绍一下关于android socket编程。

向上服务器端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package nate.PPT.control;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
 
 
public class PPTServer {
    private final static int RIGHT = 1;
    private final static int LEFT = 2;
    private final static int SHIFTF5 = 0;
    private final static int ESC = 3;
     
    private static int key;
    //注意这里用的输入输出流的对象
    private static ObjectInputStream fromClient;
    private static ObjectOutputStream fromServer;
     
    public static void main(String[] args) throws IOException,
                                    ClassNotFoundException, AWTException, InterruptedException{
        ServerSocket sSocket = new ServerSocket(2011);
        System.out.println("waiting a connection from the client");
        Robot robot = new Robot();     
        Socket sock = sSocket.accept();
        System.out.println("recv a connection");
        fromClient = new ObjectInputStream(sock.getInputStream());
        fromServer = new ObjectOutputStream(sock.getOutputStream());
        do{
            Choices choice = (Choices)fromClient.readObject();
            System.out.println("the flag is " + choice.getKey());
            key = choice.getKey();
            switch(key){
             
            case SHIFTF5:
                robot.keyPress(KeyEvent.VK_SHIFT);
                Thread.sleep(20);
                robot.keyPress(KeyEvent.VK_F5);
                Thread.sleep(10);
                robot.keyRelease(KeyEvent.VK_F5);
                robot.keyRelease(KeyEvent.VK_SHIFT);
                Thread.sleep(10);
                break;
                 
            case LEFT:
                robot.keyPress(KeyEvent.VK_LEFT);
                Thread.sleep(10);
                robot.keyRelease(KeyEvent.VK_LEFT);
                Thread.sleep(10);
                break;
                 
            case RIGHT:
                robot.keyPress(KeyEvent.VK_RIGHT);
                Thread.sleep(10);
                robot.keyRelease(KeyEvent.VK_RIGHT);
                Thread.sleep(10);
                break;
                 
            case ESC:
                robot.keyPress(KeyEvent.VK_ESCAPE);
                Thread.sleep(10);
                robot.keyPress(KeyEvent.VK_ESCAPE);
                Thread.sleep(10);
                break;
                 
                default:
                    break;
            }
        }while(key != -1);
        System.out.println("exit the app");
        fromClient.close();
        fromServer.close();
        sock.close();
        sSocket.close();
    }
}

本 例中,注意一下所用的输入输出流对象,关于这个java中已经很清楚了,就不多说。同时,本例中使用java中的Robot来模拟按键,即PPT中的快捷 键从而实现控制PPT的目的。当然,大家都知道,使用ObjectInputStream、ObjectOutputStream传输对象首先还需下面的 条件。即传送的对象所属的类,该类必须实现Serializable接口!同时注意在android手机客户端,我们需要同样拥有这样一个类型!!!将此 类copy过去即可,这些都是java中的知识。

  

上面类包含了传输的信息数据内容。

来看看client端的代码,部署在android手机端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package nate.PPT.control;
 
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
 
public class PPTClient extends Activity {
    private Button start;
    private Button escape;
    private Button forward;
    private Button back;
    private Socket sock;
    private ObjectOutputStream fromClient;
    private ObjectInputStream fromServer;
    private final static int RIGHT = 1;
    private final static int LEFT = 2;
    private final static int SHIFTF5 = 0;
    private final static int ESC = 3;
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        try {
            //sock = new Socket(InetAddress.getByName("125.71.69.199"),2011);
            sock = new Socket(InetAddress.getByName("125.70.223.165"),2011);
            fromClient = new ObjectOutputStream(sock.getOutputStream());
            fromServer = new ObjectInputStream(sock.getInputStream());
             
            } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
         
        start = (Button)this.findViewById(R.id.start);
        escape = (Button)this.findViewById(R.id.escape);
        forward = (Button)this.findViewById(R.id.froward);
        back = (Button)this.findViewById(R.id.back);
         
        start.setOnClickListener(new Button.OnClickListener(){
 
            @Override
            public void onClick(View v) {
                Choices choice = new Choices(SHIFTF5);
                try {
                    fromClient.writeObject(choice);
                    System.out.println("send the start shift + f5");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
             
        });
         
        escape.setOnClickListener(new Button.OnClickListener(){
 
            @Override
            public void onClick(View arg0) {
                Choices  choice = new Choices(ESC);
                try {
                    fromClient.writeObject(choice);
                    System.out.println("send the escape");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        forward.setOnClickListener(new Button.OnClickListener(){
 
            @Override
            public void onClick(View v) {
                Choices choice = new Choices(RIGHT);
                try {
                    fromClient.writeObject(choice);
                    System.out.println("send the right (the next)");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                 
            }
             
        });
        back.setOnClickListener(new Button.OnClickListener(){
 
            @Override
            public void onClick(View v) {
                Choices choice = new Choices(LEFT);
                try {
                    fromClient.writeObject(choice);
                    System.out.println("send the left (the last)");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
     
    /**
     * 监听BACK键
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {  
        if ( event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("exit app");
            builder.setMessage("You will exit the app...");
            //builder.setIcon(R.drawable.stat_sys_warning);
            builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(startMain);
                    System.exit(0);
                }
            });
            builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                 
                @Override
                public void onClick(DialogInterface dialog, int which) {
                     
                }
            });
            builder.show();
        }
        return super.onKeyDown(keyCode, event);
    }
}

 

代码还是很简单的,这里不多说了,强调一下的是,client端除了一个activity的类外,还有上面的Choices类!!!与服务器端的类型一模一样!同时,别忘记了需要在android manifest.XML文件中添加

1
2

用户权限!!!别忘记添加。。。
当然,代码还有很多需要改进的地方,比如要解决按下可能延迟PPT没有 反应,但是又不知道是否真的按下等问题,我们可以在手机端的按钮上加上一个震动的效果,这样我们就能准确的知道我们是否按下手机上的按键。这个应该不难 吧!不过本篇文章主要还是简单介绍android socket编程与PC的连接。


由于大家需要的网友比较多,我已经将资源放在此处,,大家可以去自由下载,谢谢。

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