ITeye博客

输入法 总结-3

mickey_hou · 2013-07-04 17:28 · andriod · 移动开发 · Markdown

public class InputApplication extends Application { private static final int SERVER_PORT = 9999;

private String clientAsk;

private Handler handler;

private Socket socket = null;

public String getClientAsk()
{
    return clientAsk;
}

public void setClientAsk(String clientAsk)
{
    this.clientAsk = clientAsk;
}

public void startThread()
{
    new ServerThread().start();
}

private void createMessage(int code,String data)
{
    Bundle bundle = new Bundle();
    bundle.putString("ask", data);
    Message msg = new Message();
    msg.what = code;
    msg.setData(bundle);
    handler.dispatchMessage(msg);
}

class ServerThread extends Thread
{
    @Override
    public void run()
    {
        DataInputStream dis = null;
        try
        {
            String ask = null;
            ServerSocket ss = new ServerSocket(SERVER_PORT);
            while (true) 
            {
                // 服务器接收到客户端的数据后,创建与此客户端对话的Socket
                Socket socket = ss.accept();
                // 用于接收客户端发来的数据的输入流
                dis = new DataInputStream(socket.getInputStream());
                ask = dis.readUTF();
                Log.v("INPUT", "-----------"+ask);
                if (handler != null)
                {
                    createMessage(10, ask);
                }
                System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
            }

// socket = new Socket("10.74.143.45",SERVER_PORT); // //获取输出流,用于客户端向服务器端发送数据 // DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); // //获取输入流,用于接收服务器端发送来的数据 // dis = new DataInputStream(socket.getInputStream()); // //客户端向服务器端发送数据 // dos.writeUTF("我是客户端,请求连接!"); // //打印出从服务器端接收到的数据 // String ask = dis.readUTF(); // Log.v("INPUT", "-----------"+ask); } catch (IOException e) { }

    }
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。