android实现socket连接(服务器端)

来源:互联网 发布:淘宝平台服务协议 编辑:程序博客网 时间:2024/05/18 00:19

服务器端运行在android手机上,和上一篇是一个Demo,大家注意给应用加权限,写的有点乱,有些变量没有用到。

如果是和电脑进行通信,注意编码问题,我把两种编码都写出来了,有不明白可以留言。

public class MainActivity extends Activity {
    EditText mContent;// 发送内容
    Button mBtn;// 发送按钮
    Handler mHandle;
    ServerSocket serverSocket;
    int port = 8001;
    OutputStream output;
    InputStream input;
    DataInputStream inputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 服务器的内容显示区
        mContent = (EditText) findViewById(R.id.X_Content_Edit);

        mHandle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 0x123:
                    Toast.makeText(getApplicationContext(), "有客户端连接", Toast.LENGTH_SHORT).show();
                    break;
                case 0x124:
                    Bundle bundle = msg.getData();
                    mContent.append(bundle.getByteArray("byte") + "\n");
                    //mContent.append(bundle.getString("byte")+"\n");
                    break;
                default:
                    break;
                }
            }
        };

    }

    public class ServerThread implements Runnable {
        @Override
        public void run() {
            try {

                serverSocket = new ServerSocket(port);
                // 阻塞在这里 等待建立连接
                Socket socket = serverSocket.accept();
                mHandle.sendEmptyMessage(0x123);// 发送消息提示有连接
                // input = socket.getInputStream();// 获取输入流
                inputStream = new DataInputStream(socket.getInputStream());
                output=socket.getOutputStream();
                while (true) {
                    Message msg = Message.obtain();
                    byte[] buff = {};
                    buff = new byte[inputStream.available()];
                    if (buff.length != 0) {
                        inputStream.read(buff);// 读取到缓存中
                        Bundle bundle = new Bundle();
                        // 1.传递字节数组 与UT8-8编码相同通信,
                        bundle.putByteArray("byte", buff);
                        // 2.传递字符串 与PC端通信需要把GBK转为UTF-8
                        //bundle.putString("byte", new String(buff, "gbk"));
                        msg.setData(bundle);
                        msg.what = 0x124;
                        mHandle.sendMessage(msg);
                        //PC GBK编码 发送
                        //output.write("服务器收到\n".getBytes("GBK"));
                        //android UTF-8 编码发送
                        output.write("服务器收到\n".getBytes("UTF-8"));
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

/*    public class DoThread implements Runnable {
        @Override
        public void run() {
            output.write(bundle.getString("context").getBytes("UTF-8"));
        }
    }*/

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new ServerThread()).start();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            inputStream.close();
            output.close();
            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

0 0
原创粉丝点击