Android socket收发中文

接收信息分字符流和字节流

字节流:

byte[] buffer = new byte[1024]; //字节数组buffer

InputStream inputStream = socket.getInputStream();//获取字节流

int len = inputStream.read(buffer);//存入buffer的同时返回信息数量

String mess = new String(buffer,0,len);//转String

 

字符流:

char[] buffer = new char[100];//字符数组buffer

BufferedReader input = new BufferedReader(

           new InputStreamReader(socket.getInputStream(), "gb2312"));

//获取字符流,接编码格式

int len = input.read(buffer);

 

socket发送:

            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(mess.getBytes("gb2312"));

posted @ 2021-02-01 12:08  倔强的不死人  阅读(208)  评论(0编辑  收藏  举报