Android-Socket-SocketDemo-AndroidStudio

来源:互联网 发布:民用枪持枪证 知乎 编辑:程序博客网 时间:2024/06/14 16:29

转载请注明出处:http://blog.csdn.net/iwanghang

觉得博文有用,请点赞,请评论,请关注,谢谢!~


上一篇博文,我们让java服务端和java客户端,实现了消息互通。

这篇博文,我们来看一下,如何让java服务端和Andrid客户端,实现消息互通。

Android使用的Demo,来自 http://blog.csdn.net/carson_ho/article/details/53366856,https://github.com/Carson-Ho/Socket_learning。




java服务端的代码,跟上一篇相同,下面贴一下Android端代码:

package scut.carson_ho.socket_carson;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MainActivity extends AppCompatActivity {    /**     * 主 变量     */    // 主线程Handler    // 用于将从服务器获取的消息显示出来    private Handler mMainHandler;    // Socket变量    private Socket socket;    // 线程池    // 为了方便展示,此处直接采用线程池进行线程管理,而没有一个个开线程    private ExecutorService mThreadPool;    /**     * 接收服务器消息 变量     */    // 输入流对象    InputStream is;    // 输入流读取器对象    InputStreamReader isr ;    BufferedReader br ;    // 接收服务器发送过来的消息    String response;    /**     * 发送消息到服务器 变量     */    // 输出流对象    OutputStream outputStream;    /**     * 按钮 变量     */    // 连接 断开连接 发送数据到服务器 的按钮变量    private Button btnConnect, btnDisconnect, btnSend;    // 显示接收服务器消息 按钮    private TextView Receive,receive_message;    // 输入需要发送的消息 输入框    private EditText mEdit;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /**         * 初始化操作         */        // 初始化所有按钮        btnConnect = (Button) findViewById(R.id.connect);        btnDisconnect = (Button) findViewById(R.id.disconnect);        btnSend = (Button) findViewById(R.id.send);        mEdit = (EditText) findViewById(R.id.edit);        receive_message = (TextView) findViewById(R.id.receive_message);        Receive = (Button) findViewById(R.id.Receive);        // 初始化线程池        mThreadPool = Executors.newCachedThreadPool();        // 实例化主线程,用于更新接收过来的消息        mMainHandler = new Handler() {            @Override            public void handleMessage(Message msg) {                switch (msg.what) {                    case 0:                        receive_message.setText(response);                        break;                }            }        };        /**         * 创建客户端 & 服务器的连接         */        btnConnect.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 利用线程池直接开启一个线程 & 执行该线程                mThreadPool.execute(new Runnable() {                    @Override                    public void run() {                        try {                            // 创建Socket对象 & 指定服务端的IP 及 端口号                            socket = new Socket("192.168.1.130", 9000);                            // 判断客户端和服务器是否连接成功                            System.out.println(socket.isConnected());                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                });            }        });        /**         * 接收 服务器消息         */        Receive.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 利用线程池直接开启一个线程 & 执行该线程                mThreadPool.execute(new Runnable() {                    @Override                    public void run() {                          try {                            // 步骤1:创建输入流对象InputStream                            is = socket.getInputStream();                              // 步骤2:创建输入流读取器对象 并传入输入流对象                              // 该对象作用:获取服务器返回的数据                              isr = new InputStreamReader(is);                              br = new BufferedReader(isr);                              // 步骤3:通过输入流读取器对象 接收服务器发送过来的数据                              response = br.readLine();                              // 步骤4:通知主线程,将接收的消息显示到界面                              Message msg = Message.obtain();                              msg.what = 0;                              mMainHandler.sendMessage(msg);                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                });            }        });        /**         * 发送消息 给 服务器         */        btnSend.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 利用线程池直接开启一个线程 & 执行该线程                mThreadPool.execute(new Runnable() {                    @Override                    public void run() {                        try {                            // 步骤1:从Socket 获得输出流对象OutputStream                            // 该对象作用:发送数据                            outputStream = socket.getOutputStream();                            // 步骤2:写入需要发送的数据到输出流对象中                            outputStream.write(("Client:" + mEdit.getText().toString()+"\n").getBytes("utf-8"));                            // 特别注意:数据的结尾加上换行符才可让服务器端的readline()停止阻塞                            // 步骤3:发送数据到服务端                            outputStream.flush();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                });            }        });        /**         * 断开客户端 & 服务器的连接         */        btnDisconnect.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                try {                    // 断开 客户端发送到服务器 的连接,即关闭输出流对象OutputStream                    outputStream.close();                    // 断开 服务器发送到客户端 的连接,即关闭输入流读取器对象BufferedReader                    br.close();                    // 最终关闭整个Socket连接                    socket.close();                    // 判断客户端和服务器是否已经断开连接                    System.out.println(socket.isConnected());                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="scut.carson_ho.socket_carson.MainActivity">    <Button        android:id="@+id/connect"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="connect" />    <Button        android:id="@+id/disconnect"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="disconnect" />    <TextView        android:id="@+id/receive_message"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/Receive"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Receive from message" />    <EditText        android:id="@+id/edit"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/send"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="send"/></LinearLayout>


哇~又水了一篇博文!真是机智如我!安静


转载请注明出处:http://blog.csdn.net/iwanghang




欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~